Commit dcf682e8 authored by Adrien Di Mascio's avatar Adrien Di Mascio
Browse files

[entities] skip_copy_for should be considered for object relations too (closes #1857474)

parent 1541d9e6b242
......@@ -96,11 +96,12 @@ class Entity(AppObject):
If None is specified, the first non-meta attribute will
be used
:type skip_copy_for: list
:cvar skip_copy_for: a list of relations that should be skipped when copying
this kind of entity. Note that some relations such
as composite relations or relations that have '?1' as object
cardinality are always skipped.
:type cw_skip_copy_for: list
:cvar cw_skip_copy_for: a list of couples (rtype, role) for each relation
that should be skipped when copying
this kind of entity. Note that some relations such
as composite relations or relations that have '?1' as object
cardinality are always skipped.
"""
__registry__ = 'etypes'
__select__ = yes()
......@@ -108,7 +109,8 @@ class Entity(AppObject):
# class attributes that must be set in class definition
rest_attr = None
fetch_attrs = None
skip_copy_for = ('in_state',) # XXX turn into a set
skip_copy_for = () # bw compat (< 3.14), use cw_skip_copy_for instead
cw_skip_copy_for = [('in_state', 'subject')]
# class attributes set automatically at registration time
e_schema = None
......@@ -542,13 +544,22 @@ class Entity(AppObject):
"""
assert self.has_eid()
execute = self._cw.execute
skip_copy_for = {'subject': set(), 'object': set()}
for rtype in self.skip_copy_for:
skip_copy_for['subject'].add(rtype)
warn('[3.14] skip_copy_for on entity classes (%s) is deprecated, '
'use cw_skip_for instead with list of couples (rtype, role)' % self.__regid__,
DeprecationWarning)
for rtype, role in self.cw_skip_copy_for:
assert role in ('subject', 'object'), role
skip_copy_for[role].add(rtype)
for rschema in self.e_schema.subject_relations():
if rschema.final or rschema.meta:
continue
# skip already defined relations
if getattr(self, rschema.type):
continue
if rschema.type in self.skip_copy_for:
if rschema.type in skip_copy_for['subject']:
continue
# skip composite relation
rdef = self.e_schema.rdef(rschema)
......@@ -568,6 +579,8 @@ class Entity(AppObject):
# skip already defined relations
if self.related(rschema.type, 'object'):
continue
if rschema.type in skip_copy_for['object']:
continue
rdef = self.e_schema.rdef(rschema, 'object')
# skip composite relation
if rdef.composite:
......
......@@ -365,6 +365,43 @@ class EditControllerTC(CubicWebTC):
self.assertEqual(path, 'view')
self.assertIn('_cwmsgid', params)
def test_simple_copy(self):
req = self.request()
blog = req.create_entity('Blog', title=u'my-blog')
blogentry = req.create_entity('BlogEntry', title=u'entry1',
content=u'content1', entry_of=blog)
req = self.request()
req.form = {'__maineid' : 'X', 'eid': 'X',
'__cloned_eid:X': blogentry.eid, '__type:X': 'BlogEntry',
'_cw_entity_fields:X': 'title-subject,content-subject',
'title-subject:X': u'entry1-copy',
'content-subject:X': u'content1',
}
self.expect_redirect_publish(req, 'edit')
blogentry2 = req.find_one_entity('BlogEntry', title=u'entry1-copy')
self.assertEqual(blogentry2.entry_of[0].eid, blog.eid)
def test_skip_copy_for(self):
req = self.request()
blog = req.create_entity('Blog', title=u'my-blog')
blogentry = req.create_entity('BlogEntry', title=u'entry1',
content=u'content1', entry_of=blog)
blogentry.__class__.cw_skip_copy_for = [('entry_of', 'subject')]
try:
req = self.request()
req.form = {'__maineid' : 'X', 'eid': 'X',
'__cloned_eid:X': blogentry.eid, '__type:X': 'BlogEntry',
'_cw_entity_fields:X': 'title-subject,content-subject',
'title-subject:X': u'entry1-copy',
'content-subject:X': u'content1',
}
self.expect_redirect_publish(req, 'edit')
blogentry2 = req.find_one_entity('BlogEntry', title=u'entry1-copy')
# entry_of should not be copied
self.assertEqual(len(blogentry2.entry_of), 0)
finally:
blogentry.__class__.cw_skip_copy_for = []
def test_nonregr_eetype_etype_editing(self):
"""non-regression test checking that a manager user can edit a CWEType entity
"""
......
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