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

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