[PATCH 8 of 9 seda] [views] Follow compound api changes and get proper actions for cloning

Sylvain Thenault sylvain.thenault at logilab.fr
Wed Mar 1 12:31:50 CET 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1488367010 -3600
#      Wed Mar 01 12:16:50 2017 +0100
# Node ID 9920bc691cc16afe90daa3cd3b7937bcab9072e5
# Parent  b7f58caefba12c946120aff0c874778f1a4a34b5
[views] Follow compound api changes and get proper actions for cloning

of archive transfers and units.

diff --git a/cubicweb_seda/views/clone.py b/cubicweb_seda/views/clone.py
--- a/cubicweb_seda/views/clone.py
+++ b/cubicweb_seda/views/clone.py
@@ -14,24 +14,21 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 """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 cubes.compound import views as compound
 from cubes.compound.entities import copy_entity
 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
 
 # clone relation
 afs.tag_subject_of(('*', 'clone_of', '*'), 'main', 'hidden')
@@ -98,5 +95,19 @@ class SearchForTargetToImportView(rwdg.S
         tetype = self._cw.form['relation'].split(':')[1]
         target = int(self._cw.form['target'])
         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))
diff --git a/dev-requirements.txt b/dev-requirements.txt
--- a/dev-requirements.txt
+++ b/dev-requirements.txt
@@ -1,1 +1,2 @@
 pytest
+http://hg.logilab.org/review/cubes/compound/archive/fe2aeaad1cae.tar.bz2#egg=cubicweb-compound
diff --git a/test/test_views.py b/test/test_views.py
--- a/test/test_views.py
+++ b/test/test_views.py
@@ -438,10 +438,37 @@ class SimplifiedFormsTC(CubicWebTC):
                                             parent=unit.eid)
             self.assertEqual(entity_fields(form.fields),
                              [])
 
 
+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)."""
 
     def setup_database(self):
         with self.admin_access.cnx() as cnx:


More information about the saem-devel mailing list