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.

fieldgroups.py 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #-*- coding: utf-8 -*-
  2. from EditorialModel.components import EmComponent
  3. from EditorialModel.fields import EmField
  4. from EditorialModel.classes import EmClass
  5. from EditorialModel.exceptions import EmComponentCheckError
  6. ## Represents groups of EmField associated with an EmClass
  7. #
  8. # EmClass fields representation is organised with EmFieldGroup
  9. # @see EditorialModel::fields::EmField EditorialModel::classes::EmClass
  10. class EmFieldGroup(EmComponent):
  11. ranked_in = 'class_id'
  12. ## EmFieldGroup instanciation
  13. def __init__(self, model, uid, name, class_id, string=None, help_text=None, date_update=None, date_create=None, rank=None):
  14. self.class_id = class_id
  15. self.check_type('class_id', int)
  16. super(EmFieldGroup, self).__init__(model=model, uid=uid, name=name, string=string, help_text=help_text, date_update=date_update, date_create=date_create, rank=rank)
  17. @property
  18. def em_class(self):
  19. return self.model.component(self.class_id)
  20. ## Check if the EmFieldGroup is valid
  21. # @throw EmComponentCheckError if fails
  22. def check(self):
  23. super(EmFieldGroup, self).check()
  24. em_class = self.model.component(self.class_id)
  25. if not em_class:
  26. raise EmComponentCheckError("class_id contains a non existing uid '%s'" % str(self.class_id))
  27. if not isinstance(em_class, EmClass):
  28. raise EmComponentCheckError("class_id cointains an uid from a component that is not an EmClass but an %s" % type(em_class))
  29. ## Deletes a fieldgroup
  30. # @return True if the deletion is possible, False if not
  31. def delete_check(self):
  32. # all the EmField objects contained in this fieldgroup should be deleted first
  33. fieldgroup_fields = self.fields()
  34. if len(fieldgroup_fields) > 0:
  35. raise NotEmptyError("This Fieldgroup still contains fields. It can't be deleted then")
  36. return True
  37. ## Get the list of associated fields
  38. # if type_id, the fields will be filtered to represent selected fields of this EmType
  39. # @return A list of EmField instance
  40. def fields(self, type_id=0):
  41. if not type_id:
  42. fields = [field for field in self.model.components(EmField) if field.fieldgroup_id == self.uid]
  43. else:
  44. # for an EmType, fields have to be filtered
  45. em_type = self.model.component(type_id)
  46. fields = []
  47. for field in self.model.components(EmField):
  48. if field.fieldgroup_id != self.uid or (field.optional and field.uid not in em_type.fields_list):
  49. continue
  50. # don't include relational field if parent should not be included
  51. if field.rel_field_id:
  52. parent = self.model.component(field.rel_field_id)
  53. if parent.optional and parent.uid not in em_type.fields_list:
  54. continue
  55. fields.append(field)
  56. return fields
  57. class NotEmptyError(Exception):
  58. pass