[primary] Update CW primary view to Brainomics primary view by default, see #3296292
authorVincent Michel <vincent.michel@logilab.fr>
Thu, 14 Nov 2013 17:10:27 +0000
changeset 485 8a8eaff1d4d2
parent 484 199e8878d98b
child 497 e2a2bb435d1a
[primary] Update CW primary view to Brainomics primary view by default, see #3296292
views/primary.py
--- a/views/primary.py	Fri Nov 22 17:45:49 2013 +0000
+++ b/views/primary.py	Thu Nov 14 17:10:27 2013 +0000
@@ -18,6 +18,8 @@
 
 """cubicweb-suivimp views/forms/actions/components for web ui"""
 from logilab.mtconverter import xml_escape
+
+from cubicweb.schema import META_RTYPES, VIRTUAL_RTYPES
 from cubicweb.selectors import is_instance
 from cubicweb.web.views.primary import PrimaryView
 
@@ -25,14 +27,26 @@
 
 
 ###############################################################################
-### ABSTRACT PRIMARY VIEW #####################################################
+### BRAINOMICS PRIMARY VIEW ###################################################
 ###############################################################################
 class BrainomicsPrimaryView(PrimaryView):
-    __select__ = PrimaryView.__select__
-    __abstract__ = True
 
     def iterate_attributes(self, entity):
-        return []
+        """ Iterate over the attributes. Logic similar to the one in CW's primary view
+        """
+        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, '*')
+            vid = dispctrl.get('vid', 'reledit')
+            if rschema.final:
+                value = entity.view(vid, rtype=rschema.type, role=role,
+                                    initargs={'dispctrl': dispctrl})
+                label = self._rel_label(entity, rschema, role, dispctrl)
+                yield label, value
 
     def iterate_data(self, entity):
         return []
@@ -49,7 +63,20 @@
         return data
 
     def display_main_col(self, entity):
-        pass
+        """ Render all relations of the 'relations' section, similarly to CW's primary view
+        """
+        self.w(u'<div class="span12">')
+        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)
+        self.w(u'</div>')
 
     def display_additional_header(self, entity):
         pass
@@ -89,6 +116,14 @@
     def call(self, rset=None):
         entity = self.cw_rset.get_entity(0,0)
         w = self.w
+        # define rsection for similar behavior than CW's primary view
+        uicfg_reg = self._cw.vreg['uicfg']
+        if self.rsection is None:
+            self.rsection = uicfg_reg.select('primaryview_section',
+                                             self._cw, entity=entity)
+        if self.display_ctrl is None:
+            self.display_ctrl = uicfg_reg.select('primaryview_display_ctrl',
+                                                 self._cw, entity=entity)
         # Tabblables
         w(u'<div class="tab-content">')
         # Mainn table
@@ -694,3 +729,11 @@
                 self.w(u'<h3>%s</h3>' % self._cw._('Sequencing results'))
                 self.wview('gene-mutation-table-view', rset=mutation_results)
             self.w(u'</div>')
+
+
+###############################################################################
+### REGISTRATION CALLBACK #####################################################
+###############################################################################
+def registration_callback(vreg):
+    vreg.register_all(globals().values(), __name__, (BrainomicsPrimaryView,))
+    vreg.register_and_replace(BrainomicsPrimaryView, PrimaryView)