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.

reference.py 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # -*- coding: utf-8 -*-
  2. from lodel.leapi.datahandlers.field_data_handler import FieldDataHandler
  3. from lodel.editorial_model.components import EmClass
  4. class Reference(FieldDataHandler):
  5. ## @brief Instanciation
  6. # @param allowed_classes list | None : list of allowed em classes if None no restriction
  7. # @param internal bool : if False, the field is not internal
  8. # @param **kwargs : other arguments
  9. def __init__(self, allowed_classes = None, internal=False, **kwargs):
  10. self.allowed_classes = None if allowed_classes is None else set(allowed_classes)
  11. super().__init__(internal=internal, **kwargs)
  12. ## @brief gets the target of the reference
  13. def get_relateds(self):
  14. return self._refs
  15. ## @brief checks if the data value is valid
  16. # @param value
  17. # @return
  18. def _check_data_value(self, value):
  19. if not isinstance(value, self._refs_class):
  20. return value, "The reference should be an instance of %s, %s gotten" % (self._refs_class, value.__class__)
  21. if isinstance(value, EmClass):
  22. value = [value]
  23. if isinstance(value, dict):
  24. ref_values = value.values()
  25. for related in value:
  26. if not isinstance(related, EmClass):
  27. return value, "The reference %s should be an instance of EmClass, %s gotten" % (related.display_name,
  28. related.__class__)
  29. if self.allowed_classes is not None and related.__class__.display_name not in self.allowed_classes:
  30. return value, "The reference %s should be an instance of either one of those classes : %s, %s gotten" % \
  31. (related.display_name, self.allowed_classes, related.__class__)
  32. return value, None