Browse Source

Bugfix on group activation (in EmClasses all fields were always actives)

Yann Weber 7 years ago
parent
commit
69f59baa2c

+ 10
- 5
em_test.py View File

@@ -324,14 +324,16 @@ index_name = index_abstract.new_field(
324 324
     display_name = {
325 325
         'eng': 'name',
326 326
         'fre': 'nom'},
327
-    data_handler = 'varchar')
327
+    data_handler = 'varchar',
328
+    group = index_group)
328 329
 
329 330
 index_value = index_abstract.new_field(
330 331
     'value',
331 332
     display_name = {
332 333
         'eng': 'value',
333 334
         'fre': 'valeur'},
334
-    data_handler = 'varchar')
335
+    data_handler = 'varchar',
336
+    group = index_group)
335 337
 
336 338
 text.new_field( 'indexes',
337 339
     display_name = {
@@ -341,7 +343,8 @@ text.new_field( 'indexes',
341 343
     back_reference = ('Indexabs', 'texts'),
342 344
     allowed_classes = [index_abstract],
343 345
     default = None,
344
-    nullable = True)
346
+    nullable = True,
347
+    group = index_group)
345 348
 
346 349
 index_abstract.new_field( 'texts',
347 350
     display_name = {
@@ -349,7 +352,8 @@ index_abstract.new_field( 'texts',
349 352
         'fre': 'Texte contenant cette index'},
350 353
     data_handler = 'list',
351 354
     back_reference = ('Text', 'indexes'),
352
-    allowed_classes = [text])
355
+    allowed_classes = [text],
356
+    group = index_group)
353 357
 
354 358
 index_theme = em.new_class(
355 359
     'indexTheme',
@@ -364,7 +368,8 @@ index_theme_theme = index_abstract.new_field(
364 368
     'theme',
365 369
     display_name = {
366 370
         'eng': 'theme'},
367
-    data_handler = 'varchar')
371
+    data_handler = 'varchar',
372
+    group = index_group)
368 373
 
369 374
 #em.save('xmlfile', filename = 'examples/em_test.xml')
370 375
 pickle_file = 'examples/em_test.pickle'

BIN
examples/em_test.pickle View File


+ 14
- 0
lodel/editorial_model/components.py View File

@@ -138,6 +138,15 @@ class EmClass(EmComponent):
138 138
             return list(fields.values()) if uid is None else fields[uid]
139 139
         except KeyError:
140 140
             raise EditorialModelError("No such EmField '%s'" % uid)
141
+    
142
+    ##@brief Keep in __fields only fields contained in active groups
143
+    def _set_active_fields(self, active_groups):
144
+        active_fields = []
145
+        for grp_name, agrp in active_groups.items():
146
+            active_fields += [ emc for emc in agrp.components()
147
+                if isinstance(emc, EmField)]
148
+        self.__fields = { fname:fdh for fname, fdh in self.__fields.items()
149
+            if fdh in active_fields }
141 150
 
142 151
     ##@brief Add a field to the EmClass
143 152
     # @param emfield EmField : an EmField instance
@@ -145,6 +154,7 @@ class EmClass(EmComponent):
145 154
     # @throw EditorialModelException if an EmField with same uid allready in this EmClass (overwritting allowed from parents)
146 155
     # @todo End the override checks (needs methods in data_handlers)
147 156
     def add_field(self, emfield):
157
+        assert_edit()
148 158
         if emfield.uid in self.__fields:
149 159
             raise EditorialModelError("Duplicated uid '%s' for EmField in this class ( %s )" % (emfield.uid, self))
150 160
         # Incomplete field override check
@@ -161,6 +171,7 @@ class EmClass(EmComponent):
161 171
     # @param uid str : the EmField uniq id
162 172
     # @param **field_kwargs :  EmField constructor parameters ( see @ref EmField.__init__() ) 
163 173
     def new_field(self, uid, data_handler, **field_kwargs):
174
+        assert_edit()
164 175
         return self.add_field(EmField(uid, data_handler, **field_kwargs))
165 176
 
166 177
     def d_hash(self):
@@ -317,6 +328,7 @@ class EmGroup(object):
317 328
     ##@brief Add components in a group
318 329
     # @param components list : EmComponent instances list
319 330
     def add_components(self, components):
331
+        assert_edit()
320 332
         for component in components:
321 333
             if isinstance(component, EmField):
322 334
                 if component._emclass is None:
@@ -328,6 +340,7 @@ class EmGroup(object):
328 340
     ##@brief Add a dependencie
329 341
     # @param em_group EmGroup|iterable : an EmGroup instance or list of instance
330 342
     def add_dependencie(self, grp):
343
+        assert_edit()
331 344
         try:
332 345
             for group in grp:
333 346
                 self.add_dependencie(group)
@@ -345,6 +358,7 @@ class EmGroup(object):
345 358
     # @param em_group EmGroup|iterable : an EmGroup instance or list of instance
346 359
     # Useless ???
347 360
     def add_applicant(self, grp):
361
+        assert_edit()
348 362
         try:
349 363
             for group in grp:
350 364
                 self.add_applicant(group)

+ 6
- 0
lodel/editorial_model/exceptions.py View File

@@ -3,3 +3,9 @@
3 3
 class EditorialModelError(Exception):
4 4
     pass
5 5
 
6
+
7
+def assert_edit():
8
+    from lodel import Settings
9
+    if not Settings.editorialmodel.editormode:
10
+        raise EditorialModelError("EM is readonly : editormode is OFF")
11
+

+ 7
- 5
lodel/editorial_model/model.py View File

@@ -148,6 +148,8 @@ class EditorialModel(object):
148 148
                 raise RuntimeError("No groups activated, abording...")
149 149
             if len(self.__active_classes) == 0:
150 150
                 raise RuntimeError("No active class found. Abording")
151
+            for clsname, acls in self.__active_classes.items():
152
+                acls._set_active_fields(self.__active_groups)
151 153
     
152 154
     ##@brief EmField getter
153 155
     # @param uid str : An EmField uid represented by "CLASSUID.FIELDUID"
@@ -174,7 +176,7 @@ class EditorialModel(object):
174 176
     # @param emclass EmClass : the EmClass instance to add
175 177
     # @return emclass
176 178
     def add_class(self, emclass):
177
-        self.raise_if_ro()
179
+        assert_edit()()
178 180
         if not isinstance(emclass, EmClass):
179 181
             raise ValueError("<class EmClass> expected but got %s " % type(emclass))
180 182
         if emclass.uid in self.classes():
@@ -186,7 +188,7 @@ class EditorialModel(object):
186 188
     # @param emgroup EmGroup : the EmGroup instance to add
187 189
     # @return emgroup
188 190
     def add_group(self, emgroup):
189
-        self.raise_if_ro()
191
+        assert_edit()()
190 192
         if not isinstance(emgroup, EmGroup):
191 193
             raise ValueError("<class EmGroup> expected but got %s" % type(emgroup))
192 194
         if emgroup.uid in self.groups():
@@ -199,7 +201,7 @@ class EditorialModel(object):
199 201
     #@param **kwargs : EmClass constructor options ( 
200 202
     # see @ref lodel.editorial_model.component.EmClass.__init__() )
201 203
     def new_class(self, uid, **kwargs):
202
-        self.raise_if_ro()
204
+        assert_edit()()
203 205
         return self.add_class(EmClass(uid, **kwargs))
204 206
     
205 207
     ##@brief Add a new EmGroup to the editorial model
@@ -207,14 +209,14 @@ class EditorialModel(object):
207 209
     #@param *kwargs : EmGroup constructor keywords arguments (
208 210
     # see @ref lodel.editorial_model.component.EmGroup.__init__() )
209 211
     def new_group(self, uid, **kwargs):
210
-        self.raise_if_ro()
212
+        assert_edit()()
211 213
         return self.add_group(EmGroup(uid, **kwargs))
212 214
 
213 215
     ##@brief Save a model
214 216
     # @param translator module : The translator module to use
215 217
     # @param **translator_args
216 218
     def save(self, translator, **translator_kwargs):
217
-        self.raise_if_ro()
219
+        assert_edit()()
218 220
         if isinstance(translator, str):
219 221
             translator = self.translator_from_name(translator)
220 222
         return translator.save(self, **translator_kwargs)

BIN
tests/editorial_model.pickle View File


Loading…
Cancel
Save