[saem-devel] [PATCH 1 of 9] [org] Extract base class for organization units'tab displaying related objects

Sylvain Thenault sylvain.thenault at logilab.fr
Fri Jan 27 16:02:49 CET 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1485326073 -3600
#      Wed Jan 25 07:34:33 2017 +0100
# Node ID ecbc3fe4f55537de17afd5143cb78e9e4f138a0e
# Parent  62914be0eaed0563bf80c793d9cddb6e614663f7
[org] Extract base class for organization units'tab displaying related objects

from the tab displaying linked profile and vocabularies, for later usage to
display authority records as well.

Along the way fix a message including 'agent' instead of 'organization unit'

Related to #12572793

diff --git a/cubicweb_saem_ref/i18n/en.po b/cubicweb_saem_ref/i18n/en.po
--- a/cubicweb_saem_ref/i18n/en.po
+++ b/cubicweb_saem_ref/i18n/en.po
@@ -223,12 +223,12 @@ msgstr ""
 
 msgid "Vocabularies"
 msgstr ""
 
 msgid ""
-"You can't add profiles or concept schemes to agent without the 'deposit' "
-"role."
+"You can't add profiles or concept schemes to organization unit without the "
+"'deposit' role."
 msgstr ""
 
 msgid "You must be in an organization to access this functionnality."
 msgstr ""
 
diff --git a/cubicweb_saem_ref/i18n/fr.po b/cubicweb_saem_ref/i18n/fr.po
--- a/cubicweb_saem_ref/i18n/fr.po
+++ b/cubicweb_saem_ref/i18n/fr.po
@@ -229,14 +229,14 @@ msgstr "Voir"
 
 msgid "Vocabularies"
 msgstr "Vocabulaires"
 
 msgid ""
-"You can't add profiles or concept schemes to agent without the 'deposit' "
-"role."
+"You can't add profiles or concept schemes to organization unit without the "
+"'deposit' role."
 msgstr ""
-"Vous ne pouvez pas ajouter de vocabulaire ou de profil à un agent qui n'a "
+"Vous ne pouvez pas ajouter de vocabulaire ou de profil à une unité administrative qui n'a "
 "pas le rôle 'service versant'."
 
 msgid "You must be in an organization to access this functionnality."
 msgstr ""
 "Vous devez appartenir à une autorité administrative pour accéder à cette fonctionnalité."
diff --git a/cubicweb_saem_ref/views/org.py b/cubicweb_saem_ref/views/org.py
--- a/cubicweb_saem_ref/views/org.py
+++ b/cubicweb_saem_ref/views/org.py
@@ -163,39 +163,46 @@ class OrganizationUnitTabbedPrimaryView(
         _('saem_ref_deposit_ou_concepts_profiles'),
         _('saem_ref_ou_authorityrecords'),
     ]
 
 
-class DepositOrganizationUnitConceptsProfilesTab(SubviewsTabView):
-    """SEDA profiles and concept schemes used by an agent with role "deposit"."""
-    __regid__ = 'saem_ref_deposit_ou_concepts_profiles'
-    __select__ = EntityView.__select__ & is_instance('OrganizationUnit')
-    subvids = (
-        'saem.ou.schemes',
-        'saem.ou.profiles',
-    )
+class LinkedObjectsTab(SubviewsTabView):
+    """Abstract class for tab displaying objects related through non-composite relation.
+
+    Parametrized using:
+
+    * `required_role`, the archival role required to edit the relation ;
+
+    * `not_possible_msg`, the message to be displayed when the organisation unit doesn't have the
+      role ;
+
+    * `link_targets`, list of 3-uple (entity type, relation type, role) to be displayed.
+    """
+    __abstract__ = True
+    required_role = None
+    not_possible_msg = None
+    link_targets = ()
 
     def entity_call(self, entity):
         self.w(u'<div id="%s%s">' % (self.__regid__, entity.eid))
-        if (not entity.has_role('deposit')
-                and has_rel_perm('add', entity, 'use_profile', 'subject')):
-            msg = self._cw._("You can't add profiles or concept schemes to agent without the "
-                             "'deposit' role.")
+        rtype, role = self.link_targets[0][1:]
+        if (not entity.has_role(self.required_role)
+                and has_rel_perm('add', entity, rtype, role)):
+            msg = self._cw._(self.not_possible_msg)
             self.w('<div class="alert alert-warning">%s</div>' % msg)
         else:
             self.generate_add_button(entity)
-        super(DepositOrganizationUnitConceptsProfilesTab, self).entity_call(entity)
+        super(LinkedObjectsTab, self).entity_call(entity)
         self.w(u'</div>')
 
     def generate_add_button(self, entity):
         """Add html for the add button on the right hand corner of the tab, if the logged user may
         add some relation.
         """
         divid = "relatedentities%s" % entity.eid
         links = []
-        for etype, rtype, role in (('SEDAArchiveTransfer', 'use_profile', 'subject'),
-                                   ('ConceptScheme', 'related_concept_scheme', 'subject')):
+        for etype, rtype, role in self.link_targets:
             if not has_rel_perm('add', entity, rtype, role, target_etype=etype):
                 continue
             relation = '%s:%s:%s' % (rtype, etype, role)
             search_url = self._cw.build_url('ajax', fname='view', vid='search_related_entities',
                                             eid=entity.eid,
@@ -210,10 +217,25 @@ class DepositOrganizationUnitConceptsPro
             self.w(tags.div(id=divid, style='display: none'))
             self.w(dropdown_button(self._cw._('add'), links))
             self.w(tags.div(klass='clearfix'))
 
 
+class DepositOrganizationUnitConceptsProfilesTab(LinkedObjectsTab):
+    """SEDA profiles and concept schemes used by an agent with role "deposit"."""
+    __regid__ = 'saem_ref_deposit_ou_concepts_profiles'
+    __select__ = EntityView.__select__ & is_instance('OrganizationUnit')
+    required_role = 'deposit'
+    not_possible_msg = _("You can't add profiles or concept schemes to organization unit "
+                         "without the 'deposit' role.")
+    link_targets = [('SEDAArchiveTransfer', 'use_profile', 'subject'),
+                    ('ConceptScheme', 'related_concept_scheme', 'subject')]
+    subvids = (
+        'saem.ou.schemes',
+        'saem.ou.profiles',
+    )
+
+
 class OrganizationUnitSearchForRelatedEntitiesView(rwdg.SearchForRelatedEntitiesView):
     __select__ = (rwdg.SearchForRelatedEntitiesView.__select__
                   & (rwdg.edited_relation('use_profile')
                      | rwdg.edited_relation('related_concept_scheme')))
     has_creation_form = False


More information about the saem-devel mailing list