설명 없음
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.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #-*- coding: utf-8 -*-
  2. import warnings
  3. import inspect
  4. import re
  5. from lodel.context import LodelContext
  6. LodelContext.expose_modules(globals(), {
  7. 'lodel.leapi.datahandlers.datas_base': ['Boolean', 'Integer', 'Varchar',
  8. 'DateTime', 'Text', 'File'],
  9. 'lodel.exceptions': ['LodelException', 'LodelExceptions',
  10. 'LodelFatalError', 'DataNoneValid', 'FieldValidationError']})
  11. ##@brief Data field designed to handle formated strings
  12. class FormatString(Varchar):
  13. help = 'Automatic string field, designed to use the str % operator to \
  14. build its content'
  15. base_type = 'char'
  16. ##@brief Build its content with a field list and a format string
  17. # @param format_string str
  18. # @param max_length int : the maximum length of the handled value
  19. # @param field_list list : List of field to use
  20. # @param **kwargs
  21. def __init__(self, format_string, field_list, **kwargs):
  22. self._field_list = field_list
  23. self._format_string = format_string
  24. super().__init__(internal='automatic',**kwargs)
  25. def _construct_data(self, emcomponent, fname, datas, cur_value):
  26. ret = self._format_string % tuple(
  27. datas[fname] for fname in self._field_list)
  28. if len(ret) > self.max_length:
  29. warnings.warn("Format field overflow. Truncating value")
  30. ret = ret[:self.max_length]
  31. return ret
  32. ##@brief Varchar validated by a regex
  33. class Regex(Varchar):
  34. help = 'String field validated with a regex. Takes two options : \
  35. max_length and regex'
  36. base_type = 'char'
  37. ##@brief A string field validated by a regex
  38. # @param regex str : a regex string (passed as argument to re.compile())
  39. # @param max_length int : the max length for this field (default : 10)
  40. # @param **kwargs
  41. def __init__(self, regex='', max_length=10, **kwargs):
  42. self.regex = regex
  43. self.compiled_re = re.compile(regex)#trigger an error if invalid regex
  44. super(self.__class__, self).__init__(max_length=max_length, **kwargs)
  45. ##@brief Check and cast value in appropriate type
  46. #@param value *
  47. #@throw FieldValidationError if value is unappropriate or can not be cast
  48. #@return value
  49. def _check_data_value(self, value):
  50. value = super()._check_data_value(value)
  51. if not self.compiled_re.match(value) or len(value) > self.max_length:
  52. msg = '"%s" doesn\'t match the regex "%s"' % (value, self.regex)
  53. raise FieldValidationError(msg)
  54. return value
  55. def can_override(self, data_handler):
  56. if not super().can_override(data_handler):
  57. return False
  58. if data_handler.max_length != self.max_length:
  59. return False
  60. return True
  61. ##@brief Handles uniq ID
  62. class UniqID(Integer):
  63. help = 'Fieldtype designed to handle editorial model UID'
  64. base_type = 'int'
  65. ##@brief A uid field
  66. # @param **kwargs
  67. def __init__(self, **kwargs):
  68. kwargs['internal'] = 'automatic'
  69. super(self.__class__, self).__init__(primary_key = True, **kwargs)
  70. def construct_data(self, emcomponent, fname, datas, cur_value):
  71. if cur_value is None:
  72. #Ask datasource to provide a new uniqID
  73. return emcomponent._ro_datasource.new_numeric_id(emcomponent)
  74. return cur_value
  75. class LeobjectSubclassIdentifier(Varchar):
  76. help = 'Datahandler designed to handle LeObject subclass identifier in DB'
  77. base_type = 'varchar'
  78. def __init__(self, **kwargs):
  79. if 'internal' in kwargs and not kwargs['internal']:
  80. raise RuntimeError(self.__class__.__name__+" datahandler can only \
  81. be internal")
  82. kwargs['internal'] = True
  83. super().__init__(**kwargs)
  84. def construct_data(self, emcomponent, fname, datas, cur_value):
  85. cls = emcomponent
  86. if not inspect.isclass(emcomponent):
  87. cls = emcomponent.__class__
  88. return cls.__name__
  89. ##@brief Data field designed to handle concatenated fields
  90. class Concat(FormatString):
  91. help = 'Automatic strings concatenation'
  92. base_type = 'char'
  93. ##@brief Build its content with a field list and a separator
  94. # @param field_list list : List of field to use
  95. # @param max_length int : the maximum length of the handled value
  96. # @param separator str
  97. # @param **kwargs
  98. def __init__(self, field_list, separator = ' ', **kwargs):
  99. format_string = separator.join(['%s' for _ in field_list])
  100. super().__init__(
  101. format_string = format_string, field_list = field_list, **kwargs)
  102. class Password(Varchar):
  103. help = 'Handle passwords'
  104. base_type = 'password'
  105. pass