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

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