Browse Source

EM: first draft for a Model Class

ArnAud 9 years ago
parent
commit
c73251795c

+ 0
- 0
EditorialModel/backend/__init__.py View File


+ 18
- 0
EditorialModel/backend/json.py View File

@@ -0,0 +1,18 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+## @file json.py
4
+# Load representation of an EditorialModel from a json file
5
+
6
+import json
7
+
8
+class EmBackendJson(object):
9
+
10
+    def __init__(self, json_file):
11
+        json_data = open(json_file).read()
12
+        self.data = json.loads(json_data)
13
+
14
+    def load(self):
15
+        data = {}
16
+        for index, component in self.data.items():
17
+            data[int(index)] = component
18
+        return data

+ 5
- 9
EditorialModel/components.py View File

@@ -45,7 +45,7 @@ class EmComponent(object):
45 45
     # @param id_or_name int|str: name or id of the object
46 46
     # @throw TypeError if id_or_name is not an integer nor a string
47 47
     # @throw NotImplementedError if called with EmComponent
48
-    def __init__(self, id_or_name):
48
+    def __init__(self, data):
49 49
         if type(self) == EmComponent:
50 50
             raise NotImplementedError('Abstract class')
51 51
 
@@ -55,14 +55,10 @@ class EmComponent(object):
55 55
         # @see EmComponent::_fields EditorialModel::fieldtypes::EmFieldType
56 56
         self._fields = OrderedDict([(name, ftype()) for (name, ftype) in (EmComponent._fields + self.__class__._fields)])
57 57
 
58
-        # populate
59
-        if isinstance(id_or_name, int):
60
-            self._fields['uid'].value = id_or_name  # read only propertie set
61
-        elif isinstance(id_or_name, str):
62
-            self.name = id_or_name
63
-        else:
64
-            raise TypeError('Bad argument: expecting <int> or <str> but got : ' + str(type(id_or_name)))
65
-        self.populate()
58
+        for name, value in data.items():
59
+            if name in self._fields:
60
+                self._fields[name].from_string(value)
61
+
66 62
 
67 63
     ## @brief Access an attribute of an EmComponent
68 64
     # This method is overloads the default __getattr__ to search in EmComponents::_fields . If there is an EditorialModel::EmField with a corresponding name in the component

+ 36
- 0
EditorialModel/model.py View File

@@ -0,0 +1,36 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+## @file editorialmodel.py
4
+# Manage instance of an editorial model
5
+
6
+from EditorialModel.classes import EmClass
7
+
8
+class Model(object):
9
+    componentClass = EmClass
10
+
11
+    def __init__(self, backend):
12
+        self.backend = backend
13
+        self.load()
14
+
15
+    def load(self):
16
+        data = self.backend.load()
17
+        self.uids = {}
18
+        for uid, component in data.items():
19
+            cls_name = 'component' + component['component']
20
+            cls = getattr(Model, cls_name)
21
+            if cls:
22
+                component['uid'] = uid
23
+                self.uids[uid] = cls(component)
24
+
25
+    # save data using the current backend
26
+    def save(self):
27
+        return self.backend.save()
28
+
29
+    # change the current backend
30
+    def set_backend(self, backend):
31
+        self.backend = backend
32
+
33
+    # return a list of all EmClass of the model
34
+    def classes():
35
+        classes = [component for component in self.data.uids if isinstance(component, EmClass)]
36
+        return classes

+ 8
- 0
EditorialModel/test/me.json View File

@@ -0,0 +1,8 @@
1
+{
2
+  "1" : {
3
+    "component":"Class", "name":"textes", "string":"{\"fre\":\"Texte\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "classtype":"entity", "icon":"0", "sortcolumn":"rank"
4
+  },
5
+  "2" : {
6
+    "component":"Class", "name":"publications", "string":"{\"fre\":\"Rubriques\"}", "help":"{}", "rank":"1", "date_update":"", "date_create":"", "classtype":"entity", "icon":"0", "sortcolumn":"rank"
7
+  }
8
+}

Loading…
Cancel
Save