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.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. ##@brief Data field designed to handle date & time
  52. class DateTime(DataField):
  53. help = 'A datetime field. Take two boolean options now_on_update and now_on_create'
  54. base_type = 'datetime'
  55. ##@brief A datetime field
  56. # @param now_on_update bool : If true, the date is set to NOW on update
  57. # @param now_on_create bool : If true, the date is set to NEW on creation
  58. # @param **kwargs
  59. def __init__(self, now_on_update=False, now_on_create=False, **kwargs):
  60. self.now_on_update = now_on_update
  61. self.now_on_create = now_on_create
  62. super().__init__(**kwargs)
  63. ##@brief Data field designed to handle long string
  64. class Text(DataField):
  65. help = 'A text field (big string)'
  66. base_type = 'text'
  67. def __init__(self, **kwargs):
  68. super(self.__class__, self).__init__(ftype='text', **kwargs)
  69. ##@brief Data field designed to handle Files
  70. class File(DataField):
  71. base_type = 'file'
  72. ##@brief a file field
  73. # @param upload_path str : None by default
  74. # @param **kwargs
  75. def __init__(self, upload_path=None, **kwargs):
  76. self.upload_path = upload_path
  77. super().__init__(**kwargs)