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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #-*- coding: utf-8 -*-
  2. from lodel.leapi.datahandlers.datas_base import *
  3. ##@brief Data field designed to handle formated strings
  4. class FormatString(Varchar):
  5. help = 'Automatic string field, designed to use the str % operator to build its content'
  6. base_type = 'char'
  7. ##@brief Build its content with a field list and a format string
  8. # @param format_string str
  9. # @param max_length int : the maximum length of the handled value
  10. # @param field_list list : List of field to use
  11. # @param **kwargs
  12. def __init__(self, format_string, field_list, max_length, **kwargs):
  13. self._field_list = field_list
  14. self._format_string = format_string
  15. super().__init__(internal='automatic', max_length=max_length)
  16. def can_override(self, data_handler):
  17. if not super().can_override(data_handler):
  18. return False
  19. if data_handler.max_length != self.max_length:
  20. return False
  21. return True
  22. ##@brief Varchar validated by a regex
  23. class Regex(Varchar):
  24. help = 'String field validated with a regex. Takes two options : max_length and regex'
  25. base_type = 'char'
  26. ##@brief A string field validated by a regex
  27. # @param regex str : a regex string (passed as argument to re.compile())
  28. # @param max_length int : the max length for this field (default : 10)
  29. # @param **kwargs
  30. def __init__(self, regex='', max_length=10, **kwargs):
  31. self.regex = regex
  32. self.compiled_re = re.compile(regex) # trigger an error if invalid regex
  33. super(self.__class__, self).__init__(max_length=max_length, **kwargs)
  34. def _check_data_value(self, value):
  35. error = None
  36. if not self.compiled_re.match(value):
  37. value = ''
  38. error = TypeError('"%s" doesn\'t match the regex "%s"' % (value, self.regex))
  39. return value, error
  40. def can_override(self, data_handler):
  41. if not super().can_override(data_handler):
  42. return False
  43. if data_handler.max_length != self.max_length:
  44. return False
  45. return True
  46. ##@brief Handles uniq ID
  47. class UniqID(Integer):
  48. help = 'Fieldtype designed to handle editorial model UID'
  49. base_type = 'int'
  50. ##@brief A uid field
  51. # @param **kwargs
  52. def __init__(self, **kwargs):
  53. kwargs['internal'] = 'automatic'
  54. super(self.__class__, self).__init__(primary_key = True, **kwargs)
  55. def _check_data_value(self, value):
  56. return value, None