説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

datas.py 4.1KB

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