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.py 4.3KB

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