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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # -*- coding: utf-8 -*-
  2. from lodel.leapi.datahandlers.base_classes import Reference, MultipleRef, SingleRef, FieldValidationError
  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 value
  15. # @param value *
  16. # @return tuple(value, exception)
  17. def _check_data_value(self, value):
  18. if isinstance(value, list) or isinstance(value, str):
  19. val, expt = super()._check_data_value(value)
  20. else:
  21. return None, FieldValidationError("List or string expected for a list field")
  22. #if not isinstance(expt, Exception):
  23. # val = list(val)
  24. return val, expt
  25. def construct_data(self, emcomponent, fname, datas, cur_value):
  26. if cur_value == 'None' or cur_value is None or cur_value == '':
  27. return None
  28. emcomponent_fields = emcomponent.fields()
  29. data_handler = None
  30. if fname in emcomponent_fields:
  31. data_handler = emcomponent_fields[fname]
  32. u_fname = emcomponent.uid_fieldname()
  33. uidtype = emcomponent.field(u_fname[0]) if isinstance(u_fname, list) else emcomponent.field(u_fname)
  34. if isinstance(cur_value, str):
  35. value = cur_value.split(',')
  36. l_value = [int(uid) for uid in value] ## à remplacer par uidtype
  37. logger.debug(l_value)
  38. return l_value
  39. elif isinstance(cur_value, list):
  40. type_list = str if isinstance(cur_value[0], str) else uidtype
  41. l_value = list()
  42. for value in cur_value:
  43. if isinstance(value,uidtype):
  44. l_value.append(value)
  45. else:
  46. raise ValueError("The items must be of the same type, string or %s" % (ecomponent.__name__))
  47. return l_value
  48. else:
  49. return None
  50. ##@brief Child class of MultipleRef where references are represented in the form of a python set
  51. class Set(MultipleRef):
  52. ##@brief instanciates a set reference
  53. # @param allowed_classes list | None : list of allowed em classes if None no restriction
  54. # @param internal bool : if False, the field is not internal
  55. # @param kwargs : Other named arguments
  56. def __init__(self, **kwargs):
  57. super().__init__(**kwargs)
  58. ##@brief Check value
  59. # @param value *
  60. # @return tuple(value, exception)
  61. def _check_data_value(self, value):
  62. if isinstance(value, set) or isinstance(value, str):
  63. val, expt = super()._check_data_value(value)
  64. else:
  65. return None, FieldValidationError("Set or string expected for a set field")
  66. return val, expt
  67. def construct_data(self, emcomponent, fname, datas, cur_value):
  68. if cur_value == 'None' or cur_value is None or cur_value == '':
  69. return None
  70. emcomponent_fields = emcomponent.fields()
  71. data_handler = None
  72. if fname in emcomponent_fields:
  73. data_handler = emcomponent_fields[fname]
  74. u_fname = emcomponent.uid_fieldname()
  75. uidtype = emcomponent.field(u_fname[0]) if isinstance(u_fname, list) else emcomponent.field(u_fname)
  76. if isinstance(cur_value, str):
  77. value = cur_value.split(',')
  78. l_value = [int(uid) for uid in value] ## à remplacer par uidtype
  79. return list(l_value)
  80. elif isinstance(cur_value, set):
  81. type_list = str if isinstance(cur_value[0], str) else uidtype
  82. l_value = list()
  83. for value in cur_value:
  84. if isinstance(value,uidtype):
  85. l_value.append(value)
  86. else:
  87. raise ValueError("The items must be of the same type, string or %s" % (ecomponent.__name__))
  88. return l_value
  89. logger.debug(l_value)
  90. else:
  91. return None
  92. ##@brief Child class of MultipleRef where references are represented in the form of a python dict
  93. class Map(MultipleRef):
  94. ##@brief instanciates a dict reference
  95. # @param allowed_classes list | None : list of allowed em classes if None no restriction
  96. # @param internal bool : if False, the field is not internal
  97. # @param kwargs : Other named arguments
  98. def __init__(self, **kwargs):
  99. super().__init__(**kwargs)
  100. ##@brief Check value
  101. # @param value *
  102. # @return tuple(value, exception)
  103. def _check_data_value(self, value):
  104. val, expt = super()._check_data_value(value)
  105. if not isinstance(value, dict):
  106. return None, FieldValidationError("Values for dict fields should be dict")
  107. return (
  108. None if isinstance(expt, Exception) else value,
  109. expt)
  110. ##@brief This Reference class is designed to handler hierarchy with some constraint
  111. class Hierarch(MultipleRef):
  112. directly_editable = False
  113. ##@brief Instanciate a data handler handling hierarchical relation with constraints
  114. # @param back_reference tuple : Here it is mandatory to have a back ref (like a parent field)
  115. # @param max_depth int | None : limit of depth
  116. # @param max_childs int | Nine : maximum number of childs by nodes
  117. def __init__(self, back_reference, max_depth = None, max_childs = None, **kwargs):
  118. super().__init__( back_reference = back_reference,
  119. max_depth = max_depth,
  120. max_childs = max_childs,
  121. **kwargs)
  122. def _check_data_value(self, value):
  123. if isinstance(value, list) or isinstance(value, str):
  124. val, expt = super()._check_data_value(value)
  125. else:
  126. return None, FieldValidationError("Set or string expected for a set field")
  127. return val, expt
  128. def construct_data(self, emcomponent, fname, datas, cur_value):
  129. if cur_value == 'None' or cur_value is None or cur_value == '':
  130. return None
  131. emcomponent_fields = emcomponent.fields()
  132. data_handler = None
  133. if fname in emcomponent_fields:
  134. data_handler = emcomponent_fields[fname]
  135. u_fname = emcomponent.uid_fieldname()
  136. uidtype = emcomponent.field(u_fname[0]) if isinstance(u_fname, list) else emcomponent.field(u_fname)
  137. if isinstance(cur_value, str):
  138. value = cur_value.split(',')
  139. l_value = [int(uid) for uid in value] ## à remplacer par uidtype
  140. return list(l_value)
  141. elif isinstance(cur_value, list):
  142. type_list = str if isinstance(cur_value[0], str) else uidtype
  143. l_value = list()
  144. for value in cur_value:
  145. if isinstance(value,uidtype):
  146. l_value.append(value)
  147. else:
  148. raise ValueError("The items must be of the same type, string or %s" % (ecomponent.__name__))
  149. return l_value
  150. else:
  151. return None