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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. ## Check if the EmFieldGroup is valid
  18. # @throw EmComponentCheckError if fails
  19. def check(self):
  20. super(EmFieldGroup, self).check()
  21. em_class = self.model.component(self.class_id)
  22. if not em_class:
  23. raise EmComponentCheckError("class_id contains a non existing uid '%s'" % str(self.class_id))
  24. if not isinstance(em_class, EmClass):
  25. raise EmComponentCheckError("class_id cointains an uid from a component that is not an EmClass but an %s" % type(em_class))
  26. ## Deletes a fieldgroup
  27. # @return True if the deletion is possible, False if not
  28. def delete_check(self):
  29. # all the EmField objects contained in this fieldgroup should be deleted first
  30. fieldgroup_fields = self.fields()
  31. if len(fieldgroup_fields) > 0:
  32. raise NotEmptyError("This Fieldgroup still contains fields. It can't be deleted then")
  33. return True
  34. ## Get the list of associated fields
  35. # if type_id, the fields will be filtered to represent selected fields of this EmType
  36. # @return A list of EmField instance
  37. def fields(self, type_id=0):
  38. if not type_id:
  39. fields = [field for field in self.model.components(EmField) if field.fieldgroup_id == self.uid]
  40. else:
  41. # for an EmType, fields have to be filtered
  42. em_type = self.model.component(type_id)
  43. fields = []
  44. for field in self.model.components(EmField):
  45. if field.fieldgroup_id != self.uid or (field.optional and field.uid not in em_type.fields_list):
  46. continue
  47. # don't include relational field if parent should not be included
  48. if field.rel_field_id:
  49. parent = self.model.component(field.rel_field_id)
  50. if parent.optional and parent.uid not in em_type.fields_list:
  51. continue
  52. fields.append(field)
  53. return fields
  54. class NotEmptyError(Exception):
  55. pass