[PATCH 1 of 1 seda V2] [schema] Fix definition of the container relation

Sylvain Thenault sylvain.thenault at logilab.fr
Tue Mar 28 10:57:09 CEST 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1490200634 -3600
#      Wed Mar 22 17:37:14 2017 +0100
# Node ID a88deb387b2beaa2b976493fffbab1b7dac467b4
# Parent  02f4b849a80fbceef658f327c5799b549d4c4562
[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.

diff --git a/cubicweb_seda/migration/0.9.1_Any.py b/cubicweb_seda/migration/0.9.1_Any.py
new file mode 100644
--- /dev/null
+++ b/cubicweb_seda/migration/0.9.1_Any.py
@@ -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')
+
+
diff --git a/cubicweb_seda/schema/__init__.py b/cubicweb_seda/schema/__init__.py
--- a/cubicweb_seda/schema/__init__.py
+++ b/cubicweb_seda/schema/__init__.py
@@ -84,12 +84,10 @@ class seda_keyword_reference_to_scheme(R
     cardinality = '?*'
     inlined = True
 
 
 class container(RelationType):
-    __permissions__ = {'add': (), 'delete': (), 'read': ()}
-    cardinality = '1*'
     inlined = False
 
 
 class _clone_of(RelationDefinition):
     __permissions__ = {'read': ('managers', 'users', 'guests'),
@@ -169,11 +167,18 @@ def post_build_callback(schema):
 
     container_etypes = ('SEDAArchiveTransfer', 'SEDAArchiveUnit')
     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
         # container entity
         for action in ('update', 'delete'):
diff --git a/test/test_schema.py b/test/test_schema.py
--- a/test/test_schema.py
+++ b/test/test_schema.py
@@ -253,10 +253,21 @@ class SchemaTC(CubicWebTC):
                 cnx.commit()
 
             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):
 
     @contextmanager
     def assertUnauthorized(self, cnx):


More information about the saem-devel mailing list