Drop add_subobjects_button utility function
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 19 Jan 2017 19:17:29 +0100
changeset 2307 657430e71449
parent 2306 eae2ee93bae4
child 2308 6ced28736579
Drop add_subobjects_button utility function it had two responsability: computing links to put in the dropdown and actually generating the HTML. This makes it unusable when one want to generate link in another fashion. Rather extract a new `add_links_from_rtypes_roles` to generate links and inline the HTML generation in client code.
views/__init__.py
views/archiveunit.py
views/viewlib.py
--- a/views/__init__.py	Thu Jan 19 11:07:01 2017 +0100
+++ b/views/__init__.py	Thu Jan 19 19:17:29 2017 +0100
@@ -104,22 +104,6 @@
     return rsection, display_ctrl
 
 
-def add_subobjects_button(entity, rtype_roles, extraurlparams, msg='add'):
-    """Return an HTML dropdown button to add subobjects linked to `entity` through relations defined
-    in `rtype_roles` or an empty string if the user doesn't have the permission to add any of them.
-
-    `rtype_roles` is list of 2-uples `(rtype, role)`.
-    """
-    links = [add_subobject_link(entity, rtype, role, extraurlparams, klass='')
-             for rtype, role in rtype_roles]
-    links = [l for l in links if l]
-    if links:
-        # No links if user cannot add any relation.
-        return dropdown_button(entity._cw._(msg), links)
-    else:
-        return u''
-
-
 def add_subobject_link(entity, rtype, role, extraurlparams, msg=None, klass=None):
     """Return a HTML link to add a subobject linked to `entity` through `rtype` relation where
     entity has `role`.
--- a/views/archiveunit.py	Thu Jan 19 11:07:01 2017 +0100
+++ b/views/archiveunit.py	Thu Jan 19 19:17:29 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, add_subobjects_button, dropdown_button,
+from . import (CONTENT_ETYPE, add_subobject_link, dropdown_button,
                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
@@ -943,8 +943,11 @@
         params = self.url_params(entity)
         params['referenced_by'] = text_type(entity.eid)
         transfer = entity.container[0]
+        button = None
         if transfer.cw_etype == 'SEDAArchiveTransfer':
-            button = add_subobjects_button(transfer, rtype_roles, params)
+            links = viewlib.add_links_from_rtype_roles(transfer, rtype_roles, params)
+            if links:
+                button = dropdown_button(entity._cw._('add'), links)
         else:
             if has_rel_perm('add', entity, 'seda_data_object_reference', 'object'):
                 vreg = self._cw.vreg
@@ -953,8 +956,6 @@
                 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])
-            else:
-                button = None
         if button:
             # No button if user cannot add any relation.
             self.w(button)
--- a/views/viewlib.py	Thu Jan 19 11:07:01 2017 +0100
+++ b/views/viewlib.py	Thu Jan 19 19:17:29 2017 +0100
@@ -22,7 +22,18 @@
 
 from ..xsd import un_camel_case
 from ..xsd2uicfg import FIRST_LEVEL_ETYPES
-from . import CONTENT_ETYPE, add_subobjects_button
+from . import CONTENT_ETYPE, add_subobject_link, dropdown_button
+
+
+def add_links_from_rtype_roles(entity, rtype_roles, extraurlparams):
+    """Return a list of links to be inserted in dropdown button to add subobjects linked to `entity`
+    through relations defined in `rtype_roles`, depending on user's permission to add any of them.
+
+    `rtype_roles` is list of 2-uples `(rtype, role)`.
+    """
+    links = [add_subobject_link(entity, rtype, role, extraurlparams, klass='')
+             for rtype, role in rtype_roles]
+    return [l for l in links if l]
 
 
 class XSDMetaEntityView(EntityView):
@@ -242,10 +253,10 @@
 
     def display_add_button(self, entity):
         rtype_roles = [(rtype, role) for rtype, role, _ in self.rtype_role_targets]
-        button = add_subobjects_button(entity, rtype_roles, self.url_params(entity))
-        if button:
-            # No button if user cannot add any relation.
-            self.w(button)
+        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'))
 
     def display_subobjects_list(self, entity):