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.

model.py 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # -*- coding: utf-8 -*-
  2. ## @file editorialmodel.py
  3. # Manage instance of an editorial model
  4. from EditorialModel.migrationhandler.dummy import DummyMigrationHandler
  5. from EditorialModel.classes import EmClass
  6. from EditorialModel.fieldgroups import EmFieldGroup
  7. from EditorialModel.fields import EmField
  8. from EditorialModel.types import EmType
  9. import EditorialModel
  10. ## Manages the Editorial Model
  11. class Model(object):
  12. components_class = [EmClass, EmField, EmFieldGroup, EmType]
  13. ## Constructor
  14. #
  15. # @param backend unknown: A backend object instanciated from one of the classes in the backend module
  16. def __init__(self, backend, migration_handler = None):
  17. self.migration_handler = DummyMigrationHandler() if migration_handler is None else migration_handler
  18. self.backend = backend
  19. self._components = {'uids': {}, 'EmClass': [], 'EmType': [], 'EmField': [], 'EmFieldGroup': []}
  20. self.load()
  21. @staticmethod
  22. ## Given a name return an EmComponent child class
  23. # @param class_name str : The name to identify an EmComponent class
  24. # @return A python class or False if the class_name is not a name of an EmComponent child class
  25. def emclass_from_name(class_name):
  26. for cls in Model.components_class:
  27. if cls.__name__ == class_name:
  28. return cls
  29. return False
  30. @staticmethod
  31. ## Given a python class return a name
  32. # @param cls : The python class we want the name
  33. # @return A class name as string or False if cls is not an EmComponent child class
  34. def name_from_emclass(em_class):
  35. if em_class not in Model.components_class:
  36. return False
  37. return em_class.__name__
  38. ## Loads the structure of the Editorial Model
  39. #
  40. # Gets all the objects contained in that structure and creates a dict indexed by their uids
  41. # @todo Change the thrown exception when a components check fails
  42. # @throw ValueError When a component class don't exists
  43. def load(self):
  44. datas = self.backend.load()
  45. for uid, kwargs in datas.items():
  46. #Store and delete the EmComponent class name from datas
  47. cls_name = kwargs['component']
  48. del kwargs['component']
  49. cls = self.emclass_from_name(cls_name)
  50. if cls:
  51. kwargs['uid'] = uid
  52. # create a dict for the component and one indexed by uids, store instanciated component in it
  53. self._components['uids'][uid] = cls(self, **kwargs)
  54. self._components[cls_name].append(self._components['uids'][uid])
  55. else:
  56. raise ValueError("Unknow EmComponent class : '" + cls_name + "'")
  57. #Sorting by rank
  58. for component_class in Model.components_class:
  59. self.sort_components(component_class)
  60. #Check integrity
  61. for uid, component in self._components['uids'].items():
  62. if not component.check():
  63. raise RuntimeError("EmComponent with uid '"+str(uid)+"' is not valid !!!")
  64. ## Saves data using the current backend
  65. def save(self):
  66. return self.backend.save()
  67. ## Given a EmComponent child class return a list of instances
  68. # @param cls EmComponent : A python class
  69. # @return a list of instances or False if the class is not an EmComponent child
  70. def components(self, cls):
  71. key_name = self.name_from_emclass(cls)
  72. return False if key_name is False else self._components[key_name]
  73. ## Return an EmComponent given an uid
  74. # @param uid int : An EmComponent uid
  75. # @return The corresponding instance or False if uid don't exists
  76. def component(self, uid):
  77. return False if uid not in self._components['uids'] else self._components['uids'][uid]
  78. ## Sort components by rank in Model::_components
  79. # @param emclass pythonClass : The type of components to sort
  80. # @throw AttributeError if emclass is not valid
  81. def sort_components(self, component_class):
  82. if component_class not in self.components_class:
  83. raise AttributeError("Bad argument emclass : '"+emclass+"', excpeting one of "+str(self.components_class))
  84. self._components[self.name_from_emclass(component_class)] = sorted(self.components(component_class), key=lambda comp: comp.rank)
  85. ## Return a new uid
  86. # @return a new uid
  87. def new_uid(self):
  88. used_uid = [ int(uid) for uid in self._components['uids'].keys()]
  89. return sorted(used_uid)[-1] + 1 if len(used_uid) > 0 else 1
  90. ## Create a component from a component type and datas
  91. #
  92. # @note if datas does not contains a rank the new component will be added last
  93. # @note datas['rank'] can be an integer or two specials strings 'last' or 'first'
  94. # @param component_type str : a component type ( component_class, component_fieldgroup, component_field or component_type )
  95. # @param datas dict : the options needed by the component creation
  96. # @throw ValueError if datas['rank'] is not valid (too big or too small, not an integer nor 'last' or 'first' )
  97. def create_component(self, component_type, datas):
  98. em_obj = self.emclass_from_name(component_type)
  99. rank = 'last'
  100. if 'rank' in datas:
  101. rank = datas['rank']
  102. del datas['rank']
  103. datas['uid'] = self.new_uid()
  104. em_component = em_obj(self, **datas)
  105. self._components['uids'][em_component.uid] = em_component
  106. self._components[self.name_from_emclass(em_component.__class__)].append(em_component)
  107. em_component.rank = em_component.get_max_rank()
  108. if rank != 'last':
  109. em_component.set_rank( 1 if rank == 'first' else rank)
  110. return em_component
  111. ## Delete a component
  112. # @param uid int : Component identifier
  113. # @throw EditorialModel.components.EmComponentNotExistError
  114. # @todo unable uid check
  115. def delete_component(self, uid):
  116. if uid not in self._components[uid]:
  117. raise EditorialModel.components.EmComponentNotExistError()
  118. em_component = self._components[uid]
  119. if em_component.delete_p():
  120. self._components[self.name_from_emclass(em_component.__class__)].remove(em_component)
  121. del self._components['uids'][uid]
  122. return True
  123. ## Changes the current backend
  124. #
  125. # @param backend unknown: A backend object
  126. def set_backend(self, backend):
  127. self.backend = backend
  128. ## Returns a list of all the EmClass objects of the model
  129. def classes(self):
  130. return list(self._components[self.name_from_emclass(EmClass)])