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.

references.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # -*- coding: utf-8 -*-
  2. from lodel.context import LodelContext
  3. LodelContext.expose_modules(globals(), {
  4. 'lodel.leapi.datahandlers.base_classes': ['Reference', 'MultipleRef',
  5. 'SingleRef'],
  6. 'lodel.logger': 'logger',
  7. 'lodel.exceptions': ['LodelException', 'LodelExceptions',
  8. 'LodelFatalError', 'DataNoneValid', 'FieldValidationError']})
  9. class Link(SingleRef):
  10. pass
  11. ##@brief Child class of MultipleRef where references are represented in the form of a python list
  12. class List(MultipleRef):
  13. ##@brief instanciates a list reference
  14. # @param allowed_classes list | None : list of allowed em classes if None no restriction
  15. # @param internal bool
  16. # @param kwargs
  17. def __init__(self, max_length = None, **kwargs):
  18. super().__init__(**kwargs)
  19. @classmethod
  20. def empty(cls):
  21. return list()
  22. ##@brief Check and cast value in appropriate type
  23. #@param value *
  24. #@throw FieldValidationError if value is unappropriate or can not be cast
  25. #@return value
  26. def _check_data_value(self, value):
  27. value = super()._check_data_value(value)
  28. try:
  29. return list(value)
  30. except Exception as e:
  31. raise FieldValidationError("Given iterable is not castable in \
  32. a list : %s" % e)
  33. return value
  34. ##@brief Child class of MultipleRef where references are represented in the form of a python set
  35. class Set(MultipleRef):
  36. ##@brief instanciates a set reference
  37. # @param allowed_classes list | None : list of allowed em classes if None no restriction
  38. # @param internal bool : if False, the field is not internal
  39. # @param kwargs : Other named arguments
  40. def __init__(self, **kwargs):
  41. super().__init__(**kwargs)
  42. @classmethod
  43. def empty(cls):
  44. return set()
  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. try:
  52. return set(value)
  53. except Exception as e:
  54. raise FieldValidationError("Given iterable is not castable in \
  55. a set : %s" % e)
  56. ##@brief Child class of MultipleRef where references are represented in the form of a python dict
  57. class Map(MultipleRef):
  58. ##@brief instanciates a dict reference
  59. # @param allowed_classes list | None : list of allowed em classes if None no restriction
  60. # @param internal bool : if False, the field is not internal
  61. # @param kwargs : Other named arguments
  62. def __init__(self, **kwargs):
  63. super().__init__(**kwargs)
  64. @classmethod
  65. def empty(cls):
  66. return dict()
  67. ##@brief Check and cast value in appropriate type
  68. #@param value *
  69. #@throw FieldValidationError if value is unappropriate or can not be cast
  70. #@return value
  71. def _check_data_value(self, value):
  72. value = super()._check_data_value(value)
  73. if not isinstance(value, dict):
  74. raise FieldValidationError("Values for dict fields should be dict")
  75. return value
  76. ##@brief This Reference class is designed to handler hierarchy with some constraint
  77. class Hierarch(MultipleRef):
  78. directly_editable = False
  79. ##@brief Instanciate a data handler handling hierarchical relation with constraints
  80. # @param back_reference tuple : Here it is mandatory to have a back ref (like a parent field)
  81. # @param max_depth int | None : limit of depth
  82. # @param max_childs int | Nine : maximum number of childs by nodes
  83. def __init__(self, back_reference, max_depth = None, max_childs = None, **kwargs):
  84. super().__init__( back_reference = back_reference,
  85. max_depth = max_depth,
  86. max_childs = max_childs,
  87. **kwargs)
  88. @classmethod
  89. def empty(cls):
  90. return tuple()
  91. ##@brief Check and cast value in appropriate type
  92. #@param value *
  93. #@throw FieldValidationError if value is unappropriate or can not be cast
  94. #@return value
  95. def _check_data_value(self, value):
  96. value = super()._check_data_value(value)
  97. if not (isinstance(value, list) or isinstance(value, str)):
  98. raise FieldValidationError(
  99. "List or string expected for a set field")
  100. return value