Kaynağa Gözat

Add more capabilities to Model class to handles actives groups

Yann Weber 8 yıl önce
ebeveyn
işleme
2739a94e5a
1 değiştirilmiş dosya ile 57 ekleme ve 15 silme
  1. 57
    15
      lodel/editorial_model/model.py

+ 57
- 15
lodel/editorial_model/model.py Dosyayı Görüntüle

@@ -4,6 +4,9 @@ import hashlib
4 4
 import importlib
5 5
 
6 6
 from lodel.utils.mlstring import MlString
7
+from lodel.logger import logger
8
+from lodel.settings import Settings
9
+from lodel.settings.utils import SettingsError
7 10
 
8 11
 from lodel.editorial_model.exceptions import *
9 12
 from lodel.editorial_model.components import EmClass, EmField, EmGroup
@@ -21,13 +24,21 @@ class EditorialModel(object):
21 24
         self.__groups = dict()
22 25
         ##@brief Stores all classes indexed by id
23 26
         self.__classes = dict()
27
+        ## @brief Stores all activated groups indexed by id
28
+        self.__active_groups = dict()
29
+        ## @brief Stores all activated classes indexed by id
30
+        self.__active_classes = dict()
24 31
     
25 32
     ##@brief EmClass accessor
26
-    # @param uid None | str : give this argument to get a specific EmClass
27
-    # @return if uid is given returns an EmClass else returns an EmClass iterator
33
+    #@param uid None | str : give this argument to get a specific EmClass
34
+    #@return if uid is given returns an EmClass else returns an EmClass
35
+    # iterator
36
+    #@todo use Settings.editorialmodel.groups to determine wich classes should
37
+    # be returned
28 38
     def classes(self, uid = None):
29 39
         try:
30
-            return self.__elt_getter(self.__classes, uid)
40
+            return self.__elt_getter(   self.__active_classes,
41
+                                        uid)
31 42
         except KeyError:
32 43
             raise EditorialModelException("EmClass not found : '%s'" % uid)
33 44
 
@@ -36,10 +47,34 @@ class EditorialModel(object):
36 47
     # @return if uid is given returns an EmGroup else returns an EmGroup iterator
37 48
     def groups(self, uid = None):
38 49
         try:
39
-            return self.__elt_getter(self.__groups, uid)
50
+            return self.__elt_getter(   self.__active_groups,
51
+                                        uid)
40 52
         except KeyError:
41 53
             raise EditorialModelException("EmGroup not found : '%s'" % uid)
42 54
     
55
+    ##@brief Private getter for __groups or __classes
56
+    # @see classes() groups()
57
+    def __elt_getter(self, elts, uid):
58
+        return list(elts.values()) if uid is None else elts[uid]
59
+    
60
+    ##@brief Update the EditorialModel.__active_groups and
61
+    #EditorialModel.__active_classes attibutes
62
+    def __set_actives(self):
63
+        if Settings.editorialmodel.editormode:
64
+            # all groups & classes actives because we are in editor mode
65
+            self.__active_groups = self.__groups
66
+            self.__active_classes = self.__classes
67
+        else:
68
+            #determine groups first
69
+            self.__active_groups = dict()
70
+            for agrp in Settings.editorialmodel.groups:
71
+                if agrp not in self.__groups:
72
+                    raise SettingsError('Invalid group found in settings : %s' % agrp)
73
+                grp = self.__groups[agrp]
74
+                self.__active_groups[grp.uid] = grp
75
+                for acls in grp.components():
76
+                    self.__active_classes[acls.uid] = acls
77
+    
43 78
     ##@brief EmField getter
44 79
     # @param uid str : An EmField uid represented by "CLASSUID.FIELDUID"
45 80
     # @return Fals or an EmField instance
@@ -65,6 +100,7 @@ class EditorialModel(object):
65 100
     # @param emclass EmClass : the EmClass instance to add
66 101
     # @return emclass
67 102
     def add_class(self, emclass):
103
+        self.raise_if_ro()
68 104
         if not isinstance(emclass, EmClass):
69 105
             raise ValueError("<class EmClass> expected but got %s " % type(emclass))
70 106
         if emclass.uid in self.classes():
@@ -76,6 +112,7 @@ class EditorialModel(object):
76 112
     # @param emgroup EmGroup : the EmGroup instance to add
77 113
     # @return emgroup
78 114
     def add_group(self, emgroup):
115
+        self.raise_if_ro()
79 116
         if not isinstance(emgroup, EmGroup):
80 117
             raise ValueError("<class EmGroup> expected but got %s" % type(emgroup))
81 118
         if emgroup.uid in self.groups():
@@ -84,25 +121,36 @@ class EditorialModel(object):
84 121
         return emgroup
85 122
 
86 123
     ##@brief Add a new EmClass to the editorial model
87
-    # @param uid str : EmClass uid
88
-    # @param **kwargs : EmClass constructor options ( see @ref lodel.editorial_model.component.EmClass.__init__() )
124
+    #@param uid str : EmClass uid
125
+    #@param **kwargs : EmClass constructor options ( 
126
+    # see @ref lodel.editorial_model.component.EmClass.__init__() )
89 127
     def new_class(self, uid, **kwargs):
128
+        self.raise_if_ro()
90 129
         return self.add_class(EmClass(uid, **kwargs))
91 130
     
92 131
     ##@brief Add a new EmGroup to the editorial model
93
-    # @param uid str : EmGroup uid
94
-    # @param *kwargs : EmGroup constructor keywords arguments (see @ref lodel.editorial_model.component.EmGroup.__init__() )
132
+    #@param uid str : EmGroup uid
133
+    #@param *kwargs : EmGroup constructor keywords arguments (
134
+    # see @ref lodel.editorial_model.component.EmGroup.__init__() )
95 135
     def new_group(self, uid, **kwargs):
136
+        self.raise_if_ro()
96 137
         return self.add_group(EmGroup(uid, **kwargs))
97 138
 
98
-    # @brief Save a model
139
+    ##@brief Save a model
99 140
     # @param translator module : The translator module to use
100 141
     # @param **translator_args
101 142
     def save(self, translator, **translator_kwargs):
143
+        self.raise_if_ro()
102 144
         if isinstance(translator, str):
103 145
             translator = self.translator_from_name(translator)
104 146
         return translator.save(self, **translator_kwargs)
105 147
     
148
+    ##@brief Raise an error if lodel is not in EM edition mode
149
+    @staticmethod
150
+    def raise_if_ro():
151
+        if not Settings.editorialmodel.editormode:
152
+            raise EditorialModelError("Lodel in not in EM editor mode. The EM is in read only state")
153
+
106 154
     ##@brief Load a model
107 155
     # @param translator module : The translator module to use
108 156
     # @param **translator_args
@@ -125,12 +173,6 @@ class EditorialModel(object):
125 173
             raise NameError("No translator named %s")
126 174
         return mod
127 175
         
128
-    
129
-    ##@brief Private getter for __groups or __classes
130
-    # @see classes() groups()
131
-    def __elt_getter(self, elts, uid):
132
-        return list(elts.values()) if uid is None else elts[uid]
133
-    
134 176
     ##@brief Lodel hash
135 177
     def d_hash(self):
136 178
         payload = "%s%s" % (

Loading…
İptal
Kaydet