[views] Follow compound api changes and get proper actions for cloning
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 01 Mar 2017 12:16:50 +0100
changeset 2447 98f67b3a4b5c
parent 2446 68d41b878667
child 2448 b123f92ffe05
[views] Follow compound api changes and get proper actions for cloning of archive transfers and units.
cubicweb_seda/views/clone.py
dev-requirements.txt
test/test_views.py
--- a/cubicweb_seda/views/clone.py	Wed Mar 01 12:17:33 2017 +0100
+++ b/cubicweb_seda/views/clone.py	Wed Mar 01 12:16:50 2017 +0100
@@ -16,20 +16,17 @@
 """Views related to cloning of SEDA compound tree."""
 
 from cubicweb import uilib, _
-from cubicweb.predicates import match_form_params, is_instance
+from cubicweb.predicates import is_instance, match_form_params, score_entity
 from cubicweb.web import Redirect, controller
-from cubicweb.web.views import actions, uicfg
+from cubicweb.web.views import uicfg
 
 from cubicweb_compound.entities import copy_entity
+from cubicweb_compound import views as compound
 from cubes.relationwidget import views as rwdg
 
 from .widgets import configure_relation_widget
 
 
-# Hide copy action for SEDA profiles
-actions.CopyAction.__select__ = actions.CopyAction.__select__ & ~is_instance('SEDAArchiveTransfer')
-
-
 afs = uicfg.autoform_section
 pvs = uicfg.primaryview_section
 
@@ -100,3 +97,17 @@
         rql = ('Any X,MD ORDERBY MD DESC WHERE X is %s, X modification_date MD, '
                'NOT X seda_archive_unit P, NOT X eid %%(target)s') % tetype
         return self._cw.execute(rql, {'target': target})
+
+
+class SEDAArchiveTransferCloneAction(compound.CloneAction):
+    __select__ = compound.CloneAction.__select__ & is_instance('SEDAArchiveTransfer')
+
+
+class SEDAArchiveUnitCloneAction(compound.CloneAction):
+    __select__ = (compound.CloneAction.__select__ & is_instance('SEDAArchiveUnit')
+                  & score_entity(lambda x: x.cw_adapt_to('IContained').container is None))
+
+    def url(self):
+        entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
+        return entity.absolute_url(vid='copy', unit_type='unit_content',
+                                   **compound.linkto_clone_url_params(entity))
--- a/dev-requirements.txt	Wed Mar 01 12:17:33 2017 +0100
+++ b/dev-requirements.txt	Wed Mar 01 12:16:50 2017 +0100
@@ -1,1 +1,2 @@
 pytest
+http://hg.logilab.org/master/cubes/compound/archive/tip.tar.bz2#egg=cubicweb-compound
--- a/test/test_views.py	Wed Mar 01 12:17:33 2017 +0100
+++ b/test/test_views.py	Wed Mar 01 12:16:50 2017 +0100
@@ -440,6 +440,33 @@
                              [])
 
 
+class CloneActionsTC(CubicWebTC):
+
+    def test_archive_transfer(self):
+        with self.admin_access.web_request() as req:
+            transfer = req.create_entity('SEDAArchiveTransfer', title=u'test')
+            unit = testutils.create_archive_unit(transfer)[0]
+            req.cnx.commit()
+
+            actions = self.pactionsdict(req, transfer.as_rset())
+            self.assertIn(clone.SEDAArchiveTransferCloneAction, actions['mainactions'])
+
+            actions = self.pactionsdict(req, unit.as_rset())
+            self.assertNotIn(clone.SEDAArchiveUnitCloneAction, actions['mainactions'])
+
+    def test_unit_component(self):
+        with self.admin_access.web_request() as req:
+            unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(None, cnx=req)
+            subunit = testutils.create_archive_unit(unit_alt_seq)[0]
+            req.cnx.commit()
+
+            actions = self.pactionsdict(req, unit.as_rset())
+            self.assertIn(clone.SEDAArchiveUnitCloneAction, actions['mainactions'])
+
+            actions = self.pactionsdict(req, subunit.as_rset())
+            self.assertNotIn(clone.SEDAArchiveUnitCloneAction, actions['mainactions'])
+
+
 class CloneImportTC(CubicWebTC):
     """Tests for 'seda.doimport' controller (called from JavaScript)."""