No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

datas_base.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #-*- coding: utf-8 -*-
  2. import warnings
  3. import datetime
  4. from lodel.leapi.datahandlers.base_classes import DataField
  5. ##@brief Data field designed to handle boolean values
  6. class Boolean(DataField):
  7. help = 'A basic boolean field'
  8. base_type = 'bool'
  9. ##@brief A boolean field
  10. def __init__(self, **kwargs):
  11. if 'check_data_value' not in kwargs:
  12. kwargs['check_data_value'] = self.check_value
  13. super().__init__(ftype='bool', **kwargs)
  14. def _check_data_value(self, value):
  15. error = None
  16. try:
  17. value = bool(value)
  18. except(ValueError, TypeError):
  19. error = TypeError("The value '%s' is not, and will never, be a boolean" % value)
  20. return value, error
  21. ##@brief Data field designed to handle integer values
  22. class Integer(DataField):
  23. help = 'Basic integer field'
  24. base_type = 'int'
  25. def __init__(self, **kwargs):
  26. super().__init__( **kwargs)
  27. def _check_data_value(self, value):
  28. error = None
  29. try:
  30. value = int(value)
  31. except(ValueError, TypeError):
  32. error = TypeError("The value '%s' is not, and will never, be an integer" % value)
  33. return value, error
  34. ##@brief Data field designed to handle string
  35. class Varchar(DataField):
  36. help = 'Basic string (varchar) field. Default size is 64 characters'
  37. base_type = 'char'
  38. ##@brief A string field
  39. # @brief max_length int: The maximum length of this field
  40. def __init__(self, max_length=64, **kwargs):
  41. self.max_length = int(max_length)
  42. super().__init__(**kwargs)
  43. ##@brief checks if this class can override the given data handler
  44. # @param data_handler DataHandler
  45. # @return bool
  46. def can_override(self, data_handler):
  47. if not super().can_override(data_handler):
  48. return False
  49. if data_handler.max_length != self.max_length:
  50. return False
  51. return True
  52. def _check_data_value(self, value):
  53. error = None
  54. try:
  55. value = str(value)
  56. except(ValueError, TypeError):
  57. error = TypeError("The value '%s' can't be a str" % value)
  58. return value, error
  59. ##@brief Data field designed to handle date & time
  60. class DateTime(DataField):
  61. help = 'A datetime field. Take two boolean options now_on_update and now_on_create'
  62. base_type = 'datetime'
  63. ##@brief A datetime field
  64. # @param now_on_update bool : If true, the date is set to NOW on update
  65. # @param now_on_create bool : If true, the date is set to NEW on creation
  66. # @param **kwargs
  67. def __init__(self, now_on_update=False, now_on_create=False, **kwargs):
  68. self.now_on_update = now_on_update
  69. self.now_on_create = now_on_create
  70. super().__init__(**kwargs)
  71. def _check_data_value(self, value):
  72. error = None
  73. return value, error
  74. def construct_data(self, emcomponent, fname, datas, cur_value):
  75. if (self.now_on_create and cur_value is None) or self.now_on_update:
  76. return datetime.datetime.now()
  77. return cur_value
  78. ##@brief Data field designed to handle long string
  79. class Text(DataField):
  80. help = 'A text field (big string)'
  81. base_type = 'text'
  82. def __init__(self, **kwargs):
  83. super(self.__class__, self).__init__(ftype='text', **kwargs)
  84. def _check_data_value(self, value):
  85. error = None
  86. return value, error
  87. ##@brief Data field designed to handle Files
  88. class File(DataField):
  89. base_type = 'file'
  90. ##@brief a file field
  91. # @param upload_path str : None by default
  92. # @param **kwargs
  93. def __init__(self, upload_path=None, **kwargs):
  94. self.upload_path = upload_path
  95. super().__init__(**kwargs)
  96. def _check_data_value(self, value):
  97. error = None
  98. return value, error