Browse Source

EM: Model: store all components in one dict

ArnAud 9 years ago
parent
commit
6f2d895462
2 changed files with 26 additions and 35 deletions
  1. 10
    19
      EditorialModel/model.py
  2. 16
    16
      EditorialModel/test/me.json

+ 10
- 19
EditorialModel/model.py View File

@@ -13,21 +13,17 @@ from EditorialModel.types import EmType
13 13
 ## Manages the Editorial Model
14 14
 class Model(object):
15 15
 
16
-    componentClass = EmClass
17
-    componentFieldGroup = EmFieldGroup
18
-    componentField = EmField
19
-    componentType = EmType
16
+    component_class = EmClass
17
+    component_fieldgroup = EmFieldGroup
18
+    component_field = EmField
19
+    component_type = EmType
20 20
 
21 21
     ## Constructor
22 22
     #
23 23
     # @param backend unknown: A backend object instanciated from one of the classes in the backend module
24 24
     def __init__(self, backend):
25 25
         self.backend = backend
26
-        self.uids = {}
27
-        self.Class = {}
28
-        self.FieldGroup = {}
29
-        self.Field = {}
30
-        self.Type = {}
26
+        self.components = {'uids':{}, 'class':{}, 'type':{}, 'field':{}, 'fieldgroup':{}}
31 27
         self.load()
32 28
 
33 29
     ## Loads the structure of the Editorial Model
@@ -36,18 +32,13 @@ class Model(object):
36 32
     def load(self):
37 33
         data = self.backend.load()
38 34
         for uid, component in data.items():
39
-            cls_name = 'component' + component['component']
35
+            cls_name = 'component_' + component['component']
40 36
             cls = getattr(Model, cls_name)
41 37
             if cls:
42 38
                 component['uid'] = uid
43
-                self.uids[uid] = cls(component)
44
-                # create a dict for each component
45
-                getattr(self, component['component'])[uid] = self.uids[uid]
46
-        # TODO
47
-        # iterate over classes, link to subordinates types
48
-        # iterate over types, attach them to classes
49
-        # iterate over fieldgroups, attach them to classes
50
-        # iterate over fields, attach them to fieldgroups, link to types, link to relational fields
39
+                # create a dict for the component and one indexed by uids, store instanciated component in it
40
+                self.components[component['component']][uid] = self.components['uids'][uid] = cls(component)
41
+        # TODO : check integrity
51 42
 
52 43
     ## Saves data using the current backend
53 44
     def save(self):
@@ -61,4 +52,4 @@ class Model(object):
61 52
 
62 53
     ## Returns a list of all the EmClass objects of the model
63 54
     def classes(self):
64
-        return list(self.Class.values())
55
+        return list(self.components['class'].values())

+ 16
- 16
EditorialModel/test/me.json View File

@@ -1,50 +1,50 @@
1 1
 {
2 2
   "1" : {
3
-    "component":"Class", "name":"textes", "string":"{\"fre\":\"Texte\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "classtype":"entity", "icon":"0", "sortcolumn":"rank"
3
+    "component":"class", "name":"textes", "string":"{\"fre\":\"Texte\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "classtype":"entity", "icon":"0", "sortcolumn":"rank"
4 4
   },
5 5
   "2" : {
6
-    "component":"Class", "name":"personnes", "string":"{\"fre\":\"Personnes\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "classtype":"person", "icon":"0", "sortcolumn":"rank"
6
+    "component":"class", "name":"personnes", "string":"{\"fre\":\"Personnes\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "classtype":"person", "icon":"0", "sortcolumn":"rank"
7 7
   },
8 8
   "3" : {
9
-    "component":"FieldGroup", "name":"info", "string":"{\"fre\":\"Info\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "class_id":"1"
9
+    "component":"fieldgroup", "name":"info", "string":"{\"fre\":\"Info\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "class_id":"1"
10 10
   },
11 11
   "4" : {
12
-    "component":"Field", "name":"titre", "string":"{\"fre\":\"Titre\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"3", "rel_to_type_id":"", "rel_field_id":"", "optional":"0", "internal":"", "icon":"0"
12
+    "component":"field", "name":"titre", "string":"{\"fre\":\"Titre\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"3", "rel_to_type_id":"", "rel_field_id":"", "optional":"0", "internal":"", "icon":"0"
13 13
   },
14 14
   "5" : {
15
-    "component":"Type", "name":"article", "string":"{\"fre\":\"Article\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "class_id":"1", "icon":"0", "sortcolumn":"rank", "fields":[7]
15
+    "component":"type", "name":"article", "string":"{\"fre\":\"Article\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "class_id":"1", "icon":"0", "sortcolumn":"rank", "fields":[7]
16 16
   },
17 17
   "6" : {
18
-    "component":"Type", "name":"personne", "string":"{\"fre\":\"Personne\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "class_id":"2", "icon":"0", "sortcolumn":"rank", "fields":[10]
18
+    "component":"type", "name":"personne", "string":"{\"fre\":\"Personne\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "class_id":"2", "icon":"0", "sortcolumn":"rank", "fields":[10]
19 19
   },
20 20
   "7" : {
21
-    "component":"Field", "name":"soustitre", "string":"{\"fre\":\"Sous-titre\"}", "help":"{}", "rank":"2", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"3", "rel_to_type_id":"", "rel_field_id":"", "optional":"1", "internal":"", "icon":"0"
21
+    "component":"field", "name":"soustitre", "string":"{\"fre\":\"Sous-titre\"}", "help":"{}", "rank":"2", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"3", "rel_to_type_id":"", "rel_field_id":"", "optional":"1", "internal":"", "icon":"0"
22 22
   },
23 23
   "8" : {
24
-    "component":"FieldGroup", "name":"civilité", "string":"{\"fre\":\"Civilité\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "class_id":"2"
24
+    "component":"fieldgroup", "name":"civilité", "string":"{\"fre\":\"Civilité\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "class_id":"2"
25 25
   },
26 26
   "9" : {
27
-    "component":"Field", "name":"nom", "string":"{\"fre\":\"Nom\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"8", "rel_to_type_id":"", "rel_field_id":"", "optional":"0", "internal":"", "icon":"0"
27
+    "component":"field", "name":"nom", "string":"{\"fre\":\"Nom\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"8", "rel_to_type_id":"", "rel_field_id":"", "optional":"0", "internal":"", "icon":"0"
28 28
   },
29 29
   "10" : {
30
-    "component":"Field", "name":"prenom", "string":"{\"fre\":\"Preom\"}", "help":"{}", "rank":"2", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"8", "rel_to_type_id":"", "rel_field_id":"", "optional":"1", "internal":"", "icon":"0"
30
+    "component":"field", "name":"prenom", "string":"{\"fre\":\"Preom\"}", "help":"{}", "rank":"2", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"8", "rel_to_type_id":"", "rel_field_id":"", "optional":"1", "internal":"", "icon":"0"
31 31
   },
32 32
   "11" : {
33
-    "component":"Field", "name":"auteur", "string":"{\"fre\":\"Auteur\"}", "help":"{}", "rank":"3", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"3", "rel_to_type_id":"6", "rel_field_id":"", "optional":"1", "internal":"", "icon":"0"
33
+    "component":"field", "name":"auteur", "string":"{\"fre\":\"Auteur\"}", "help":"{}", "rank":"3", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"3", "rel_to_type_id":"6", "rel_field_id":"", "optional":"1", "internal":"", "icon":"0"
34 34
   },
35 35
   "12" : {
36
-    "component":"Field", "name":"adresse", "string":"{\"fre\":\"Adresse\"}", "help":"{}", "rank":"4", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"3", "rel_to_type_id":"", "rel_field_id":"11", "optional":"1", "internal":"", "icon":"0"
36
+    "component":"field", "name":"adresse", "string":"{\"fre\":\"Adresse\"}", "help":"{}", "rank":"4", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"3", "rel_to_type_id":"", "rel_field_id":"11", "optional":"1", "internal":"", "icon":"0"
37 37
   },
38 38
   "13" : {
39
-    "component":"Class", "name":"publication", "string":"{\"fre\":\"Publication\"}", "help":"{}", "rank":"2", "date_update":"", "date_create":"", "classtype":"entity", "icon":"0", "sortcolumn":"rank"
39
+    "component":"class", "name":"publication", "string":"{\"fre\":\"Publication\"}", "help":"{}", "rank":"2", "date_update":"", "date_create":"", "classtype":"entity", "icon":"0", "sortcolumn":"rank"
40 40
   },
41 41
   "14" : {
42
-    "component":"Type", "name":"rubrique", "string":"{\"fre\":\"Rubrique\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "class_id":"13", "icon":"0", "sortcolumn":"rank", "subordinates":[["parent",5], ["parent",14]]
42
+    "component":"type", "name":"rubrique", "string":"{\"fre\":\"Rubrique\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "class_id":"13", "icon":"0", "sortcolumn":"rank", "subordinates":[["parent",5], ["parent",14]]
43 43
   },
44 44
   "15" : {
45
-    "component":"FieldGroup", "name":"info", "string":"{\"fre\":\"Info\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "class_id":"13"
45
+    "component":"fieldgroup", "name":"info", "string":"{\"fre\":\"Info\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "class_id":"13"
46 46
   },
47 47
   "16" : {
48
-    "component":"Field", "name":"titre", "string":"{\"fre\":\"Titre\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"15", "rel_to_type_id":"", "rel_field_id":"", "optional":"0", "internal":"", "icon":"0"
48
+    "component":"field", "name":"titre", "string":"{\"fre\":\"Titre\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "fieldtype":"", "fieldgroup_id":"15", "rel_to_type_id":"", "rel_field_id":"", "optional":"0", "internal":"", "icon":"0"
49 49
   }
50 50
 }

Loading…
Cancel
Save