[PATCH 3 of 8 compound V2] Make CloneAction abstract

Sylvain Thenault sylvain.thenault at logilab.fr
Fri Mar 3 09:49:02 CET 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1488530671 -3600
#      Fri Mar 03 09:44:31 2017 +0100
# Node ID 41dcce2866f1932b1b2c231494d6832572ef9852
# Parent  0ccdfb18031a8b063dbf9b96af64d19db2bfd57b
Make CloneAction abstract

we don't want it to appear auto-magically for every IClonable entities

diff --git a/test/data/entities.py b/test/data/entities.py
--- a/test/data/entities.py
+++ b/test/data/entities.py
@@ -1,9 +1,10 @@
-from cubicweb.predicates import has_related_entities
+from cubicweb.predicates import has_related_entities, is_instance
 
 from cubes.compound.entities import (IClonableAdapter, IContained, IContainer,
                                      structure_def)
+from cubes.compound.views import CloneAction
 
 
 def agent_structure_def(schema):
     return structure_def(schema, 'Agent').items()
 
@@ -20,10 +21,14 @@ class AgentInGroupIClonableAdapter(IClon
                   & has_related_entities('member', role='object'))
     rtype = 'clone_of'
     follow_relations = [('member', 'object')]
 
 
+class AgentCloneAction(CloneAction):
+    __select__ = CloneAction.__select__ & is_instance('Agent')
+
+
 def registration_callback(vreg):
     vreg.register_all(globals().values(), __name__)
     vreg.register(IContainer.build_class('Agent'))
     for etype, parent_relations in agent_structure_def(vreg.schema):
         IContained.register_class(vreg, etype, parent_relations)
diff --git a/views.py b/views.py
--- a/views.py
+++ b/views.py
@@ -24,22 +24,31 @@ from cubicweb.web.views import actions, 
 
 from cubes.compound.entities import copy_entity
 
 
 class CloneAction(actions.CopyAction):
-    """Just a copy action (copy is handled by edit controller below) named 'clone'."""
-    __select__ = (actions.CopyAction.__select__ & one_line_rset() &
-                  adaptable('IClonable') & has_permission('add'))
+    """Abstract clone action of ICloneable entities.
+
+    Simply inherit from it with a specific selector if you want to activate it.
+    The action will link to the copy form with shallow copy message disabled and
+    linkto information to rely on the clone hook for the actual cloning.
+    """
+    __abstract__ = True
+    __select__ = (actions.CopyAction.__select__ & one_line_rset()
+                  & has_permission('add')
+                  & adaptable('IClonable'))
     title = _('clone')
 
     def url(self):
         entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
         iclone = entity.cw_adapt_to('IClonable')
         linkto = '%s:%s:%s' % (iclone.rtype, entity.eid, neg_role(iclone.role))
         return entity.absolute_url(vid='copy', __linkto=linkto)
 
 
+# In any case IClonable entities want default copy disabled since it wont handle
+# composite relations by default.
 actions.CopyAction.__select__ &= ~adaptable('IClonable')
 
 
 class CloneController(Controller):
     """Controller handling cloning of the original entity (with `eid` passed


More information about the saem-devel mailing list