暫無描述
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.8KB

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