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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. ## @file classes.py
  3. # @see EditorialModel::classes::EmClass
  4. from EditorialModel.components import EmComponent
  5. import EditorialModel.fieldtypes as ftypes
  6. import EditorialModel
  7. ## @brief Manipulate Classes of the Editorial Model
  8. # Create classes of object.
  9. # @see EmClass, EmType, EmFieldGroup, EmField
  10. # @todo sortcolumn handling
  11. class EmClass(EmComponent):
  12. table = 'em_class'
  13. ranked_in = 'classtype'
  14. ## @brief Specific EmClass fields
  15. # @see EditorialModel::components::EmComponent::_fields
  16. _fields = [
  17. ('classtype', ftypes.EmField_char),
  18. ('icon', ftypes.EmField_icon),
  19. ('sortcolumn', ftypes.EmField_char)
  20. ]
  21. ## Create a new class
  22. # @param name str: name of the new class
  23. # @param class_type EmClasstype: type of the class
  24. # @return an EmClass instance
  25. # @throw EmComponentExistError if an EmClass with this name and a different classtype exists
  26. @classmethod
  27. def create(cls, name, classtype, icon=None, sortcolumn='rank', **em_component_args):
  28. result = super(EmClass, cls).create(name=name, classtype=classtype, icon=icon, sortcolumn=sortcolumn, **em_component_args)
  29. return result
  30. @property
  31. ## @brief Return the table name used to stores data on this class
  32. def class_table_name(self):
  33. return self.name
  34. ## @brief Delete a class if it's ''empty''
  35. # If a class has no fieldgroups delete it
  36. # @return bool : True if deleted False if deletion aborded
  37. def delete(self):
  38. for uid, emtype in self.components[Model.name_from_emclass(EmType)].items:
  39. if emtype.class_id == self.uid:
  40. return False
  41. for uid, fieldgroup in self.components[Model.name_from_emclass(EmFieldGroup)].items():
  42. if fieldgroup.class_id == self.uid:
  43. return False
  44. return super(EmClass, self).delete()
  45. ## Retrieve list of the field_groups of this class
  46. # @return A list of fieldgroups instance
  47. def fieldgroups(self):
  48. ret = []
  49. for uid,fieldgroup in self.components[Model.name_from_emclass(EmFieldGroup)].items():
  50. if fieldgroup.class_id == self.uid:
  51. ret.append(fieldgroup)
  52. return ret
  53. ## Retrieve list of fields
  54. # @return fields [EmField]:
  55. def fields(self):
  56. fieldgroups = self.fieldgroups()
  57. fields = []
  58. for fieldgroup in fieldgroups:
  59. fields += fieldgroup.fields()
  60. return fields
  61. ## Retrieve list of type of this class
  62. # @return types [EmType]:
  63. def types(self):
  64. ret = []
  65. for uid, emtype in self.components[Model.name_from_emclass(EmType)].items:
  66. if emtype.class_id == self.uid:
  67. ret.append(emtype)
  68. return ret
  69. ## Add a new EmType that can ben linked to this class
  70. # @param em_type EmType: type to link
  71. # @return success bool: done or not
  72. def link_type(self, em_type):
  73. pass
  74. ## Retrieve list of EmType that are linked to this class
  75. # @return types [EmType]:
  76. def linked_types(self):
  77. pass