[views] Make primary views uicfg-configurable (related to #3340591)
authorVladimir Popescu <vladimir.popescu@logilab.fr>
Mon, 20 Jan 2014 10:51:28 +0000
changeset 626 a95dafbddee4
parent 625 fb0db36b7812
child 627 f8f9f52a327b
[views] Make primary views uicfg-configurable (related to #3340591)
views/primary.py
--- a/views/primary.py	Mon Jan 20 10:39:54 2014 +0000
+++ b/views/primary.py	Mon Jan 20 10:51:28 2014 +0000
@@ -30,90 +30,47 @@
 ### BRAINOMICS PRIMARY VIEW ###################################################
 ###############################################################################
 class BrainomicsPrimaryView(PrimaryView):
+    """ Redefine primary view for html customization """
 
-    def iterate_attributes(self, entity):
-        """ Iterate over the attributes. Logic similar to the one in CW's primary view
+    def render_entity_attributes(self, entity):
+        """Renders all attributes and relations in the 'attributes' section.
         """
-        eschema = entity.e_schema
-        for rschema, tschemas, role in eschema.relation_definitions(True):
-            if rschema in VIRTUAL_RTYPES or rschema in META_RTYPES:
-                continue
-            if rschema in VIRTUAL_RTYPES:
-                continue
-            dispctrl = self.display_ctrl.etype_get(eschema, rschema, role, '*')
+        display_attributes = []
+        for rschema, _, role, dispctrl in self._section_def(entity, 'attributes'):
             vid = dispctrl.get('vid', 'reledit')
-            if rschema.final:
+            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 != '':
+                display_attributes.append( (rschema, role, dispctrl, value) )
+        # BRAINOMICS MODIFICATIONS
+        if display_attributes:
+            self.w(u'<dl class="dl-horizontal">')
+            for rschema, role, dispctrl, value in display_attributes:
+                # pylint: disable=E1101
                 label = self._rel_label(entity, rschema, role, dispctrl)
-                yield label, value
-
-    def iterate_data(self, entity):
-        return []
+                self.render_attribute(label, value)
+            self.w(u'</dl>')
 
-    def _build_data(self, data, rql, label, vid=None):
-        rset = self._cw.execute(rql)
-        if len(rset):
-            if not vid:
-                url = self._cw.build_url(rql=rql)
-            else:
-                url = self._cw.build_url(rql=rql, vid=vid)
-            ahref = u'<a href="%s">%s</a>' % (url, self._cw._('See all'))
-            data.append((label, (len(rset), ahref)))
-        return data
+    def render_attribute(self, label, value):
+        self.w(u'<dt>%(label)s</dt><dd>%(value)s</dd>'
+               % {'label': xml_escape(self._cw._(label)),
+                  'value': value})
 
-    def display_main_col(self, entity):
-        """ Render all relations of the 'relations' section, similarly to CW's primary view
-        """
+    def render_relation(self, label, value):
         self.w(u'<div class="col-md-12">')
-        eschema = entity.e_schema
-        for rschema, tschemas, role in eschema.relation_definitions(True):
-            if rschema in VIRTUAL_RTYPES or rschema in META_RTYPES or rschema.final:
-                continue
-            rset = entity.related(rschema.type, role)
-            dispctrl = self.display_ctrl.etype_get(eschema, rschema, role, '*')
-            label = self._rel_label(entity, rschema, role, dispctrl)
-            if rset:
-                self.w(u'<h3>%s</h3>' % self._cw._(label))
-                self.wview('list', rset=rset)
+        if label:
+            self.w(u'<h4>%s</h4>' % label)
+        self.w(value)
         self.w(u'</div>')
 
-    def display_additional_header(self, entity):
-        pass
-
-    def display_header(self, entity):
-        w = self.w
-        w(u'<div class="well">')
-        w(u'<div class="page-header">')
-        w(u'<h2>%s</h2>' % xml_escape(entity.dc_title()))
-        w(u'</div>')
-        w(u'<dl class="dl-horizontal">')
-        for label, attribute in self.iterate_attributes(entity):
-            if attribute:
-                w(u'<dt>%s</dt><dd>%s</dd>' % (self._cw._(label), attribute))
-        w(u'</dl>')
-        # Additional header
-        self.display_additional_header(entity)
-        # Data
-        self.display_data_table(entity)
-        w(u'</div>')
-
-    def display_data_table(self, entity):
-        w = self.w
-        table_data = self.iterate_data(entity)
-        if not table_data:
-            return
-        w(u'<h3>%s</h3>' % self._cw._('Data overview'))
-        w(u'<table class="table table-striped table-bordered table-condensed">')
-        for label, data in table_data:
-            w(u'<tr>')
-            w(u'<th>%s</th>' % self._cw._(label))
-            for d in data:
-                w(u'<th>%s</th>' % d)
-            w(u'</tr>')
-        w(u'</table>')
-
-    def call(self, rset=None):
+    def render_entity(self, rset=None):
         entity = self.cw_rset.get_entity(0,0)
         w = self.w
         # define rsection for similar behavior than CW's primary view
@@ -124,19 +81,20 @@
         if self.display_ctrl is None:
             self.display_ctrl = uicfg_reg.select('primaryview_display_ctrl',
                                                  self._cw, entity=entity)
-        # Tabblables
+        # BRAINOMICS MODIFICATIONS - Customize html
         w(u'<div class="tab-content">')
-        # Mainn table
         w(u'<div class="tab-pane active" id="entity-mainview">')
-        # Header
-        self.display_header(entity)
-        # Main col
+        # Attributes
+        w(u'<div class="well">')
+        w(u'<h2>%s</h2>' % xml_escape(entity.dc_title()))
+        self.render_entity_attributes(entity)
+        w(u'</div>')
+        # Relations
         w(u'<div class="row">')
-        self.display_main_col(entity)
+        self.render_entity_relations(entity)
         w(u'</div>')
-        # Close table
         w(u'</div>')
-        # Add other tables
+        # BRAINOMICS MODIFICATIONS - Add tabs
         if 'ctx-entity-tabs' in self._cw.vreg:
             tabs = self._cw.vreg['ctx-entity-tabs'].possible_objects(self._cw, rset=self.cw_rset)
             for tab in tabs:
@@ -150,7 +108,7 @@
 class SubjectPrimaryView(BrainomicsPrimaryView):
     __select__ = BrainomicsPrimaryView.__select__ & is_instance('Subject')
 
-    def iterate_attributes(self, entity):
+    def render_entity_attributes(self, entity):
         return [(self._cw._('Age'), entity.display_age_for_assessments()),
                 (self._cw._('Handedness'), entity.handedness),
                 (self._cw._('Gender'), entity.gender),]
@@ -181,7 +139,7 @@
             data = self._build_data(data, rql, self._cw._(measure))
         return data
 
-    def display_main_col(self, entity):
+    def render_entity_relations(self, entity):
         self.w(u'<div class="col-md-10">')
         # Detailed measures
         rset = self._cw.execute('Any X WHERE X concerns S, S eid %(e)s', {'e': entity.eid})
@@ -205,7 +163,7 @@
 class StudyPrimaryView(BrainomicsPrimaryView):
     __select__ = BrainomicsPrimaryView.__select__ & is_instance('Study')
 
-    def iterate_attributes(self, entity):
+    def render_entity_attributes(self, entity):
         return [(self._cw._('Name'), entity.name),
                 (self._cw._('Description'), entity.description),
                 (self._cw._('Keywords'), entity.keywords),
@@ -231,14 +189,14 @@
 class InvestigatorPrimaryView(BrainomicsPrimaryView):
     __select__ = BrainomicsPrimaryView.__select__ & is_instance('Investigator')
 
-    def iterate_attributes(self, entity):
+    def render_entity_attributes(self, entity):
         return [(self._cw._('Firstname'), entity.firstname),
                 (self._cw._('Lastname'), entity.lastname),
                 (self._cw._('Title'), entity.title),
                 (self._cw._('Institution'), entity.institution),
                 (self._cw._('Department'), entity.department),]
 
-    def display_main_col(self, entity):
+    def render_entity_relations(self, entity):
         # Detailed assessments
         # XXX Direct link to the measures/runs ?
         self.w(u'<h3>%s</h3>' % xml_escape(self._cw._('Assessments conducted by this investigator')))
@@ -253,7 +211,7 @@
 class CenterPrimaryView(BrainomicsPrimaryView):
     __select__ = BrainomicsPrimaryView.__select__ & is_instance('Center')
 
-    def iterate_attributes(self, entity):
+    def render_entity_attributes(self, entity):
         return [(self._cw._('Identifier'), entity.identifier),
                 (self._cw._('Department'), entity.department),
                 (self._cw._('City'), entity.city),
@@ -269,7 +227,7 @@
         data = self._build_data(data, rql, self._cw._('Assessments'))
         return data
 
-    def display_main_col(self, entity):
+    def render_entity_relations(self, entity):
         # Devices
         rset = self._cw.execute('Any S WHERE S is Device, '
                                 'S hosted_by X, X eid %(e)s', {'e': entity.eid})
@@ -284,7 +242,7 @@
 class DevicePrimaryView(BrainomicsPrimaryView):
     __select__ = BrainomicsPrimaryView.__select__ & is_instance('Device')
 
-    def iterate_attributes(self, entity):
+    def render_entity_attributes(self, entity):
         center = entity.hosted_by[0]
         center = u'<a href="%s">%s</a>' % (center.absolute_url(), center.dc_title())
         return [(self._cw._('Name'), entity.name),
@@ -305,7 +263,7 @@
 class AssessmentPrimaryView(BrainomicsPrimaryView):
     __select__ = BrainomicsPrimaryView.__select__ & is_instance('Assessment')
 
-    def iterate_attributes(self, entity):
+    def render_entity_attributes(self, entity):
         subject = entity.reverse_concerned_by[0]
         subject = u'<a href="%s">%s</a>' % (subject.absolute_url(), subject.dc_title())
         return [(self._cw._('Identifier'), entity.identifier),
@@ -332,7 +290,7 @@
             data = self._build_data(data, rql, self._cw._('Used %(s)s' % {'s': m}))
         return data
 
-    def display_main_col(self, entity):
+    def render_entity_relations(self, entity):
         pass
 
 ###############################################################################
@@ -341,15 +299,15 @@
 class ScoreDefinitionPrimaryView(BrainomicsPrimaryView):
     __select__ = BrainomicsPrimaryView.__select__ & is_instance('ScoreDefinition')
 
-    def iterate_attributes(self, entity):
+    def render_entity_attributes(self, entity):
         return [(self._cw._('Name'), entity.name),
                 (self._cw._('Category'), entity.category),
                 (self._cw._('Type'), entity.type),
                 (self._cw._('Unit'), entity.unit),
                 (self._cw._('Possible values'), entity.possible_values)
-                ]
+               ]
 
-    def display_main_col(self, entity):
+    def render_entity_relations(self, entity):
         # Scores
         w = self.w
         rset = self._cw.execute('Any S,V,T,DA WHERE S is ScoreValue, S value V, S text T, '
@@ -392,7 +350,7 @@
 class GenericTestRunPrimaryView(BrainomicsPrimaryView):
     __select__ = BrainomicsPrimaryView.__select__ & is_instance('GenericTestRun')
 
-    def iterate_attributes(self, entity):
+    def render_entity_attributes(self, entity):
         test = entity.instance_of[0]
         test = u'<a href="%s">%s</a>' % (test.absolute_url(), test.dc_title())
         subject = entity.concerns[0]
@@ -401,7 +359,7 @@
                 (self._cw._('Instance of'), test),
                 (self._cw._('Subject'), subject),]
 
-    def display_main_col(self, entity):
+    def render_entity_relations(self, entity):
         w = self.w
         # Scores
         self.w(u'<div class="col-md-9">')
@@ -437,7 +395,7 @@
 class GenericTestPrimaryView(BrainomicsPrimaryView):
     __select__ = BrainomicsPrimaryView.__select__ & is_instance('GenericTest')
 
-    def iterate_attributes(self, entity):
+    def render_entity_attributes(self, entity):
         return [(self._cw._('Identifier'), entity.identifier),
                 (self._cw._('Name'), entity.name),
                 (self._cw._('Type'), entity.type),
@@ -458,13 +416,13 @@
 class ExternalResourcePrimaryView(BrainomicsPrimaryView):
     __select__ = BrainomicsPrimaryView.__select__ & is_instance('ExternalResource')
 
-    def iterate_attributes(self, entity):
+    def render_entity_attributes(self, entity):
         study = entity.related_study
         study = u'<a href="%s">%s</a>' % (study[0].absolute_url(), study[0].dc_title()) if study else u''
         return [(self._cw._('Name'), entity.name),
                 (self._cw._('Filepath'), entity.filepath),
                 (self._cw._('Study'), study),
-                ]
+               ]
 
 
 ###############################################################################
@@ -473,7 +431,7 @@
 class QuestionnaireRunPrimaryView(BrainomicsPrimaryView):
     __select__ = BrainomicsPrimaryView.__select__ & is_instance('QuestionnaireRun')
 
-    def iterate_attributes(self, entity):
+    def render_entity_attributes(self, entity):
         questionnaire = entity.instance_of[0]
         questionnaire = u'<a href="%s">%s</a>' % (questionnaire.absolute_url(), questionnaire.dc_title())
         subject = entity.concerns[0]
@@ -484,7 +442,7 @@
                 (self._cw._('Instance of'), questionnaire),
                 (self._cw._('Subject'), subject),
                 (self._cw._('Assessment'), assessment),
-                ]
+               ]
 
 
     def display_additional_header(self, entity):
@@ -501,7 +459,7 @@
             w(u'<h3>%s</h3>' % self._cw._('Additional resources'))
             self.wview('list', rset=rset)
 
-    def display_main_col(self, entity):
+    def render_entity_relations(self, entity):
         w = self.w
         # Results file
         w(entity.view('results-view'))
@@ -536,13 +494,13 @@
 class QuestionnairePrimaryView(BrainomicsPrimaryView):
     __select__ = BrainomicsPrimaryView.__select__ & is_instance('Questionnaire')
 
-    def iterate_attributes(self, entity):
+    def render_entity_attributes(self, entity):
         return [(self._cw._('Name'), entity.name),
                 (self._cw._('Type'), entity.type),
                 (self._cw._('Version'), entity.version),
                 (self._cw._('Language'), entity.language),
                 (self._cw._('Note'), entity.note),
-                ]
+               ]
 
     def iterate_data(self, entity):
         data = []
@@ -550,7 +508,7 @@
         data = self._build_data(data, rql, self._cw._('Related runs'))
         return data
 
-    def display_main_col(self, entity):
+    def render_entity_relations(self, entity):
         # Results file
         self.w(entity.view('results-view'))
         # Questions
@@ -583,7 +541,7 @@
 class QuestionPrimaryView(BrainomicsPrimaryView):
     __select__ = BrainomicsPrimaryView.__select__ & is_instance('Question')
 
-    def iterate_attributes(self, entity):
+    def render_entity_attributes(self, entity):
         questionnaire = entity.questionnaire[0]
         questionnaire = u'<a href="%s">%s</a>' % (questionnaire.absolute_url(), questionnaire.dc_title())
         return [(self._cw._('Questionnaire'), questionnaire),
@@ -591,9 +549,9 @@
                 (self._cw._('Text'), entity.text),
                 (self._cw._('Type'), entity.type),
                 (self._cw._('Possible answers'), entity.possible_answers),
-                ]
+               ]
 
-    def display_main_col(self, entity):
+    def render_entity_relations(self, entity):
         # Answers
         w = self.w
         rset = self._cw.execute('Any A, AQ, S, Q, AV, AD, AI, I, QT, QPA WHERE A is Answer, '
@@ -625,7 +583,7 @@
 class ScanPrimaryView(BrainomicsPrimaryView):
     __select__ = BrainomicsPrimaryView.__select__ & is_instance('Scan')
 
-    def iterate_attributes(self, entity):
+    def render_entity_attributes(self, entity):
         subject = entity.concerns[0]
         subject = u'<a href="%s">%s</a>' % (subject.absolute_url(), subject.dc_title())
         device = entity.uses_device
@@ -643,7 +601,7 @@
                 (self._cw._('Subject'), subject),
                 (self._cw._('Device'), device),
                 (self._cw._('Assessment'), assessment),
-                ]
+               ]
 
     def display_header(self, entity):
         w = self.w
@@ -652,9 +610,9 @@
         w(u'<h2>%s</h2>' % xml_escape(entity.dc_title()))
         w(u'</div>')
         w(u'<dl class="dl-horizontal">')
-        for label, attribute in self.iterate_attributes(entity):
-            if attribute:
-                w(u'<dt>%s</dt><dd>%s</dd>' % (self._cw._(label), attribute))
+        for label, value in self.render_entity_attributes(entity):
+            if value:
+                self.render_entity_attribute(self._cw._(lavel), value)
         # Add data info
         data = entity.has_data
         if data:
@@ -666,7 +624,7 @@
         self.display_data_table(entity)
         w(u'</div>')
 
-    def display_main_col(self, entity):
+    def render_entity_relations(self, entity):
         # Results file
         self.w(entity.view('results-view'))
         # Related external resources
@@ -684,7 +642,7 @@
 class GenomicMeasurePrimaryView(BrainomicsPrimaryView):
     __select__ = BrainomicsPrimaryView.__select__ & is_instance('GenomicMeasure')
 
-    def iterate_attributes(self, entity):
+    def render_entity_attributes(self, entity):
         subject = entity.concerns[0]
         subject = u'<a href="%s">%s</a>' % (subject.absolute_url(), subject.dc_title())
         fields = [(self._cw._('Type'), entity.type),
@@ -697,7 +655,7 @@
             fields.append((self._cw._('Platform'), entity.platform[0].view('outofcontext')))
         return fields
 
-    def display_main_col(self, entity):
+    def render_entity_relations(self, entity):
         self.w(entity.view('results-view'))
         cgh_results = self._cw.execute('Any R WHERE R is CghResult, '
                                        'R related_measure X, X eid %(e)s',
@@ -724,14 +682,14 @@
 class GenePrimaryView(BrainomicsPrimaryView):
     __select__ = BrainomicsPrimaryView.__select__ & is_instance('Gene')
 
-    def iterate_attributes(self, entity):
+    def render_entity_attributes(self, entity):
         fields = [(self._cw._('Identifier'), entity.gene_id),
                   (self._cw._('Start position'), entity.start_position),
                   (self._cw._('Stop position'), entity.stop_position),
                   (self._cw._('Chromosomes'), '; '.join(chrom.view('incontext') for chrom in entity.chromosomes))]
         return fields
 
-    def display_main_col(self, entity):
+    def render_entity_relations(self, entity):
         cgh_results = self._cw.execute('Any R WHERE R is CghResult, '
                                        'R genomic_region G, G genes X, X eid %(e)s',
                                        {'e': entity.eid})
@@ -753,7 +711,7 @@
 class GenomicRegionPrimaryView(BrainomicsPrimaryView):
     __select__ = BrainomicsPrimaryView.__select__ & is_instance('GenomicRegion')
 
-    def iterate_attributes(self, entity):
+    def render_entity_attributes(self, entity):
         genes_rql = ('Any R WHERE R is Gene, X genes R, X eid %(e)s'
                      % {'e': entity.eid})
         genes_url = self._cw.build_url(rql=genes_rql)
@@ -767,7 +725,7 @@
                   (self._cw._('Genes'), u'<a href="%s">%s</a>' % (genes_url, self._cw._('See all genes')))]
         return fields
 
-    def display_main_col(self, entity):
+    def render_entity_relations(self, entity):
         cgh_results = self._cw.execute('Any R WHERE R is CghResult, '
                                        'R genomic_region X, X eid %(e)s',
                                        {'e': entity.eid})
@@ -782,14 +740,14 @@
 class ScoreGroupPrimaryView(BrainomicsPrimaryView):
     __select__ = BrainomicsPrimaryView.__select__ & is_instance('ScoreGroup')
 
-    def iterate_attributes(self, entity):
+    def render_entity_attributes(self, entity):
         yield (self._cw._('Identifier'), entity.identifier)
         for related_ent in entity.reverse_related_score_groups:
             yield (self._cw._(related_ent.cw_etype), related_ent.view('incontext'))
         if entity.protocols:
             yield (self._cw._('Protocols'), ', '.join([t.dc_title() for t in entity.protocols]))
 
-    def display_main_col(self, entity):
+    def render_entity_relations(self, entity):
         scores_rset = self._cw.execute('Any V WHERE V is ScoreValue, X scores V, X eid %(e)s',
                                        {'e': entity.eid})
         if scores_rset: