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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. value = int(value)
  44. else:
  45. value = int(float(value))
  46. except(ValueError, TypeError):
  47. raise FieldValidationError("The value '%s' is not, and will never, be an integer" % value)
  48. return value
  49. ##@brief Data field designed to handle string
  50. class Varchar(DataField):
  51. help = 'Basic string (varchar) field. Default size is 64 characters'
  52. base_type = 'char'
  53. ##@brief A string field
  54. # @brief max_length int: The maximum length of this field
  55. def __init__(self, max_length=64, **kwargs):
  56. self.max_length = int(max_length)
  57. super().__init__(**kwargs)
  58. ##@brief checks if this class can override the given data handler
  59. # @param data_handler DataHandler
  60. # @return bool
  61. def can_override(self, data_handler):
  62. if not super().can_override(data_handler):
  63. return False
  64. if data_handler.max_length != self.max_length:
  65. return False
  66. return True
  67. ##@brief Check and cast value in appropriate type
  68. #@param value *
  69. #@throw FieldValidationError if value is unappropriate or can not be cast
  70. #@return value
  71. def _check_data_value(self, value):
  72. value = super()._check_data_value(value)
  73. if not isinstance(value, str):
  74. raise FieldValidationError("The value '%s' can't be a str" % value)
  75. if len(value) > self.max_length:
  76. raise FieldValidationError("The value '%s' is longer than the maximum length of this field (%s)" % (value, self.max_length))
  77. return value
  78. ##@brief Data field designed to handle date & time
  79. class DateTime(DataField):
  80. help = 'A datetime field. Take two boolean options now_on_update and now_on_create'
  81. base_type = 'datetime'
  82. ##@brief A datetime field
  83. # @param now_on_update bool : If true, the date is set to NOW on update
  84. # @param now_on_create bool : If true, the date is set to NEW on creation
  85. # @param **kwargs
  86. def __init__(self, now_on_update=False, now_on_create=False, **kwargs):
  87. self.now_on_update = now_on_update
  88. self.now_on_create = now_on_create
  89. self.datetime_format = '%Y-%m-%d' if 'format' not in kwargs else kwargs['format']
  90. super().__init__(**kwargs)
  91. ##@brief Check and cast value in appropriate type
  92. #@param value *
  93. #@throw FieldValidationError if value is unappropriate or can not be cast
  94. #@return value
  95. def _check_data_value(self, value):
  96. value = super()._check_data_value(value)
  97. if isinstance(value,str):
  98. try:
  99. value = datetime.datetime.fromtimestamp(time.mktime(time.strptime(value, self.datetime_format)))
  100. except ValueError:
  101. raise FieldValidationError("The value '%s' cannot be converted as a datetime" % value)
  102. if not isinstance(value, datetime.datetime):
  103. raise FieldValidationError("Tue value has to be a string or a datetime")
  104. return value
  105. def _construct_data(self, emcomponent, fname, datas, cur_value):
  106. if (self.now_on_create and cur_value is None) or self.now_on_update:
  107. return datetime.datetime.now()
  108. return cur_value
  109. ##@brief Data field designed to handle long string
  110. class Text(DataField):
  111. help = 'A text field (big string)'
  112. base_type = 'text'
  113. def __init__(self, **kwargs):
  114. super(self.__class__, self).__init__(ftype='text', **kwargs)
  115. ##@brief Check and cast value in appropriate type
  116. #@param value *
  117. #@throw FieldValidationError if value is unappropriate or can not be cast
  118. #@return value
  119. def _check_data_value(self, value):
  120. value = super()._check_data_value(value)
  121. if not isinstance(value, str):
  122. raise FieldValidationError("The content passed to this Text field is not a convertible to a string")
  123. return value
  124. ##@brief Data field designed to handle Files
  125. class File(DataField):
  126. base_type = 'file'
  127. ##@brief a file field
  128. # @param upload_path str : None by default
  129. # @param **kwargs
  130. def __init__(self, upload_path=None, **kwargs):
  131. self.upload_path = upload_path
  132. super().__init__(**kwargs)
  133. # @todo Add here a check for the validity of the given value (should have a correct path syntax)
  134. def _check_data_value(self, value):
  135. return super()._check_data_value(value)