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.

classes.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. ## @file classes.py
  3. # @see EditorialModel::classes::EmClass
  4. import EditorialModel
  5. from EditorialModel.components import EmComponent
  6. from EditorialModel.classtypes import EmClassType
  7. ## @brief Manipulate Classes of the Editorial Model
  8. # Create classes of object.
  9. # @see EmClass, EditorialModel.types.EmType, EditorialModel.fieldgroups.EmFieldGroup, EmField
  10. # @todo sortcolumn handling
  11. class EmClass(EmComponent):
  12. ranked_in = 'classtype'
  13. ## @brief default fieldgroup name
  14. default_fieldgroup = '_default'
  15. ## EmClass instanciation
  16. # @todo Classtype initialisation and test is not good EmClassType should give an answer or something like that
  17. # @todo defines types check for icon and sortcolumn
  18. def __init__(self, model, uid, name, classtype, icon='0', sortcolumn='rank', string=None, help_text=None, date_update=None, date_create=None, rank=None):
  19. if EmClassType.get(classtype) is None:
  20. raise AttributeError("Unknown classtype '%s'" % classtype)
  21. self.classtype = classtype
  22. self.icon = icon
  23. self.sortcolumn = sortcolumn # 'rank'
  24. super(EmClass, self).__init__(model=model, uid=uid, name=name, string=string, help_text=help_text, date_update=date_update, date_create=date_create, rank=rank)
  25. ## Check if the EmComponent is valid
  26. # @note this function add default and common fields to the EmClass if they are not yet created
  27. # @throw EmComponentCheckError if fails
  28. def check(self):
  29. for fname in self.default_fields_list().keys():
  30. if fname not in [f.name for f in self.fields()]:
  31. self.model.add_default_class_fields(self.uid)
  32. super(EmClass, self).check()
  33. ## @brief Return the default fields list for this EmClass
  34. # @return a dict with key = fieldname and value is a dict to pass to the EditorialModel::model::Model::creat_component() method
  35. def default_fields_list(self):
  36. ctype = EditorialModel.classtypes.EmClassType.get(self.classtype)
  37. res = ctype['default_fields']
  38. res.update(EditorialModel.classtypes.common_fields)
  39. return res
  40. ## @brief Delete a class if it's ''empty''
  41. # If a class has no fieldgroups delete it
  42. # @return bool : True if deleted False if deletion aborded
  43. def delete_check(self):
  44. for emtype in self.model.components(EditorialModel.types.EmType):
  45. if emtype.class_id == self.uid:
  46. return False
  47. for fieldgroup in self.model.components(EditorialModel.fieldgroups.EmFieldGroup):
  48. if fieldgroup.name == self.default_fieldgroup:
  49. #checking that the default fieldgroup contains only default fields
  50. for fname in [f.name for f in fieldgroup.fields()]:
  51. if fname not in EmClassType.get(self.classtype)['default_fields'].keys():
  52. return False
  53. elif fieldgroup.class_id == self.uid and fieldgroup.name != self.default_fieldgroup:
  54. return False
  55. return True
  56. ## Retrieve list of the field_groups of this class
  57. # @return A list of fieldgroups instance
  58. def fieldgroups(self):
  59. ret = []
  60. for fieldgroup in self.model.components(EditorialModel.fieldgroups.EmFieldGroup):
  61. if fieldgroup.class_id == self.uid:
  62. ret.append(fieldgroup)
  63. return ret
  64. ## Retrieve list of fields
  65. # @return fields [EmField]:
  66. def fields(self):
  67. fieldgroups = self.fieldgroups()
  68. fields = []
  69. for fieldgroup in fieldgroups:
  70. fields += fieldgroup.fields()
  71. return fields
  72. ## Retrieve list of type of this class
  73. # @return types [EditorialModel.types.EmType]:
  74. def types(self):
  75. ret = []
  76. for emtype in self.model.components(EditorialModel.types.EmType):
  77. if emtype.class_id == self.uid:
  78. ret.append(emtype)
  79. return ret
  80. ## Add a new EditorialModel.types.EmType that can ben linked to this class
  81. # @param em_type EditorialModel.types.EmType: type to link
  82. # @return success bool: done or not
  83. # @deprecated To do this add a rel2type field to any fieldtype of this EmClass
  84. def link_type(self, em_type):
  85. pass
  86. ## Retrieve list of EditorialModel.types.EmType that are linked to this class
  87. # @return types [EditorialModel.types.EmType]:
  88. def linked_types(self):
  89. res = list()
  90. for field in self.fields():
  91. if field.fieldtype_instance().name == 'rel2type':
  92. res.append(self.model.component(field.rel_to_type_id))
  93. return res