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

12345678910111213141516171819202122232425262728293031323334353637
  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 target is valid
  16. def check_data_value(self, value):
  17. if not isinstance(value, self._refs_class):
  18. return (value, "The reference should be an instance of %s, %s gotten" % (self._refs_class, value.__class__))
  19. if isinstance(value, EmClass):
  20. value = [value]
  21. if isinstance(value, dict):
  22. ref_values = value.values()
  23. for related in value:
  24. if not isinstance(related, EmClass):
  25. return (value, "The reference %s should be an instance of EmClass, %s gotten" % (related.display_name, related.__class__))
  26. return (value, None)