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

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