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

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