[simplified ui] Add a custom data objects tab on archive unit of a simplified profile
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 21 Jul 2016 15:10:36 +0200
changeset 1490 e59235e603cf
parent 1489 c6a82e112fdf
child 1491 0de8c6c807a7
[simplified ui] Add a custom data objects tab on archive unit of a simplified profile * generate buttons to add binary/physical data object instead of reference, with extra information allowing to create automatically the reference * override data object autoform's `inlined_form_views` method to insert the aforementioned information as an hidden field (this won't work with __linkto since this is a relation to an inlined entity, not the main entity) * similar display of exising data objects as in the original data objects tab of archive transfer Related to #14066262
views/archiveunit.py
--- a/views/archiveunit.py	Thu Jul 21 15:05:12 2016 +0200
+++ b/views/archiveunit.py	Thu Jul 21 15:10:36 2016 +0200
@@ -26,9 +26,9 @@
 
 from cubes.seda.entities import simplified_profile
 from cubes.seda.entities.itree import parent_archive_unit
-from cubes.seda.views import (add_subobject_link, dropdown_button, rtags_from_rtype_role_targets,
-                              copy_rtag)
-from cubes.seda.views import viewlib, widgets
+from cubes.seda.views import (add_subobject_link, add_subobjects_button, dropdown_button,
+                              rtags_from_rtype_role_targets, copy_rtag)
+from cubes.seda.views import viewlib, widgets, archivetransfer
 from cubes.seda.views import uicfg as sedauicfg  # noqa - ensure those rules are defined first
 
 _ = unicode
@@ -344,10 +344,25 @@
 
 
 class DataObjectSimplifiedAutomaticEntityForm(widgets.SimplifiedAutomaticEntityForm):
+    """On creation of a BinaryDataObject or PhysicalDataObject's in the context of a simplified
+    profile, add a field to handle the creation of the relation to the archive unit specified in
+    `req.form`.
+    """
+    # don't add match_form_params('referenced_by') since it's only specified for creation, not
+    # edition
     __select__ = (widgets.SimplifiedAutomaticEntityForm.__select__
                   & is_instance('SEDABinaryDataObject', 'SEDAPhysicalDataObject')
                   & simplified_profile())
 
+    def inlined_form_views(self):
+        views = super(DataObjectSimplifiedAutomaticEntityForm, self).inlined_form_views()
+        ref_form = [v.form for v in views if v.rtype == 'seda_data_object_reference_id'][0]
+        if not ref_form.edited_entity.has_eid() and not ref_form.posting:
+            ref_form.add_hidden(name='seda_data_object_reference', eidparam=True,
+                                role='subject',
+                                value=self._cw.form['referenced_by'])
+        return views
+
 
 class DataObjectReferenceNoTitleEntityInlinedFormRenderer(widgets.NoTitleEntityInlinedFormRenderer):
     """Don't display any title nor remove link for DataObjectReference in the context of a
@@ -356,3 +371,36 @@
     __select__ = (widgets.NoTitleEntityInlinedFormRenderer.__select__
                   & is_instance('SEDADataObjectReference')
                   & simplified_profile())
+
+
+class ArchiveUnitDataObjectReferencesTab(ArchiveUnitDataObjectReferencesTab):
+    """Tab for data object references of an archive unit within a simplified transfer: see
+    referenced data object as if they were children of the archive unit referencing them (only one
+    reference is allowed in such case, so this is fine).
+    """
+    __regid__ = 'seda_au_data_objects_refs_tab'
+    __select__ = ArchiveUnitDataObjectReferencesTab.__select__ & simplified_profile()
+
+    def display_add_button(self, entity):
+        rtype_role_targets = archivetransfer.ArchiveTransferDataObjectsTab.rtype_role_targets
+        rtype_roles = [(rtype, role) for rtype, role, _ in rtype_role_targets]
+        params = self.url_params(entity)
+        params['referenced_by'] = unicode(entity.eid)
+        transfer = entity.container[0]
+        button = add_subobjects_button(transfer, rtype_roles, params)
+        if button:
+            # No button if user cannot add any relation.
+            self.w(button)
+            self.w(tags.div(klass='clearfix'))
+
+    def display_subobjects_list(self, entity):
+        for target in ('SEDABinaryDataObject', 'SEDAPhysicalDataObject'):
+            rset = self._cw.execute(
+                'Any DO, DOID, DOUC ORDERBY DOID WHERE DO is {0}, '
+                'DO id DOID, DO user_cardinality DOUC, '
+                'REF seda_data_object_reference_id DO, REF seda_data_object_reference SEQ, '
+                'SEQ eid %(x)s'.format(target), {'x': entity.eid})
+            if rset:
+                self.w('<h2>%s</h2>' % self._cw.__(target + '_plural'))
+                self._cw.view('list', rset=rset, parent=self.parent(entity), w=self.w,
+                              subvid=self.subvid, tabid=self.__regid__)