Turn components backported from saem_ref into actions
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 24 Feb 2017 09:39:00 +0100
changeset 164 8f2a7cf7a620
parent 162 243db7c6410e
child 169 efcc1ab69722
Turn components backported from saem_ref into actions to be displayed in the contextual hamburger menu.
cubicweb_sherpa/i18n/en.po
cubicweb_sherpa/i18n/fr.po
cubicweb_sherpa/views/components.py
--- a/cubicweb_sherpa/i18n/en.po	Fri Feb 24 09:18:33 2017 +0100
+++ b/cubicweb_sherpa/i18n/en.po	Fri Feb 24 09:39:00 2017 +0100
@@ -7,25 +7,19 @@
 "Generated-By: pygettext.py 1.5\n"
 "Plural-Forms: nplurals=2; plural=(n > 1);\n"
 
-msgid "Import AuthorityRecord"
+msgid "ctxcomponents_sherpa.actions"
 msgstr ""
 
-msgid "Import ConceptScheme"
-msgstr ""
-
-msgid "ctxcomponents_sherpa.add_entity"
-msgstr ""
-
-# schema pot file, generated on 2017-02-23 09:38:25
+# schema pot file, generated on 2017-02-24 09:01:15
 #
 # singular and plural forms for each entity type
 # subject and object forms for each relation type
 # (no object form for final or symmetric relation types)
-msgid "ctxcomponents_sherpa.add_entity_description"
+msgid "ctxcomponents_sherpa.actions_description"
 msgstr ""
 
-msgid "ctxcomponents_sherpa.import_entity"
+msgid "import AuthorityRecord"
 msgstr ""
 
-msgid "ctxcomponents_sherpa.import_entity_description"
+msgid "import ConceptScheme"
 msgstr ""
--- a/cubicweb_sherpa/i18n/fr.po	Fri Feb 24 09:18:33 2017 +0100
+++ b/cubicweb_sherpa/i18n/fr.po	Fri Feb 24 09:39:00 2017 +0100
@@ -7,25 +7,19 @@
 "Generated-By: pygettext.py 1.5\n"
 "Plural-Forms: nplurals=2; plural=(n > 1);\n"
 
-msgid "Import AuthorityRecord"
-msgstr "Importer une notice d'autorité"
-
-msgid "Import ConceptScheme"
-msgstr "Importer un vocabulaire contrôlé"
-
-msgid "ctxcomponents_sherpa.add_entity"
+msgid "ctxcomponents_sherpa.actions"
 msgstr ""
 
-# schema pot file, generated on 2017-02-23 09:38:25
+# schema pot file, generated on 2017-02-24 09:01:15
 #
 # singular and plural forms for each entity type
 # subject and object forms for each relation type
 # (no object form for final or symmetric relation types)
-msgid "ctxcomponents_sherpa.add_entity_description"
+msgid "ctxcomponents_sherpa.actions_description"
 msgstr ""
 
-msgid "ctxcomponents_sherpa.import_entity"
-msgstr ""
+msgid "import AuthorityRecord"
+msgstr "importer une notice d'autorité"
 
-msgid "ctxcomponents_sherpa.import_entity_description"
-msgstr ""
+msgid "import ConceptScheme"
+msgstr "importer un vocabulaire contrôlé"
--- a/cubicweb_sherpa/views/components.py	Fri Feb 24 09:18:33 2017 +0100
+++ b/cubicweb_sherpa/views/components.py	Fri Feb 24 09:39:00 2017 +0100
@@ -15,52 +15,36 @@
 # 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).lower()
+
+    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`
@@ -68,33 +52,30 @@
     """
     __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')