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 4.1KB

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