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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #-*- coding: utf-8 -*-
  2. import warnings
  3. import datetime
  4. import time
  5. import os
  6. from lodel.context import LodelContext
  7. LodelContext.expose_modules(globals(), {
  8. 'lodel.leapi.datahandlers.base_classes': ['DataField'],
  9. 'lodel.exceptions': ['LodelException', 'LodelExceptions',
  10. 'LodelFatalError', 'DataNoneValid', 'FieldValidationError']})
  11. ##@brief Data field designed to handle boolean values
  12. class Boolean(DataField):
  13. help = 'A basic boolean field'
  14. base_type = 'bool'
  15. ##@brief A boolean field
  16. def __init__(self, **kwargs):
  17. #if 'check_data_value' not in kwargs:
  18. # kwargs['check_data_value'] = self._check_data_value
  19. super().__init__(ftype='bool', **kwargs)
  20. ##@brief Check and cast value in appropriate type
  21. #@param value *
  22. #@throw FieldValidationError if value is unappropriate or can not be cast
  23. #@return value
  24. def _check_data_value(self, value):
  25. value = super()._check_data_value(value)
  26. if not isinstance(value, bool):
  27. raise FieldValidationError("The value '%s' is not, and will never, be a boolean" % value)
  28. return value
  29. ##@brief Data field designed to handle integer values
  30. class Integer(DataField):
  31. help = 'Basic integer field'
  32. base_type = 'int'
  33. cast_type = int
  34. def __init__(self, **kwargs):
  35. super().__init__( **kwargs)
  36. ##@brief Check and cast value in appropriate type
  37. #@param value *
  38. #@throw FieldValidationError if value is unappropriate or can not be cast
  39. #@return value
  40. def _check_data_value(self, value, strict = False):
  41. value = super()._check_data_value(value)
  42. if (strict and not isinstance(value, int)):
  43. raise FieldValidationError("The value '%s' is not a python type integer" % value)
  44. try:
  45. if strict:
  46. value = int(value)
  47. else:
  48. value = int(float(value))
  49. except(ValueError, TypeError):
  50. raise FieldValidationError("The value '%s' is not, and will never, be an integer" % value)
  51. return value
  52. ##@brief Data field designed to handle string
  53. class Varchar(DataField):
  54. help = 'Basic string (varchar) field. Default size is 64 characters'
  55. base_type = 'char'
  56. ##@brief A string field
  57. # @brief max_length int: The maximum length of this field
  58. def __init__(self, max_length=64, **kwargs):
  59. self.max_length = int(max_length)
  60. super().__init__(**kwargs)
  61. ##@brief checks if this class can override the given data handler
  62. # @param data_handler DataHandler
  63. # @return bool
  64. def can_override(self, data_handler):
  65. if not super().can_override(data_handler):
  66. return False
  67. if data_handler.max_length != self.max_length:
  68. return False
  69. return True
  70. ##@brief Check and cast value in appropriate type
  71. #@param value *
  72. #@throw FieldValidationError if value is unappropriate or can not be cast
  73. #@return value
  74. def _check_data_value(self, value):
  75. value = super()._check_data_value(value)
  76. if not isinstance(value, str):
  77. raise FieldValidationError("The value '%s' can't be a str" % value)
  78. if len(value) > self.max_length:
  79. raise FieldValidationError("The value '%s' is longer than the maximum length of this field (%s)" % (value, self.max_length))
  80. return value
  81. ##@brief Data field designed to handle date & time
  82. class DateTime(DataField):
  83. help = 'A datetime field. Take two boolean options now_on_update and now_on_create'
  84. base_type = 'datetime'
  85. ##@brief A datetime field
  86. # @param now_on_update bool : If true, the date is set to NOW on update
  87. # @param now_on_create bool : If true, the date is set to NEW on creation
  88. # @param **kwargs
  89. def __init__(self, now_on_update=False, now_on_create=False, **kwargs):
  90. self.now_on_update = now_on_update
  91. self.now_on_create = now_on_create
  92. self.datetime_format = '%Y-%m-%d' if 'format' not in kwargs else kwargs['format']
  93. super().__init__(**kwargs)
  94. ##@brief Check and cast value in appropriate type
  95. #@param value *
  96. #@throw FieldValidationError if value is unappropriate or can not be cast
  97. #@return value
  98. def _check_data_value(self, value):
  99. value = super()._check_data_value(value)
  100. if isinstance(value,str):
  101. try:
  102. value = datetime.datetime.fromtimestamp(time.mktime(time.strptime(value, self.datetime_format)))
  103. except ValueError:
  104. raise FieldValidationError("The value '%s' cannot be converted as a datetime" % value)
  105. if not isinstance(value, datetime.datetime):
  106. raise FieldValidationError("Tue value has to be a string or a datetime")
  107. return value
  108. def _construct_data(self, emcomponent, fname, datas, cur_value):
  109. if (self.now_on_create and cur_value is None) or self.now_on_update:
  110. return datetime.datetime.now()
  111. return cur_value
  112. ##@brief Data field designed to handle long string
  113. class Text(DataField):
  114. help = 'A text field (big string)'
  115. base_type = 'text'
  116. def __init__(self, **kwargs):
  117. super(self.__class__, self).__init__(ftype='text', **kwargs)
  118. ##@brief Check and cast value in appropriate type
  119. #@param value *
  120. #@throw FieldValidationError if value is unappropriate or can not be cast
  121. #@return value
  122. def _check_data_value(self, value):
  123. value = super()._check_data_value(value)
  124. if not isinstance(value, str):
  125. raise FieldValidationError("The content passed to this Text field is not a convertible to a string")
  126. return value
  127. ##@brief Data field designed to handle Files
  128. class File(DataField):
  129. base_type = 'file'
  130. ##@brief a file field
  131. # @param upload_path str : None by default
  132. # @param **kwargs
  133. def __init__(self, upload_path=None, **kwargs):
  134. self.upload_path = upload_path
  135. super().__init__(**kwargs)
  136. # @todo Add here a check for the validity of the given value (should have a correct path syntax)
  137. def _check_data_value(self, value):
  138. return super()._check_data_value(value)