Extract function to compute 'add links' with custom arguments
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 19 Jan 2017 19:53:11 +0100
changeset 2309 b4e5981f7aad
parent 2308 6ced28736579
child 2310 d7f6c9b89b7b
Extract function to compute 'add links' with custom arguments from the archive unit's archive units tab (to add sub archive unit parametrized by an extra URL parameter).
views/archiveunit.py
--- a/views/archiveunit.py	Thu Jan 19 19:28:05 2017 +0100
+++ b/views/archiveunit.py	Thu Jan 19 19:53:11 2017 +0100
@@ -38,6 +38,22 @@
 from . import uicfg as sedauicfg  # noqa - ensure those rules are defined first
 
 
+def add_links_with_custom_arguments(seq, rtype, role, extraurlparams, sub_unit_types):
+    """Return a list of links to be inserted in dropdown button to add subobjects through `rtype`
+    and `role` but with an extra argument in url params, given through the `param_defs` list of
+    3-uple defining argument's (name, value, link label).
+    """
+    links = []
+    urlparams = extraurlparams.copy()
+    for argument, value, label in sub_unit_types:
+        urlparams[argument] = value
+        link = add_subobject_link(seq, rtype, role, urlparams,
+                                  msg=label, klass='')
+        if link:
+            links.append(link)
+    return links
+
+
 @objectify_predicate
 def is_archive_unit_ref(cls, req, rset=None, entity=None, **kwargs):
     """Return 1 if a unit_type value is specified in kwargs or in form parameters, and its value is
@@ -879,25 +895,21 @@
 
     rtype = 'seda_archive_unit'
     role = 'object'
+    unit_custom_arguments = [
+        ('unit_type', 'unit_content', _('archive unit (content)')),
+        ('unit_type', 'unit_ref', _('archive unit (reference)')),
+    ]
 
     _('creating SEDAArchiveUnit (SEDAArchiveUnit seda_archive_unit '
       'SEDASeqAltArchiveUnitArchiveUnitRefIdManagement %(linkto)s)')
 
-    sub_unit_types = [('unit_content', _('archive unit (content)')),
-                      ('unit_ref', _('archive unit (reference)'))]
-
     def entity_call(self, entity):
-        seq = entity.first_level_choice.content_sequence
         urlparams = {'__redirectparams': 'tab=' + self.__regid__,
                      '__redirectpath': entity.rest_path()}
-        links = []
-        for unit_type, label in self.sub_unit_types:
-            urlparams['unit_type'] = unit_type
-            link = add_subobject_link(seq, self.rtype, self.role, urlparams,
-                                      msg=self._cw._(label), klass='')
-            if link:
-                links.append(link)
+        links = add_links_with_custom_arguments(entity, self.rtype, self.role, urlparams,
+                                                self.unit_custom_arguments)
         viewlib.display_add_button(self.w, links, self._cw.__('add'))
+        seq = entity.first_level_choice.content_sequence
         rset = seq.related(self.rtype, self.role)
         if rset:
             subvid = 'seda.listitem'