[PATCH 1 of 9 seda] [views] Move action to import an archive unit to the 'add' contextual button

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


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1488317783 -3600
#      Tue Feb 28 22:36:23 2017 +0100
# Node ID 200a7c5659f83a7314fddb53ddda501c8eeb4c17
# Parent  3315ecaad7608f53718bf92ab017d622f1d6c0de
[views] Move action to import an archive unit to the 'add' contextual button

Closes #16070787

diff --git a/cubicweb_seda/i18n/en.po b/cubicweb_seda/i18n/en.po
--- a/cubicweb_seda/i18n/en.po
+++ b/cubicweb_seda/i18n/en.po
@@ -4638,12 +4638,12 @@ msgstr ""
 
 msgctxt "SEDAFormatLitteral"
 msgid "format_litteral"
 msgstr ""
 
-msgid "import menu"
-msgstr "import"
+msgid "import_unit"
+msgstr ""
 
 msgid "juridictional"
 msgstr ""
 
 msgctxt "SEDAJuridictional"
diff --git a/cubicweb_seda/i18n/fr.po b/cubicweb_seda/i18n/fr.po
--- a/cubicweb_seda/i18n/fr.po
+++ b/cubicweb_seda/i18n/fr.po
@@ -4657,12 +4657,12 @@ msgstr "valeur"
 
 msgctxt "SEDAFormatLitteral"
 msgid "format_litteral"
 msgstr ""
 
-msgid "import menu"
-msgstr "importer"
+msgid "import_unit"
+msgstr "unité d'archive du référentiel"
 
 msgid "juridictional"
 msgstr "juridication administrative"
 
 msgctxt "SEDAJuridictional"
diff --git a/cubicweb_seda/views/archivetransfer.py b/cubicweb_seda/views/archivetransfer.py
--- a/cubicweb_seda/views/archivetransfer.py
+++ b/cubicweb_seda/views/archivetransfer.py
@@ -177,10 +177,18 @@ class ArchiveTransferArchiveUnitsTab(vie
     rtype_role_targets = [('seda_archive_unit', 'object', None)]
 
     _('creating SEDAArchiveUnit (SEDAArchiveUnit seda_archive_unit '
       'SEDAArchiveTransfer %(linkto)s)')
 
+    def display_add_button(self, entity):
+        rtype_roles = [(rtype, role) for rtype, role, _ in self.rtype_role_targets]
+        links = viewlib.add_links_from_rtype_roles(entity, rtype_roles, self.url_params(entity))
+        if links:
+            import_url = clone.import_unit_url(self._cw, entity)
+            links.append(tags.a(self._cw._('import_unit'), href=import_url))
+        viewlib.display_add_button(self.w, links, self._cw.__('add'))
+
     def url_params(self, entity):
         return {'__redirectparams': 'tab=' + self.__regid__,
                 'unit_type': 'unit_content'}
 
 
diff --git a/cubicweb_seda/views/archiveunit.py b/cubicweb_seda/views/archiveunit.py
--- a/cubicweb_seda/views/archiveunit.py
+++ b/cubicweb_seda/views/archiveunit.py
@@ -966,10 +966,13 @@ class ArchiveUnitArchiveUnitsTab(Archive
 
     def display_add_button(self, entity):
         urlparams = self.url_params(entity)
         links = add_links_with_custom_arguments(entity, self.rtype, self.role, urlparams,
                                                 self.unit_custom_arguments)
+        if links:
+            import_url = clone.import_unit_url(self._cw, parent_archive_unit(entity))
+            links.append(tags.a(self._cw._('import_unit'), href=import_url))
         viewlib.display_add_button(self.w, links, self._cw.__('add'))
 
     def display_subobjects_list(self, entity):
         rset = entity.related(self.rtype, self.role)
         if rset:
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,12 +14,12 @@
 # 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, has_permission, one_line_rset, is_instance
-from cubicweb.web import Redirect, action, controller
+from cubicweb.predicates import match_form_params, is_instance
+from cubicweb.web import Redirect, controller
 from cubicweb.web.views import actions, uicfg
 
 from cubes.compound.entities import copy_entity
 from cubes.relationwidget import views as rwdg
 
@@ -38,51 +38,25 @@ afs.tag_subject_of(('*', 'clone_of', '*'
 afs.tag_object_of(('*', 'clone_of', '*'), 'main', 'hidden')
 pvs.tag_subject_of(('*', 'clone_of', '*'), 'relations')
 pvs.tag_object_of(('*', 'clone_of', '*'), 'relations')
 
 
-class ImportAction(action.Action):
-    __abstract__ = True
-
-    __select__ = (action.Action.__select__
-                  & one_line_rset()
-                  & has_permission('update'))
-
-    category = 'moreactions'
-    submenu = _('import menu')
-    etype = None  # to be specified in concrete classes
-
-    @property
-    def title(self):
-        return self._cw._(self.etype).lower()
-
-    def fill_menu(self, box, menu):
-        # when there is only one item in the sub-menu, replace the sub-menu by
-        # item's title prefixed by 'add'
-        menu.label_prefix = self._cw._('import menu')
-        super(ImportAction, self).fill_menu(box, menu)
-
-    def url(self):
-        self._cw.add_js('cubes.editionext.js')
-        entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
-        if entity.cw_etype == 'SEDAArchiveTransfer':
-            root = entity
-        else:
-            root = entity.cw_adapt_to('IContained').container or entity
-        relation = 'clone_of:%s:subject' % self.etype
-        search_url = self._cw.build_url('ajax', fname='view', vid='search_related_entities',
-                                        __modal=1, multiple='1', relation=relation,
-                                        etype=root.cw_etype, target=root.eid)
-        title = self._cw._('Search entity to import')
-        return configure_relation_widget(self._cw, _import_div_id(entity), search_url, title,
-                                         True, uilib.js.editext.buildSedaImportValidate(entity.eid))
-
-
-class ImportSEDAArchiveUnitAction(ImportAction):
-    __regid__ = 'seda.import.archiveunit'
-    __select__ = ImportAction.__select__ & is_instance('SEDAArchiveTransfer', 'SEDAArchiveUnit')
-    etype = 'SEDAArchiveUnit'
+def import_unit_url(req, entity):
+    """Return an URL to display the relation widget to search for an archive unit to import.
+    """
+    req.add_js('cubes.editionext.js')
+    if entity.cw_etype == 'SEDAArchiveTransfer':
+        root = entity
+    else:
+        root = entity.cw_adapt_to('IContained').container or entity
+    search_url = req.build_url('ajax', fname='view', vid='search_related_entities',
+                               __modal='1', multiple='1',
+                               relation='clone_of:SEDAArchiveUnit:subject',
+                               etype=root.cw_etype, target=root.eid)
+    title = req._('Search entity to import')
+    return configure_relation_widget(req, _import_div_id(entity), search_url, title,
+                                     True, uilib.js.editext.buildSedaImportValidate(entity.eid))
 
 
 class DoImportView(controller.Controller):
     __regid__ = 'seda.doimport'
     __select__ = match_form_params('eid', 'cloned')
diff --git a/test/test_views.py b/test/test_views.py
--- a/test/test_views.py
+++ b/test/test_views.py
@@ -438,28 +438,10 @@ class SimplifiedFormsTC(CubicWebTC):
                                             parent=unit.eid)
             self.assertEqual(entity_fields(form.fields),
                              [])
 
 
-class CloneActionsTC(CubicWebTC):
-
-    def test_actions_notype_to_import(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.ImportSEDAArchiveUnitAction, actions['moreactions'])
-            actions = self.pactionsdict(req, unit.as_rset())
-            self.assertIn(clone.ImportSEDAArchiveUnitAction, actions['moreactions'])
-        with self.new_access('anon').web_request() as req:
-            profile = req.entity_from_eid(transfer.eid)
-            actions = self.pactionsdict(req, profile.as_rset())
-            self.assertNotIn(clone.ImportSEDAArchiveUnitAction, actions['moreactions'])
-
-
 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