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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- coding: utf-8 -*-
  2. from lodel.leapi.datahandlers.base_classes import Reference, MultipleRef, SingleRef, FieldValidationError
  3. class Link(SingleRef):
  4. pass
  5. ##@brief Child class of MultipleRef where references are represented in the form of a python list
  6. class List(MultipleRef):
  7. ##@brief instanciates a list reference
  8. # @param allowed_classes list | None : list of allowed em classes if None no restriction
  9. # @param internal bool
  10. # @param kwargs
  11. def __init__(self, max_length = None, **kwargs):
  12. super().__init__(**kwargs)
  13. ##@brief Check value
  14. # @param value *
  15. # @return tuple(value, exception)
  16. def _check_data_value(self, value):
  17. val, expt = super()._check_data_value(value)
  18. if not isinstance(expt, Exception):
  19. val = list(val)
  20. return val, expt
  21. ##@brief Child class of MultipleRef where references are represented in the form of a python set
  22. class Set(MultipleRef):
  23. ##@brief instanciates a set reference
  24. # @param allowed_classes list | None : list of allowed em classes if None no restriction
  25. # @param internal bool : if False, the field is not internal
  26. # @param kwargs : Other named arguments
  27. def __init__(self, **kwargs):
  28. super().__init__(**kwargs)
  29. ##@brief Check value
  30. # @param value *
  31. # @return tuple(value, exception)
  32. def _check_data_value(self, value):
  33. val, expt = super()._check_data_value(value)
  34. if not isinstance(expt, Exception):
  35. val = tuple(set(val))
  36. return val, expt
  37. ##@brief Child class of MultipleRef where references are represented in the form of a python dict
  38. class Map(MultipleRef):
  39. ##@brief instanciates a dict reference
  40. # @param allowed_classes list | None : list of allowed em classes if None no restriction
  41. # @param internal bool : if False, the field is not internal
  42. # @param kwargs : Other named arguments
  43. def __init__(self, **kwargs):
  44. super().__init__(**kwargs)
  45. ##@brief Check value
  46. # @param value *
  47. # @return tuple(value, exception)
  48. def _check_data_value(self, value):
  49. val, expt = super()._check_data_value(value)
  50. if not isinstance(value, dict):
  51. return None, FieldValidationError("Values for dict fields should be dict")
  52. return (
  53. None if isinstance(expt, Exception) else value,
  54. expt)
  55. ##@brief This Reference class is designed to handler hierarchy with some constraint
  56. class Hierarch(MultipleRef):
  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. def _check_data_value(self, value):
  67. value, expt = super()._check_data_value(value)
  68. if isinstance(expt, Exception):
  69. return None, expt
  70. # determine depth with datasource