[PATCH 3 of 3 sherpa V2] Turn components backported from saem_ref into actions

Sylvain Thenault sylvain.thenault at logilab.fr
Fri Feb 24 09:49:19 CET 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1487925540 -3600
#      Fri Feb 24 09:39:00 2017 +0100
# Node ID 8b4aacdfe387fb28afc03f36083ea3ab3c2eed5b
# Parent  bfaa0c10092d1aced8d64fe76f2ce5daa92f9bf2
Turn components backported from saem_ref into actions

to be displayed in the hamberger menu.

diff --git a/cubicweb_sherpa/views/components.py b/cubicweb_sherpa/views/components.py
--- a/cubicweb_sherpa/views/components.py
+++ b/cubicweb_sherpa/views/components.py
@@ -13,90 +13,71 @@
 #
 # 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/>.
 """cubicweb-sherpa components, originaly copied from the saem_ref cube."""
 
-from cubicweb import _, tags
+from cubicweb import _
 from cubicweb.predicates import multi_lines_rset, has_permission, is_instance, non_final_entity
-from cubicweb.web import component
+from cubicweb.web import action, component
 
 from . import jinja_render
 
 
-def add_etype_link(req, etype, text=u'', klass='icon-plus-circled pull-right',
-                   **urlparams):
-    """Return an HTML link to add an entity of type 'etype'."""
-    vreg = req.vreg
-    eschema = vreg.schema.eschema(etype)
-    if eschema.has_perm(req, 'add'):
-        url = vreg['etypes'].etype_class(etype).cw_create_url(req, **urlparams)
-        return tags.a(text, href=url, klass=klass,
-                      title=req.__('New %s' % etype))
-    return u''
+class AddEntityAction(action.Action):
+    """Action with 'add' link to be displayed in 'same etype' views usually 'SameETypeListView'.
+    """
+    __regid__ = 'sherpa.add_entity'
+    __select__ = (multi_lines_rset()
+                  & has_permission('add')
+                  & is_instance('AuthorityRecord', 'ConceptScheme',
+                                'SEDAArchiveTransfer', 'SEDAArchiveUnit'))
+    extra_kwargs = {'SEDAArchiveUnit': {'unit_type': 'unit_content'}}
+
+    @property
+    def title(self):
+        etype = self.cw_rset.description[0][0]
+        return self._cw.__('New %s' % etype)
+
+    def url(self):
+        etype = self.cw_rset.description[0][0]
+        urlparams = self.extra_kwargs.get(etype, {})
+        return self._cw.vreg['etypes'].etype_class(etype).cw_create_url(self._cw, **urlparams)
 
 
-def import_etype_link(req, etype, url):
-    """Return an HTML link to the view that may be used to import an entity of type `etype`.
-    """
-    eschema = req.vreg.schema.eschema(etype)
-    if eschema.has_perm(req, 'add'):
-        return tags.a(u'', href=url, klass='icon-upload pull-right',
-                      title=req.__('Import %s' % etype))
-    return u''
-
-
-class AddEntityComponent(component.CtxComponent):
-    """Component with 'add' link to be displayed in 'same etype' views usually 'SameETypeListView'.
-    """
-    __regid__ = 'sherpa.add_entity'
-    __select__ = (component.CtxComponent.__select__ & multi_lines_rset() & has_permission('add') &
-                  is_instance('AuthorityRecord', 'ConceptScheme',
-                              'SEDAArchiveTransfer', 'SEDAArchiveUnit'))
-    context = 'navtop'
-    extra_kwargs = {'SEDAArchiveUnit': {'unit_type': 'unit_content'}}
-
-    def render_body(self, w):
-        etype = self.cw_rset.description[0][0]
-        w(add_etype_link(self._cw, etype, **self.extra_kwargs.get(etype, {})))
-
-
-class ImportEntityComponent(component.CtxComponent):
-    """Component with 'import' link to be displayed in 'same etype' views usually
+class ImportEntityAction(action.Action):
+    """Action with 'import' link to be displayed in 'same etype' views usually
     'SameETypeListView'.
 
     Concret class should fill the `import_vid` class attribute and add a proper `is_instance`
     selector.
     """
     __abstract__ = True
     __regid__ = 'sherpa.import_entity'
-    __select__ = component.CtxComponent.__select__ & multi_lines_rset() & has_permission('add')
-    import_url = None  # URL of the view that may be used to import data
-    context = 'navtop'
+    __select__ = multi_lines_rset() & has_permission('add')
 
-    def render_body(self, w):
+    @property
+    def title(self):
         etype = self.cw_rset.description[0][0]
-        w(import_etype_link(self._cw, etype, self.import_url))
+        return self._cw.__('Import %s' % etype)
 
 
-class EACImportComponent(ImportEntityComponent):
-    """Component with a link to import an authority record from an EAC-CPF file."""
-    __select__ = (ImportEntityComponent.__select__
+class EACImportAction(ImportEntityAction):
+    """Action with a link to import an authority record from an EAC-CPF file."""
+    __select__ = (ImportEntityAction.__select__
                   & is_instance('AuthorityRecord'))
-    _('Import AuthorityRecord')  # generate message used by the import component
+    _('Import AuthorityRecord')  # generate message used by the import action
 
-    @property
-    def import_url(self):
+    def url(self):
         return self._cw.build_url('view', vid='eac.import')
 
 
-class SKOSImportComponent(ImportEntityComponent):
-    """Component with a link to import a concept scheme from a SKOS file."""
-    __select__ = ImportEntityComponent.__select__ & is_instance('ConceptScheme')
-    _('Import ConceptScheme')  # generate message used by the import component
+class SKOSImportAction(ImportEntityAction):
+    """Action with a link to import a concept scheme from a SKOS file."""
+    __select__ = ImportEntityAction.__select__ & is_instance('ConceptScheme')
+    _('Import ConceptScheme')  # generate message used by the import action
 
-    @property
-    def import_url(self):
+    def url(self):
         return self._cw.build_url('add/skossource')
 
 
 class ActionsComponent(component.CtxComponent):
     """Contextual component replacing the actions box (`cubicweb.web.views.boxes.EditBox`)."""


More information about the saem-devel mailing list