[views] UI to edit file_category instead of format_id / mime_type relations
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 11 Oct 2017 15:16:03 +0200
changeset 2750 1ef4f5936c15
parent 2749 5b3ec7300aae
child 2753 e562b3d62a0d
[views] UI to edit file_category instead of format_id / mime_type relations Do it minimal with no js involved but a simple multiple select. This still have to be coupled to format_id / mime_type but this will be done in later csets. Related to #36331831
cubicweb_seda/views/dataobject.py
--- a/cubicweb_seda/views/dataobject.py	Wed Oct 11 16:40:21 2017 +0200
+++ b/cubicweb_seda/views/dataobject.py	Wed Oct 11 15:16:03 2017 +0200
@@ -119,6 +119,8 @@
 
 pvs.tag_subject_of(('*', 'seda_algorithm', '*'), 'attributes')
 pvs.tag_object_of(('*', 'seda_target', '*'), 'hidden')  # in the relationship tab
+# hide file_category from the main tab, it lives in the format tab
+pvs.tag_subject_of(('SEDABinaryDataObject', 'file_category', '*'), 'hidden')
 
 for rtype in ('seda_compressed', 'seda_data_object_version_from'):
     # hide relation from autoform because of limitation of _container_eid
@@ -182,6 +184,17 @@
     ]
 
 
+def _setup_format_tab(rsection, display_ctrl):
+    # hide format_id / mime_type, in favor of custom 'file_category'. Actual
+    # format_id and mime_type will be derived from that value (XXX hook or profile
+    # gen time ?)
+    rsection.tag_object_of(('*', 'seda_format_id_from', 'SEDABinaryDataObject'), 'hidden')
+    rsection.tag_object_of(('*', 'seda_mime_type_from', 'SEDABinaryDataObject'), 'hidden')
+    rsection.tag_subject_of(('SEDABinaryDataObject', 'file_category', '*'), 'attributes')
+    # dunno why it crashes without this
+    display_ctrl.tag_subject_of(('SEDABinaryDataObject', 'file_category', '*'), {'order': 0})
+
+
 class BinaryDataObjectFormatIdentificationTab(viewlib.PrimaryTabWithoutBoxes):
     """Display format identification information of a binary data object"""
 
@@ -189,6 +202,7 @@
     __select__ = is_instance('SEDABinaryDataObject')
 
     rsection, display_ctrl = rtags_from_xsd_element('SEDABinaryDataObject', 'FormatIdentification')
+    _setup_format_tab(rsection, display_ctrl)
 
 
 class SimplifiedBinaryDataObjectFormatIdentificationTab(BinaryDataObjectFormatIdentificationTab):
@@ -196,9 +210,33 @@
     __select__ = BinaryDataObjectFormatIdentificationTab.__select__ & simplified_profile()
 
     rsection, display_ctrl = rtags_from_xsd_element('SEDABinaryDataObject', 'FormatIdentification')
+    _setup_format_tab(rsection, display_ctrl)
     rsection.tag_object_of(('*', 'seda_format_litteral', 'SEDABinaryDataObject'), 'hidden')
 
 
+def file_category_vocabulary(form, field):
+    rset = form._cw.execute(
+        'Any C,CLL,E,ELL ORDERBY CLL,ELL WHERE '
+        'C in_scheme CS, CS scheme_relation_type CR, CR name "file_category", '
+        'NOT C broader_concept SC, E broader_concept C, '
+        'CL label_of C, CL label CLL,'
+        'EL label_of E, EL label ELL'
+    )
+    values = []
+    current_category = None
+    for category, category_label, extension, extension_label in rset:
+        if current_category is None or current_category != category:
+            current_category = category
+            values.append((category_label, str(category)))
+        values.append((u'{} > {}'.format(category_label, extension_label),
+                       str(extension)))
+    return values
+
+
+affk.tag_subject_of(('SEDABinaryDataObject', 'file_category', '*'),
+                    {'choices': file_category_vocabulary})
+
+
 class BinaryDataObjectFileInfoTab(viewlib.PrimaryTabWithoutBoxes):
     """Display file information of a binary data object"""