Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

references.py 7.6KB

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