[entities] skip_copy_for should be considered for object relations too (closes #1857474)
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Tue, 26 Jul 2011 19:34:43 +0200
changeset 7702 73cadb5d0097
parent 7696 1541d9e6b242
child 7721 d313666c171e
[entities] skip_copy_for should be considered for object relations too (closes #1857474)
entity.py
web/test/unittest_views_basecontrollers.py
--- a/entity.py	Tue Jul 26 16:33:52 2011 +0200
+++ b/entity.py	Tue Jul 26 19:34:43 2011 +0200
@@ -96,11 +96,12 @@
                     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 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 @@
         """
         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 @@
             # 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:
--- a/web/test/unittest_views_basecontrollers.py	Tue Jul 26 16:33:52 2011 +0200
+++ b/web/test/unittest_views_basecontrollers.py	Tue Jul 26 19:34:43 2011 +0200
@@ -365,6 +365,43 @@
         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
         """