Fold content tab into archive unit's description tab
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 11 Jan 2017 21:29:38 +0100
changeset 2301 15734eda777a
parent 2300 b37082098c56
child 2302 9ead08e8c6fd
Fold content tab into archive unit's description tab This requires a bit more work than expected due to monolitic implementation of render_attributes in cubicweb, as explained in the inline comment. Closes #16684017
i18n/en.po
i18n/fr.po
views/archiveunit.py
--- a/i18n/en.po	Wed Jan 11 15:07:07 2017 +0100
+++ b/i18n/en.po	Wed Jan 11 21:29:38 2017 +0100
@@ -5473,9 +5473,6 @@
 msgid "seda_compression_algorithm_code_list_version_to_object"
 msgstr ""
 
-msgid "seda_content_main_tab"
-msgstr ""
-
 msgid "seda_coverage_tab"
 msgstr ""
 
--- a/i18n/fr.po	Wed Jan 11 15:07:07 2017 +0100
+++ b/i18n/fr.po	Wed Jan 11 21:29:38 2017 +0100
@@ -5494,9 +5494,6 @@
 msgid "seda_compression_algorithm_code_list_version_to_object"
 msgstr ""
 
-msgid "seda_content_main_tab"
-msgstr "contenu"
-
 msgid "seda_coverage_tab"
 msgstr "couverture"
 
--- a/views/archiveunit.py	Wed Jan 11 15:07:07 2017 +0100
+++ b/views/archiveunit.py	Wed Jan 11 21:29:38 2017 +0100
@@ -19,6 +19,7 @@
 
 from logilab.mtconverter import xml_escape
 from logilab.common.registry import objectify_predicate
+from logilab.common.decorators import monkeypatch
 
 from cubicweb import tags, _
 from cubicweb.predicates import is_instance
@@ -224,7 +225,6 @@
         _('seda_archive_units_tab'),
         _('seda_au_data_objects_refs_tab'),
         # Content tabs
-        _('seda_content_main_tab'),
         _('seda_identification_tab'),
         _('seda_restriction_tab'),
         _('seda_dates_tab'),
@@ -244,6 +244,8 @@
         rwdg.boostrap_dialog(self.w, self._cw._, clone._import_div_id(entity), u'')
 
 
+# main tab for archiv unit reference ###########################################
+
 au_ref_pvs = copy_rtag(pvs, __name__,
                        is_instance('SEDAArchiveUnit') & is_archive_unit_ref())
 au_ref_pvs.tag_subject_of(
@@ -735,13 +737,62 @@
                 self.w(self._cw._(' (optional timestamp)'))
 
 
-# content main tab #############################################################
+# main tab for archive unit content ############################################
+
+class ArchiveUnitPrimaryTab(tabs.PrimaryTab):
+    """Overriden primary tab to display attributes from both the archive unit and it's content
+    sequence.
+    """
+
+    __select__ = is_instance('SEDAArchiveUnit') & ~is_archive_unit_ref()
+
+    def render_entity_attributes(self, entity):
+        """Renders all attributes and relations in the 'attributes' section.
+        """
+        display_attributes = list(self._display_attributes(entity))
+        seq = entity.first_level_choice.content_sequence
+        view = self._cw.vreg['views'].select('seda_content_main', self._cw, rset=seq.as_rset())
+        display_attributes += view._display_attributes(seq)
+        if display_attributes:
+            self.w(u'<table class="table cw-table-primary-entity">')
+            for rschema, role, dispctrl, value in display_attributes:
+                label = self._rel_label(entity, rschema, role, dispctrl)
+                self.render_attribute(label, value, table=True)
+            self.w(u'</table>')
+
+
+# add as a monkey patch the first step of original 'render_entity_attributes'
+# method, which is unfortunatly monolitic in cubicweb. We need this so we can
+# have attributes from two distinct entities as if it was only one.
 
-class ArchiveUnitContentMainTab(ArchiveUnitContentTab):
-    """Display content information which are not in a dedicated tab for an archive unit."""
+@monkeypatch(tabs.PrimaryTab)
+def _display_attributes(self, entity):
+    for rschema, targets, role, dispctrl in self._section_def(entity, 'attributes'):
+        vid = dispctrl.get('vid', 'reledit')
+        if rschema.final or vid == 'reledit' or dispctrl.get('rtypevid'):
+            value = entity.view(vid, rtype=rschema.type, role=role,
+                                initargs={'dispctrl': dispctrl})
+        else:
+            rset = self._relation_rset(entity, rschema, role, dispctrl)
+            if rset:
+                value = self._cw.view(vid, rset)
+            else:
+                value = None
+        if value is not None and value != '':
+            yield (rschema, role, dispctrl, value)
 
-    __regid__ = 'seda_content_main_tab'
-    content_vid = 'seda_content_main'
+
+content_ordered_fields = [
+    ('seda_description_level', 'subject'),
+    ('seda_title', 'object'),
+    ('seda_description', 'object'),
+    ('seda_version', 'object'),
+    ('seda_type_from', 'object'),
+    ('seda_document_type', 'object'),
+    ('seda_status', 'object'),
+    ('seda_language_from', 'object'),
+    ('seda_description_language_from', 'object'),
+]
 
 
 class ContentMainView(viewlib.PrimaryTabWithoutBoxes):
@@ -750,6 +801,9 @@
     __regid__ = 'seda_content_main'
     __select__ = is_instance(CONTENT_ETYPE)
 
+    rtype_role_targets = [field_def + (None,) for field_def in content_ordered_fields]
+    rsection, display_ctrl = rtags_from_rtype_role_targets(CONTENT_ETYPE, rtype_role_targets)
+
 
 class SimplifiedContentMainView(ContentMainView):
     """Display content information which are not in a dedicated tab for a simplified archive unit.
@@ -770,17 +824,6 @@
     rsection, display_ctrl = rtags_from_rtype_role_targets(CONTENT_ETYPE, rtype_role_targets)
 
 
-content_ordered_fields = [
-    ('seda_description_level', 'subject'),
-    ('seda_title', 'object'),
-    ('seda_description', 'object'),
-    ('seda_version', 'object'),
-    ('seda_type_from', 'object'),
-    ('seda_document_type', 'object'),
-    ('seda_status', 'object'),
-    ('seda_language_from', 'object'),
-    ('seda_description_language_from', 'object'),
-]
 for rtype, role in content_ordered_fields:
     if role == 'subject':
         pvs.tag_subject_of((CONTENT_ETYPE, rtype, '*'), 'attributes')