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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- coding: utf-8 -*-
  2. from lodel.leapi.datahandlers.base_classes import Reference, MultipleRef, SingleRef
  3. class Link(SingleRef): pass
  4. ##@brief Child class of MultipleRef where references are represented in the form of a python list
  5. class List(MultipleRef):
  6. ##@brief instanciates a list reference
  7. # @param allowed_classes list | None : list of allowed em classes if None no restriction
  8. # @param internal bool
  9. # @param kwargs
  10. def __init__(self, max_length = None, **kwargs):
  11. super().__init__(**kwargs)
  12. ##@brief Check value
  13. # @param value *
  14. # @return tuple(value, exception)
  15. def _check_data_value(self, value):
  16. val, expt = super()._check_data_value()
  17. if not isinstance(expt, Exception):
  18. val = list(val)
  19. val, expt = super()._check_data_value(value.values())
  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()
  34. if not isinstance(expt, Exception):
  35. val = set(val)
  36. val, expt = super()._check_data_value(value.values())
  37. return val, expt
  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 value
  47. # @param value *
  48. # @return tuple(value, exception)
  49. def _check_data_value(self, value):
  50. if not isinstance(value, dict):
  51. return None, FieldValidationError("Values for dict fields should be dict")
  52. val, expt = super()._check_data_value(value.values())
  53. return (
  54. None if isinstance(expt, Exception) else value,
  55. expt)
  56. ##@brief This Reference class is designed to handler hierarchy with some constraint
  57. class Hierarch(MultipleRef):
  58. ##@brief Instanciate a data handler handling hierarchical relation with constraints
  59. # @param back_reference tuple : Here it is mandatory to have a back ref (like a parent field)
  60. # @param max_depth int | None : limit of depth
  61. # @param max_childs int | Nine : maximum number of childs by nodes
  62. def __init__(self, back_reference, max_depth = None, max_childs = None, **kwargs):
  63. super().__init__(back_reference = back_reference, max_depth = max_depth, )
  64. def _check_data_value(self, value):
  65. value, expt = super()._check_data_value(value)
  66. if isinstance(expt, Exception):
  67. return None, expt
  68. # determine depth with datasource