Factorize display of 'add button' in a new display_add_button function
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 19 Jan 2017 19:28:05 +0100
changeset 2308 6ced28736579
parent 2307 657430e71449
child 2309 b4e5981f7aad
Factorize display of 'add button' in a new display_add_button function
views/archiveunit.py
views/viewlib.py
--- a/views/archiveunit.py	Thu Jan 19 19:17:29 2017 +0100
+++ b/views/archiveunit.py	Thu Jan 19 19:28:05 2017 +0100
@@ -32,7 +32,7 @@
 from ..xsd import un_camel_case
 from ..entities import full_seda2_profile, simplified_profile, parent_and_container
 from ..entities.itree import parent_archive_unit
-from . import (CONTENT_ETYPE, add_subobject_link, dropdown_button,
+from . import (CONTENT_ETYPE, add_subobject_link,
                rtags_from_xsd_element, rtags_from_rtype_role_targets, copy_rtag, has_rel_perm)
 from . import clone, viewlib, widgets
 from . import uicfg as sedauicfg  # noqa - ensure those rules are defined first
@@ -897,10 +897,7 @@
                                       msg=self._cw._(label), klass='')
             if link:
                 links.append(link)
-        if links:
-            add_link = dropdown_button(entity._cw.__('add'), links)
-            self.w(add_link)
-            self.w(tags.div(klass='clearfix'))
+        viewlib.display_add_button(self.w, links, self._cw.__('add'))
         rset = seq.related(self.rtype, self.role)
         if rset:
             subvid = 'seda.listitem'
@@ -943,23 +940,18 @@
         params = self.url_params(entity)
         params['referenced_by'] = text_type(entity.eid)
         transfer = entity.container[0]
-        button = None
+        links = []
         if transfer.cw_etype == 'SEDAArchiveTransfer':
             links = viewlib.add_links_from_rtype_roles(transfer, rtype_roles, params)
-            if links:
-                button = dropdown_button(entity._cw._('add'), links)
+            label = self._cw.__('add')
         else:
             if has_rel_perm('add', entity, 'seda_data_object_reference', 'object'):
                 vreg = self._cw.vreg
                 url = vreg['etypes'].etype_class('SEDABinaryDataObject').cw_create_url(
                     self._cw, **params)
-                msg = self._cw.__('seda_binary_data_object_object')
-                link = tags.a(msg, href=url, title=self._cw.__('New SEDABinaryDataObject'))
-                button = dropdown_button(entity._cw._(msg), [link])
-        if button:
-            # No button if user cannot add any relation.
-            self.w(button)
-            self.w(tags.div(klass='clearfix'))
+                label = self._cw.__('seda_binary_data_object_object')
+                links = [tags.a(label, href=url, title=self._cw.__('New SEDABinaryDataObject'))]
+        viewlib.display_add_button(self.w, links, self._cw.__('add'))
 
     def display_subobjects_list(self, entity):
         rset = self._cw.execute(
--- a/views/viewlib.py	Thu Jan 19 19:17:29 2017 +0100
+++ b/views/viewlib.py	Thu Jan 19 19:28:05 2017 +0100
@@ -36,6 +36,13 @@
     return [l for l in links if l]
 
 
+def display_add_button(w, links, label):
+    if links:
+        # No links if user cannot add any relation.
+        w(dropdown_button(label, links))
+        w(tags.div(klass='clearfix'))
+
+
 class XSDMetaEntityView(EntityView):
     __regid__ = 'seda.xsdmeta'
 
@@ -254,10 +261,7 @@
     def display_add_button(self, entity):
         rtype_roles = [(rtype, role) for rtype, role, _ in self.rtype_role_targets]
         links = add_links_from_rtype_roles(entity, rtype_roles, self.url_params(entity))
-        if links:
-            # No links if user cannot add any relation.
-            self.w(dropdown_button(entity._cw._('add'), links))
-            self.w(tags.div(klass='clearfix'))
+        display_add_button(self.w, links, self._cw.__('add'))
 
     def display_subobjects_list(self, entity):
         for rtype, role, target in self.rtype_role_targets: