Commit b58ae17c authored by Denis Laxalde's avatar Denis Laxalde
Browse files

[entity] Exclude computed relations from Entity.copy_relations

Closes #12481591.
parent 0119ba327117
......@@ -783,7 +783,7 @@ class Entity(AppObject):
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 @@ class Entity(AppObject):
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'):
......
......@@ -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.schema import (WorkflowableEntityType,
from cubicweb import _
class buddies(ComputedRelation):
rule = 'S in_group G, O in_group G'
class Personne(EntityType):
nom = String(required=True)
prenom = String()
......
......@@ -138,6 +138,22 @@ class EntityTC(CubicWebTC):
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)
......
......@@ -178,7 +178,7 @@ class SchemaReaderClassTest(TestCase):
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 @@ class SchemaReaderClassTest(TestCase):
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 @@ class SchemaReaderClassTest(TestCase):
'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')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment