[entity] Exclude computed relations from Entity.copy_relations
authorDenis Laxalde <denis.laxalde@logilab.fr>
Fri, 22 Apr 2016 16:16:09 +0200
changeset 11261 9e926f2dc84d
parent 11260 0119ba327117
child 11262 25f9a76ddf50
[entity] Exclude computed relations from Entity.copy_relations Closes #12481591.
entity.py
test/data/schema.py
test/unittest_entity.py
test/unittest_schema.py
--- a/entity.py	Thu May 19 10:51:33 2016 +0200
+++ b/entity.py	Fri Apr 22 16:16:09 2016 +0200
@@ -783,7 +783,7 @@
         for rschema in self.e_schema.subject_relations():
             if rschema.type in skip_copy_for['subject']:
                 continue
-            if rschema.final or rschema.meta:
+            if rschema.final or rschema.meta or rschema.rule:
                 continue
             # skip already defined relations
             if getattr(self, rschema.type):
@@ -802,7 +802,7 @@
             execute(rql, {'x': self.eid, 'y': ceid})
             self.cw_clear_relation_cache(rschema.type, 'subject')
         for rschema in self.e_schema.object_relations():
-            if rschema.meta:
+            if rschema.meta or rschema.rule:
                 continue
             # skip already defined relations
             if self.related(rschema.type, 'object'):
--- a/test/data/schema.py	Thu May 19 10:51:33 2016 +0200
+++ b/test/data/schema.py	Fri Apr 22 16:16:09 2016 +0200
@@ -17,7 +17,7 @@
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
 
 from yams.buildobjs import (EntityType, String, RichString, Bytes,
-                            SubjectRelation, RelationDefinition)
+                            ComputedRelation, SubjectRelation, RelationDefinition)
 
 from cubicweb.schema import (WorkflowableEntityType,
                              RQLConstraint, RQLVocabularyConstraint)
@@ -26,6 +26,10 @@
 from cubicweb import _
 
 
+class buddies(ComputedRelation):
+    rule = 'S in_group G, O in_group G'
+
+
 class Personne(EntityType):
     nom = String(required=True)
     prenom = String()
--- a/test/unittest_entity.py	Thu May 19 10:51:33 2016 +0200
+++ b/test/unittest_entity.py	Fri Apr 22 16:16:09 2016 +0200
@@ -138,6 +138,22 @@
             e.cw_clear_relation_cache('in_state', 'subject')
             self.assertEqual(e.cw_adapt_to('IWorkflowable').state, 'activated')
 
+    def test_copy_exclude_computed_relations(self):
+        """The `CWUser buddies CWUser` (computed) relation should not be copied.
+        """
+        with self.admin_access.cnx() as cnx:
+            friends = cnx.create_entity('CWGroup', name=u'friends')
+            bob = self.create_user(cnx, u'bob', groups=('friends',))
+            cnx.create_entity('EmailAddress', address=u'bob@cubicweb.org',
+                              reverse_use_email=bob)
+            alice = self.create_user(cnx, u'alices', groups=('friends',))
+            cnx.commit()
+            charles = self.create_user(cnx, u'charles')
+            cnx.commit()
+            # Just ensure this does not crash (it would if computed relation
+            # attempted to be copied).
+            charles.copy_relations(bob.eid)
+
     def test_related_cache_both(self):
         with self.admin_access.web_request() as req:
             user = req.execute('Any X WHERE X eid %(x)s', {'x':req.user.eid}).get_entity(0, 0)
--- a/test/unittest_schema.py	Thu May 19 10:51:33 2016 +0200
+++ b/test/unittest_schema.py	Fri Apr 22 16:16:09 2016 +0200
@@ -178,7 +178,7 @@
         self.assertListEqual(sorted(expected_entities), entities)
         relations = sorted([str(r) for r in schema.relations()])
         expected_relations = ['actionnaire', 'add_permission', 'address', 'alias', 'allowed_transition', 'associe',
-                              'bookmarked_by', 'by_transition',
+                              'bookmarked_by', 'by_transition', 'buddies',
 
                               'cardinality', 'comment', 'comment_format',
                               'composite', 'condition', 'config', 'connait',
@@ -225,7 +225,7 @@
 
         eschema = schema.eschema('CWUser')
         rels = sorted(str(r) for r in eschema.subject_relations())
-        self.assertListEqual(rels, ['created_by', 'creation_date', 'custom_workflow',
+        self.assertListEqual(rels, ['buddies', 'created_by', 'creation_date', 'custom_workflow',
                                     'cw_source', 'cwuri', 'eid',
                                     'evaluee', 'firstname', 'has_group_permission',
                                     'has_text', 'identity',
@@ -235,7 +235,7 @@
                                     'primary_email', 'surname', 'upassword',
                                     'use_email'])
         rels = sorted(r.type for r in eschema.object_relations())
-        self.assertListEqual(rels, ['bookmarked_by', 'created_by', 'for_user',
+        self.assertListEqual(rels, ['bookmarked_by', 'buddies', 'created_by', 'for_user',
                                      'identity', 'owned_by', 'wf_info_for'])
         rschema = schema.rschema('relation_type')
         properties = rschema.rdef('CWAttribute', 'CWRType')