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 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. # @param type_id int|None : if not None the fields will be filtered to represent selected fields of the EmType identified by this uid
  40. # @param relational bool : If False don't returns the relational fields
  41. # @return A list of EmField instance
  42. def fields(self, type_id=None, relational=True):
  43. if type_id is None:
  44. fields = [field for field in self.model.components(EmField) if field.fieldgroup_id == self.uid]
  45. else:
  46. # for an EmType, fields have to be filtered
  47. em_type = self.model.component(type_id)
  48. fields = []
  49. for field in self.model.components(EmField):
  50. if field.fieldgroup_id != self.uid or (field.optional and field.uid not in em_type.fields_list):
  51. continue
  52. # don't include relational field if parent should not be included
  53. if field.rel_field_id:
  54. parent = self.model.component(field.rel_field_id)
  55. if parent.optional and parent.uid not in em_type.fields_list:
  56. continue
  57. fields.append(field)
  58. if not relational:
  59. fields = [ f for f in fields if f.rel_field_id is None and f.fieldtype != 'rel2type' ]
  60. return fields
  61. class NotEmptyError(Exception):
  62. pass