Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

datas.py 4.0KB

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