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.

references.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #
  2. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  3. #
  4. # Copyright (C) 2015-2017 Cléo UMS-3287
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. from lodel.context import LodelContext
  20. LodelContext.expose_modules(globals(), {
  21. 'lodel.leapi.datahandlers.base_classes': ['Reference', 'MultipleRef',
  22. 'SingleRef'],
  23. 'lodel.logger': 'logger',
  24. 'lodel.exceptions': ['LodelException', 'LodelExceptions',
  25. 'LodelFatalError', 'DataNoneValid',
  26. 'FieldValidationError']})
  27. ## @brief Child class of SingleRef. The object referenced must exist
  28. class Link(SingleRef):
  29. pass
  30. ## @brief Child class of MultipleRef where references are represented in the form of a python list
  31. # All the objects referenced must exist
  32. class List(MultipleRef):
  33. ## @brief instanciates a list reference
  34. # @param max_length int
  35. # @param kwargs
  36. # - allowed_classes list | None : list of allowed em classes if None no restriction
  37. # - internal bool
  38. def __init__(self, max_length=None, **kwargs):
  39. super().__init__(**kwargs)
  40. @classmethod
  41. def empty(cls):
  42. return list()
  43. ## @brief Check and cast value in appropriate type
  44. # @param value *
  45. # @throw FieldValidationError if value is unappropriate or can not be cast
  46. # @return value
  47. def _check_data_value(self, value):
  48. value = super()._check_data_value(value)
  49. try:
  50. return list(value)
  51. except Exception as e:
  52. raise FieldValidationError("Given iterable is not castable in \
  53. a list : %s" % e)
  54. ## @brief Child class of MultipleRef where references are represented in the form of a python set
  55. class Set(MultipleRef):
  56. ## @brief instanciates a set reference
  57. # @param kwargs : named arguments
  58. # - allowed_classes list | None : list of allowed em classes if None no restriction
  59. # - internal bool : if False, the field is not internal
  60. def __init__(self, **kwargs):
  61. super().__init__(**kwargs)
  62. @classmethod
  63. def empty(cls):
  64. return set()
  65. ## @brief Check and cast value in appropriate type
  66. # @param value *
  67. # @throw FieldValidationError if value is unappropriate or can not be cast
  68. # @return value
  69. def _check_data_value(self, value):
  70. value = super()._check_data_value(value)
  71. try:
  72. return set(value)
  73. except Exception as e:
  74. raise FieldValidationError("Given iterable is not castable in \
  75. a set : %s" % e)
  76. ## @brief Child class of MultipleRef where references are represented in the form of a python dict
  77. class Map(MultipleRef):
  78. ## @brief instanciates a dict reference
  79. # @param kwargs : named arguments
  80. # - allowed_classes list | None : list of allowed em classes if None no restriction
  81. # - internal bool : if False, the field is not internal
  82. def __init__(self, **kwargs):
  83. super().__init__(**kwargs)
  84. @classmethod
  85. def empty(cls):
  86. return dict()
  87. ## @brief Check and cast value in appropriate type
  88. # @param value *
  89. # @throw FieldValidationError if value is unappropriate or can not be cast
  90. # @return value
  91. def _check_data_value(self, value):
  92. value = super()._check_data_value(value)
  93. if not isinstance(value, dict):
  94. raise FieldValidationError("Values for dict fields should be dict")
  95. return value
  96. ## @brief This Reference class is designed to handler hierarchy with some constraint
  97. class Hierarch(MultipleRef):
  98. directly_editable = False
  99. ## @brief Instanciate a data handler handling hierarchical relation with constraints
  100. # @param back_reference tuple : Here it is mandatory to have a back ref (like a parent field)
  101. # @param max_depth int | None : limit of depth
  102. # @param max_childs int | Nine : maximum number of childs by nodes
  103. # @param kwargs :
  104. # - allowed_classes list | None : list of allowed em classes if None no restriction
  105. # - internal bool : if False, the field is not internal
  106. def __init__(self, back_reference, max_depth=None, max_childs=None, **kwargs):
  107. super().__init__(back_reference=back_reference,
  108. max_depth=max_depth,
  109. max_childs=max_childs,
  110. **kwargs)
  111. @classmethod
  112. def empty(cls):
  113. return tuple()
  114. ## @brief Check and cast value in appropriate type
  115. # @param value *
  116. # @throw FieldValidationError if value is unappropriate or can not be cast
  117. # @return value
  118. def _check_data_value(self, value):
  119. value = super()._check_data_value(value)
  120. if not (isinstance(value, list) or isinstance(value, str)):
  121. raise FieldValidationError(
  122. "List or string expected for a set field")
  123. return value