Browse Source

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

Yann Weber 8 years ago
parent
commit
69f59baa2c

+ 10
- 5
em_test.py View File

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

BIN
examples/em_test.pickle View File


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

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

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

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

BIN
tests/editorial_model.pickle View File


Loading…
Cancel
Save