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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #-*- coding: utf-8 -*-
  2. import warnings
  3. from lodel.leapi.datahandlers.base_classes import DataField
  4. ##@brief Data field designed to handle boolean values
  5. class Boolean(DataField):
  6. help = 'A basic boolean field'
  7. base_type = 'bool'
  8. ##@brief A boolean field
  9. def __init__(self, **kwargs):
  10. if 'check_data_value' not in kwargs:
  11. kwargs['check_data_value'] = self.check_value
  12. super().__init__(ftype='bool', **kwargs)
  13. def _check_data_value(self, value):
  14. error = None
  15. try:
  16. value = bool(value)
  17. except(ValueError, TypeError):
  18. error = TypeError("The value '%s' is not, and will never, be a boolean" % value)
  19. return value, error
  20. ##@brief Data field designed to handle integer values
  21. class Integer(DataField):
  22. help = 'Basic integer field'
  23. base_type = 'int'
  24. def __init__(self, **kwargs):
  25. super().__init__( **kwargs)
  26. def _check_data_value(self, value):
  27. error = None
  28. try:
  29. value = int(value)
  30. except(ValueError, TypeError):
  31. error = TypeError("The value '%s' is not, and will never, be an integer" % value)
  32. return value, error
  33. ##@brief Data field designed to handle string
  34. class Varchar(DataField):
  35. help = 'Basic string (varchar) field. Default size is 64 characters'
  36. base_type = 'char'
  37. ##@brief A string field
  38. # @brief max_length int: The maximum length of this field
  39. def __init__(self, max_length=64, **kwargs):
  40. self.max_length = int(max_length)
  41. super().__init__(**kwargs)
  42. ##@brief checks if this class can override the given data handler
  43. # @param data_handler DataHandler
  44. # @return bool
  45. def can_override(self, data_handler):
  46. if not super().can_override(data_handler):
  47. return False
  48. if data_handler.max_length != self.max_length:
  49. return False
  50. return True
  51. def _check_data_value(self, value):
  52. error = None
  53. try:
  54. value = str(value)
  55. except(ValueError, TypeError):
  56. error = TypeError("The value '%s' can't be a str" % value)
  57. return value, error
  58. ##@brief Data field designed to handle date & time
  59. class DateTime(DataField):
  60. help = 'A datetime field. Take two boolean options now_on_update and now_on_create'
  61. base_type = 'datetime'
  62. ##@brief A datetime field
  63. # @param now_on_update bool : If true, the date is set to NOW on update
  64. # @param now_on_create bool : If true, the date is set to NEW on creation
  65. # @param **kwargs
  66. def __init__(self, now_on_update=False, now_on_create=False, **kwargs):
  67. self.now_on_update = now_on_update
  68. self.now_on_create = now_on_create
  69. super().__init__(**kwargs)
  70. def _check_data_value(self, value):
  71. error = None
  72. return value, error
  73. ##@brief Data field designed to handle long string
  74. class Text(DataField):
  75. help = 'A text field (big string)'
  76. base_type = 'text'
  77. def __init__(self, **kwargs):
  78. super(self.__class__, self).__init__(ftype='text', **kwargs)
  79. def _check_data_value(self, value):
  80. error = None
  81. return value, error
  82. ##@brief Data field designed to handle Files
  83. class File(DataField):
  84. base_type = 'file'
  85. ##@brief a file field
  86. # @param upload_path str : None by default
  87. # @param **kwargs
  88. def __init__(self, upload_path=None, **kwargs):
  89. self.upload_path = upload_path
  90. super().__init__(**kwargs)
  91. def _check_data_value(self, value):
  92. error = None
  93. return value, error