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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- coding: utf-8 -*-
  2. ## @file editorialmodel.py
  3. # Manage instance of an editorial model
  4. from EditorialModel.classes import EmClass
  5. from EditorialModel.fieldgroups import EmFieldGroup
  6. from EditorialModel.fields import EmField
  7. from EditorialModel.types import EmType
  8. ## Manages the Editorial Model
  9. class Model(object):
  10. componentClass = EmClass
  11. componentFieldGroup = EmFieldGroup
  12. componentField = EmField
  13. componentType = EmType
  14. ## Constructor
  15. #
  16. # @param backend unknown: A backend object instanciated from one of the classes in the backend module
  17. def __init__(self, backend):
  18. self.backend = backend
  19. self.uids = {}
  20. self.Class = {}
  21. self.FieldGroup = {}
  22. self.Field = {}
  23. self.Type = {}
  24. self.load()
  25. ## Loads the structure of the Editorial Model
  26. #
  27. # Gets all the objects contained in that structure and creates a dict indexed by their uids
  28. def load(self):
  29. data = self.backend.load()
  30. for uid, component in data.items():
  31. cls_name = 'component' + component['component']
  32. cls = getattr(Model, cls_name)
  33. if cls:
  34. component['uid'] = uid
  35. self.uids[uid] = cls(component)
  36. # create a dict for each component
  37. getattr(self, component['component'])[uid] = self.uids[uid]
  38. # TODO
  39. # iterate over classes, link to subordinates types
  40. # iterate over types, attach them to classes
  41. # iterate over fieldgroups, attach them to classes
  42. # iterate over fields, attach them to fieldgroups, link to types, link to relational fields
  43. ## Saves data using the current backend
  44. def save(self):
  45. return self.backend.save()
  46. ## Changes the current backend
  47. #
  48. # @param backend unknown: A backend object
  49. def set_backend(self, backend):
  50. self.backend = backend
  51. ## Returns a list of all the EmClass objects of the model
  52. def classes(self):
  53. return list(self.Class.values())