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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. ## EmClass instanciation
  15. # @todo Classtype initialisation and test is not good EmClassType should give an answer or something like that
  16. # @todo defines types check for icon and sortcolumn
  17. def __init__(self, model, uid, name, classtype, icon='0', sortcolumn='rank', string=None, help_text=None, date_update=None, date_create=None, rank=None):
  18. if EmClassType.get(classtype) is None:
  19. raise AttributeError("Unknown classtype '%s'" % classtype)
  20. self.classtype = classtype
  21. self.icon = icon
  22. self.sortcolumn = sortcolumn # 'rank'
  23. 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)
  24. ## Check if the EmComponent is valid
  25. # @note this function add default and common fields to the EmClass if they are not yet created
  26. # @throw EmComponentCheckError if fails
  27. def check(self):
  28. for fname in self.default_fields_list().keys():
  29. if fname not in [f.name for f in self.fields()]:
  30. self.model.add_default_class_fields(self.uid)
  31. super(EmClass, self).check()
  32. ## @brief Return the default fields list for this EmClass
  33. # @return a dict with key = fieldname and value is a dict to pass to the EditorialModel::model::Model::creat_component() method
  34. def default_fields_list(self):
  35. ctype = EditorialModel.classtypes.EmClassType.get(self.classtype)
  36. res = ctype['default_fields']
  37. for k, v in EditorialModel.classtypes.common_fields.items():
  38. res[k] = copy.copy(v)
  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. #If the class contains EmField that are not added by default, you cannot delete the EmClass
  48. if len([f for f in self.fields() if f.name not in self.default_fields_list().keys()]) > 0:
  49. return False
  50. return True
  51. ## Retrieve list of the field_groups of this class
  52. # @return A list of fieldgroups instance
  53. def _fieldgroups(self):
  54. ret = []
  55. for fieldgroup in self.model.components(EditorialModel.fieldgroups.EmFieldGroup):
  56. if fieldgroup.class_id == self.uid:
  57. ret.append(fieldgroup)
  58. return ret
  59. ## Retrieve list of fields
  60. # @return fields [EmField]:
  61. def fields(self, relational=True):
  62. if relational:
  63. return [f for f in self.model.components('EmField') if f.class_id == self.uid]
  64. else:
  65. return [f for f in self.model.components('EmField') if f.class_id == self.uid and f.fieldtype != 'rel2type' and f.rel_field_id is None]
  66. ## Retrieve list of type of this class
  67. # @return types [EditorialModel.types.EmType]:
  68. def types(self):
  69. ret = []
  70. for emtype in self.model.components(EditorialModel.types.EmType):
  71. if emtype.class_id == self.uid:
  72. ret.append(emtype)
  73. return ret
  74. ## Add a new EditorialModel.types.EmType that can ben linked to this class
  75. # @param em_type EditorialModel.types.EmType: type to link
  76. # @return success bool: done or not
  77. # @deprecated To do this add a rel2type field to any fieldtype of this EmClass
  78. def link_type(self, em_type):
  79. pass
  80. ## Retrieve list of EditorialModel.types.EmType that are linked to this class
  81. # @return types [EditorialModel.types.EmType]:
  82. def linked_types(self):
  83. res = list()
  84. for field in self.fields():
  85. if field.fieldtype_instance().name == 'rel2type':
  86. res.append(self.model.component(field.rel_to_type_id))
  87. return res