Browse Source

Emtype: superiors_list should be a dict with list of int

=> a type can have multiple superiors (only instance of LeObject can have one)
ArnAud 9 years ago
parent
commit
644f76b14d
3 changed files with 62 additions and 46 deletions
  1. 3
    2
      EditorialModel/model.py
  2. 2
    2
      EditorialModel/test/me.json
  3. 57
    42
      EditorialModel/types.py

+ 3
- 2
EditorialModel/model.py View File

@@ -247,8 +247,9 @@ class Model(object):
247 247
         # add superiors to types
248 248
         for superiors_list in relations['superiors_list']:
249 249
             uid, sup_list = superiors_list
250
-            for nature, superior_uid in sup_list.items():
251
-                new_me.component(uid).add_superior(new_me.component(superior_uid), nature)
250
+            for nature, superiors_uid in sup_list.items():
251
+                for superior_uid in superiors_uid:
252
+                    new_me.component(uid).add_superior(new_me.component(superior_uid), nature)
252 253
 
253 254
         del new_me
254 255
 

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

@@ -12,7 +12,7 @@
12 12
     "component":"EmField", "name":"titre", "string":"{\"fre\":\"Titre\"}", "help_text":"{}", "rank":"1", "date_update":"", "date_create":"", "fieldtype":"char", "fieldgroup_id":"3", "rel_field_id":"", "optional":0, "internal":"", "icon":"0"
13 13
   },
14 14
   "5" : {
15
-    "component":"EmType", "name":"article", "string":"{\"fre\":\"Article\"}", "help_text":"{}", "rank":"1", "date_update":"", "date_create":"", "class_id":"1", "icon":"0", "sortcolumn":"rank", "fields_list":[7], "superiors_list":{"parent":14}
15
+    "component":"EmType", "name":"article", "string":"{\"fre\":\"Article\"}", "help_text":"{}", "rank":"1", "date_update":"", "date_create":"", "class_id":"1", "icon":"0", "sortcolumn":"rank", "fields_list":[7], "superiors_list":{"parent":[14]}
16 16
   },
17 17
   "6" : {
18 18
     "component":"EmType", "name":"personne", "string":"{\"fre\":\"Personne\"}", "help_text":"{}", "rank":"1", "date_update":"", "date_create":"", "class_id":"2", "icon":"0", "sortcolumn":"rank", "fields_list":[10]
@@ -39,7 +39,7 @@
39 39
     "component":"EmClass", "name":"publication", "string":"{\"fre\":\"Publication\"}", "help_text":"{}", "rank":"2", "date_update":"", "date_create":"", "classtype":"entity", "icon":"0", "sortcolumn":"rank"
40 40
   },
41 41
   "14" : {
42
-    "component":"EmType", "name":"rubrique", "string":"{\"fre\":\"Rubrique\"}", "help_text":"{}", "rank":"1", "date_update":"", "date_create":"", "class_id":"13", "icon":"0", "sortcolumn":"rank", "superiors_list":{"parent":14}
42
+    "component":"EmType", "name":"rubrique", "string":"{\"fre\":\"Rubrique\"}", "help_text":"{}", "rank":"1", "date_update":"", "date_create":"", "class_id":"13", "icon":"0", "sortcolumn":"rank", "superiors_list":{"parent":[14]}
43 43
   },
44 44
   "15" : {
45 45
     "component":"EmFieldGroup", "name":"info", "string":"{\"fre\":\"Info\"}", "help_text":"{}", "rank":"1", "date_update":"", "date_create":"", "class_id":"13"

+ 57
- 42
EditorialModel/types.py View File

@@ -30,17 +30,20 @@ class EmType(EmComponent):
30 30
         self.check_type('class_id', int)
31 31
         self.fields_list = fields_list if fields_list is not None else []
32 32
         self.check_type('fields_list', list)
33
-        for l_uid in self.fields_list:
34
-            if not isinstance(l_uid, int):
35
-                raise AttributeError("Excepted fields_list to be a list of integers, but found a +"+str(type(l_uid))+" in it")
33
+        for field_uid in self.fields_list:
34
+            if not isinstance(field_uid, int):
35
+                raise AttributeError("Excepted fields_list to be a list of integers, but found a " + str(type(field_uid)) + " in it")
36 36
 
37 37
         self.superiors_list = superiors_list if superiors_list is not None else {}
38 38
         self.check_type('superiors_list', dict)
39
-        for nature, sup_uid in self.superiors_list.items():
39
+        for nature, superiors_uid in self.superiors_list.items():
40 40
             if nature not in [EmNature.PARENT, EmNature.TRANSLATION, EmNature.IDENTITY]:
41
-                raise AttributeError("Nature '%s' of subordinates is not allowed !" % nature)
42
-            if not isinstance(sup_uid, int):
43
-                raise AttributeError("Excepted subordinates of nature '%s' to be an int !" % nature)
41
+                raise AttributeError("Nature '%s' of superior is not allowed !" % nature)
42
+            if not isinstance(superiors_uid, list):
43
+                raise AttributeError("Excepted superiors of nature '%s' to be an list !" % nature)
44
+            for superior_uid in superiors_uid:
45
+                if not isinstance(superior_uid, int):
46
+                    raise AttributeError("Excepted superiors_list of nature '%s' to be a list of integers, but found a '%s' in it" % str(type(superior_uid)))
44 47
 
45 48
         self.icon = icon
46 49
         self.sortcolumn = sortcolumn
@@ -196,8 +199,8 @@ class EmType(EmComponent):
196 199
     def subordinates(self):
197 200
         subordinates = {}
198 201
         for em_type in self.model.components(EmType):
199
-            for nature, superior_uid in em_type.superiors_list.items():
200
-                if self.uid == superior_uid:
202
+            for nature, superiors_uid in em_type.superiors_list.items():
203
+                if self.uid in superiors_uid:
201 204
                     if nature in subordinates:
202 205
                         subordinates[nature].append(em_type)
203 206
                     else:
@@ -209,7 +212,7 @@ class EmType(EmComponent):
209 212
     # @return Return a dict with relation nature as keys and an EmType as value
210 213
     # @throw RuntimeError if a nature has multiple superiors
211 214
     def superiors(self):
212
-        return { nature:self.model.component(superior_uid) for nature, superior_uid in self.superiors_list.items() }
215
+        return { nature:[self.model.component(superior_uid) for superior_uid in superiors_uid] for nature, superiors_uid in self.superiors_list.items() }
213 216
 
214 217
     ## Add a superior in the type hierarchy
215 218
     # @param em_type EmType: An EmType instance
@@ -252,16 +255,27 @@ class EmType(EmComponent):
252 255
 
253 256
         try:
254 257
             if add:
255
-                self.superiors_list[relation_nature] = em_type.uid
258
+                if relation_nature in self.superiors_list:
259
+                    self.superiors_list[relation_nature].append(em_type.uid)
260
+                else:
261
+                    self.superiors_list[relation_nature] = [em_type.uid]
256 262
                 self.model.migration_handler.register_change(self.model, self.uid, None, {'superiors_list': {relation_nature: em_type.uid}})
257 263
             else:
258
-                del self.superiors_list[relation_nature]
264
+                self.superiors_list[relation_nature].remove(em_type.uid)
265
+                if len(self.superiors_list[relation_nature]) == 0:
266
+                    del self.superiors_list[relation_nature]
259 267
                 self.model.migration_handler.register_change(self.model, self.uid, {'superiors_list': {relation_nature: em_type.uid}}, None)
268
+        # roll-back
260 269
         except MigrationHandlerChangeError as exception_object:
261 270
             if add:
262
-                del self.superiors_list[relation_nature]
271
+                self.superiors_list[relation_nature].remove(em_type.uid)
272
+                if len(self.superiors_list[relation_nature]) == 0:
273
+                    del self.superiors_list[relation_nature]
263 274
             else:
264
-                self.superiors_list[relation_nature] = em_type.uid
275
+                if relation_nature in self.superiors_list:
276
+                    self.superiors_list[relation_nature].append(em_type.uid)
277
+                else:
278
+                    self.superiors_list[relation_nature] = [em_type.uid]
265 279
             raise exception_object
266 280
 
267 281
         self.model.migration_handler.register_model_state(self.model, hash(self.model))
@@ -287,35 +301,36 @@ class EmType(EmComponent):
287 301
             if field.fieldgroup_id not in [ fg.uid for fg in self.fieldgroups() ]:
288 302
                 raise EmComponentCheckErrro("The element %d of selected_field is an EmField that is part of an EmFieldGroup that is not associated with this EmType" % i)
289 303
 
290
-        for nature, sup_uid in self.superiors_list.items():
291
-            em_type = self.model.component(sup_uid)
292
-            if not em_type:
293
-                raise EmComponentCheckError("The superior is a non existing uid '%d'" % (sup_uid))
294
-            if not isinstance(em_type, EmType):
295
-                raise EmComponentCheckError("The superior is a component that is not an EmType but a %s" % (str(type(em_type))))
296
-            if nature not in EmClassType.natures(self.em_class.classtype):
297
-                raise EmComponentCheckError("The relation nature '%s' of the superior is not valid for this EmType classtype '%s'", (nature, self.classtype) )
298
-
299
-            nat_spec = getattr(EmClassType, self.em_class.classtype)['hierarchy'][nature]
300
-
301
-            if nat_spec['attach'] == 'classtype':
302
-                if self.classtype != em_type.classtype:
303
-                    raise EmComponentCheckError("The superior is of '%s' classtype. But the current type is of '%s' classtype, and relation nature '%s' require two EmType of same classtype" % (em_type.classtype, self.classtype, nature) )
304
-            elif nat_spec['attach'] == 'type':
305
-                if self.uid != em_type.uid:
306
-                    raise EmComponentCheckError("The superior is a different EmType. But the relation nature '%s' require the same EmType" % (nature))
307
-            else:
308
-                raise NotImplementedError("The nature['attach'] '%s' is not implemented in this check !" % nat_spec['attach'])
309
-
310
-            if 'max_depth' in nat_spec and nat_spec['max_depth'] > 0:
311
-                depth = 1
312
-                cur_type = em_type
313
-                while depth >= nat_spec['max_depth']:
314
-                    depth +=1
315
-                    if len(cur_type.subordinates()[nature]) == 0:
316
-                        break
304
+        for nature, superiors_uid in self.superiors_list.items():
305
+            for superior_uid in superiors_uid:
306
+                em_type = self.model.component(superior_uid)
307
+                if not em_type:
308
+                    raise EmComponentCheckError("The superior is a non existing uid '%d'" % (superior_uid))
309
+                if not isinstance(em_type, EmType):
310
+                    raise EmComponentCheckError("The superior is a component that is not an EmType but a %s" % (str(type(em_type))))
311
+                if nature not in EmClassType.natures(self.em_class.classtype):
312
+                    raise EmComponentCheckError("The relation nature '%s' of the superior is not valid for this EmType classtype '%s'", (nature, self.classtype) )
313
+
314
+                nat_spec = getattr(EmClassType, self.em_class.classtype)['hierarchy'][nature]
315
+
316
+                if nat_spec['attach'] == 'classtype':
317
+                    if self.classtype != em_type.classtype:
318
+                        raise EmComponentCheckError("The superior is of '%s' classtype. But the current type is of '%s' classtype, and relation nature '%s' require two EmType of same classtype" % (em_type.classtype, self.classtype, nature) )
319
+                elif nat_spec['attach'] == 'type':
320
+                    if self.uid != em_type.uid:
321
+                        raise EmComponentCheckError("The superior is a different EmType. But the relation nature '%s' require the same EmType" % (nature))
317 322
                 else:
318
-                    raise EmComponentCheckError("The relation with the element %d of subordinates has a depth superior than the maximum depth ( %d ) allowed by the relation's nature ( '%s' )" %( i, nat_spec['max_depth'], nature) )
323
+                    raise NotImplementedError("The nature['attach'] '%s' is not implemented in this check !" % nat_spec['attach'])
324
+
325
+                if 'max_depth' in nat_spec and nat_spec['max_depth'] > 0:
326
+                    depth = 1
327
+                    cur_type = em_type
328
+                    while depth >= nat_spec['max_depth']:
329
+                        depth +=1
330
+                        if len(cur_type.subordinates()[nature]) == 0:
331
+                            break
332
+                    else:
333
+                        raise EmComponentCheckError("The relation with the element %d of subordinates has a depth superior than the maximum depth ( %d ) allowed by the relation's nature ( '%s' )" %( i, nat_spec['max_depth'], nature) )
319 334
 
320 335
         for nature in self.subordinates():
321 336
             nat_spec = getattr(EmClassType, self.em_class.classtype)['hierarchy'][nature]

Loading…
Cancel
Save