[schema] Fix definition of the container relation
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 22 Mar 2017 17:37:14 +0100
changeset 2532 a88deb387b2b
parent 2531 02f4b849a80f
child 2542 1c4456729955
[schema] Fix definition of the container relation Cardinality and permissions defined on the relation type wasn't considered as expected because the relation is added dynamically in post_build_callback. We have to set them explicitly there. This added to a bug in the cloning code led to entities with several containers. To fix this, migration script pick the container with the greatest eid since it should be the one which is not unexpectedly copied. Anyway, even if that's wrong that's no big deal because there are only demo instances for now which don't really care of data loss. This patch also set properly cardinality on container relation for SEDAArchiveUnit, which is expected to be optional while mandatory for others. This has not been detected before since it wasn't actually mandatory by default before this patch.
cubicweb_seda/migration/0.9.1_Any.py
cubicweb_seda/schema/__init__.py
test/test_schema.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cubicweb_seda/migration/0.9.1_Any.py	Wed Mar 22 17:37:14 2017 +0100
@@ -0,0 +1,9 @@
+for e in rql('Any X groupby X WHERE X container C HAVING COUNT(C) > 1').entities():
+    container = max(c.eid for c in e.container)
+    e.cw_set(container=None)
+    e.cw_set(container=container)
+    commit()
+
+sync_schema_props_perms('container')
+
+
--- a/cubicweb_seda/schema/__init__.py	Tue Mar 28 10:55:58 2017 +0200
+++ b/cubicweb_seda/schema/__init__.py	Wed Mar 22 17:37:14 2017 +0100
@@ -86,8 +86,6 @@
 
 
 class container(RelationType):
-    __permissions__ = {'add': (), 'delete': (), 'read': ()}
-    cardinality = '1*'
     inlined = False
 
 
@@ -171,7 +169,14 @@
     for etype, parent_rdefs in seda_profile_container_def(schema):
         # add relation to the container from every entity type within the compound graph
         for container_etype in container_etypes:
-            container_rdef = RelationDefinition(etype, 'container', container_etype)
+            # special case for SEDAArchiveUnit's cardinality as it may be both
+            # contained and container
+            container_rdef = RelationDefinition(
+                etype, 'container', container_etype,
+                cardinality='?*' if etype == 'SEDAArchiveUnit' else '1*',
+                __permissions__={'add': (),
+                                 'delete': (),
+                                 'read': ('managers', 'users', 'guests')})
             schema.add_relation_def(container_rdef)
         eschema = schema[etype]
         # set permissions on entity types from the compound graph according to permission on the
--- a/test/test_schema.py	Tue Mar 28 10:55:58 2017 +0200
+++ b/test/test_schema.py	Wed Mar 22 17:37:14 2017 +0100
@@ -255,6 +255,17 @@
             scheme.cw_set(code_keyword_type=ckt_concept)
             cnx.commit()
 
+    def test_container_relation(self):
+        container_rtype = self.schema['container']
+        for rdef in container_rtype.rdefs.values():
+            if rdef.subject == 'SEDAArchiveUnit':
+                self.assertEqual(rdef.cardinality, '?*')
+            else:
+                self.assertEqual(rdef.cardinality, '1*')
+            self.assertEqual(rdef.permissions, {'add': (),
+                                                'delete': (),
+                                                'read': ('managers', 'users', 'guests')})
+
 
 class SecurityTC(CubicWebTC):