[components] Add related data components
authorVincent Michel <vincent.michel@logilab.fr>
Wed, 19 Dec 2012 17:17:59 +0100
changeset 25 c1f4606e9228
parent 24 51ef281aac62
child 26 e9f7ffbd5b50
[components] Add related data components
views/components.py
--- a/views/components.py	Wed Dec 19 14:24:13 2012 +0100
+++ b/views/components.py	Wed Dec 19 17:17:59 2012 +0100
@@ -6,7 +6,14 @@
                                  has_related_entities, match_view)
 from cubicweb.web import component
 
+from cubes.medicalexp.views.components import MedicalExpRelatedDataAbstract
 
+from cubes.brainomics.schema import ALL_MEASURES
+
+
+###############################################################################
+### DOWNLOAD BOXES ############################################################
+###############################################################################
 class ScanZipFileBox(component.CtxComponent):
     __regid__ = 'scan.zipfile'
     __select__ = component.CtxComponent.__select__  & is_instance('Scan', 'GenomicMeasure')
@@ -55,22 +62,80 @@
              self._cw._('View images')))
 
 
-class RelatedSubjectsBox(component.CtxComponent):
+###############################################################################
+### RELATED DATA COMPONENTS ###################################################
+###############################################################################
+class BrainomicsRelatedStudies(MedicalExpRelatedDataAbstract):
+    __regid__ = 'related.studies'
+    __select__ = MedicalExpRelatedDataAbstract.__select__  & is_instance('Subject', *ALL_MEASURES)
+    title = _('Related studies')
+    order = 1
+
+    @property
+    def rql_query(self):
+        eids = [r[0] for r in self.cw_rset]
+        if not eids:
+            return
+        eids = ', '.join([str(e) for e in eids])
+        # Only one Etype/at least one entity, cf selector
+        entity = self.cw_rset.get_entity(0,0)
+        if entity.__regid__ == 'Subject':
+            return 'DISTINCT Any S WHERE X related_studies S, X eid IN (%s)'  % eids
+        return 'DISTINCT Any S WHERE X related_study S, X eid IN (%s)'  % eids
+
+
+class BrainomicsRelatedSubjects(MedicalExpRelatedDataAbstract):
     __regid__ = 'related.subjects'
-    __select__ = component.CtxComponent.__select__  & is_instance('Scan',
-                                                                  'QuestionnaireRun','GenomicMeasure')
-    title = _('See the related subjects')
-    order = 1
-    context = 'left'
+    __select__ = MedicalExpRelatedDataAbstract.__select__  & is_instance('Study', *ALL_MEASURES)
+    title = _('Related subjects')
+    order = 2
+
+    @property
+    def rql_query(self):
+        eids = [r[0] for r in self.cw_rset]
+        if not eids:
+            return
+        eids = ', '.join([str(e) for e in eids])
+        # Only one Etype/at least one entity, cf selector
+        entity = self.cw_rset.get_entity(0,0)
+        if entity.__regid__ == 'Study':
+            return 'DISTINCT Any X WHERE X related_studies S, S eid IN (%s)'  % eids
+        return 'DISTINCT Any S WHERE X concerns S, X eid IN (%s)'  % eids
+
+
+class BrainomicsRelatedMeasures(MedicalExpRelatedDataAbstract):
+    __regid__ = 'related.measures'
+    __select__ = MedicalExpRelatedDataAbstract.__select__  & is_instance('Study', 'Subject', *ALL_MEASURES)
+    order = 3
 
     def render(self, w, **kwargs):
         eids = [r[0] for r in self.cw_rset]
         if not eids:
             return
         eids = ', '.join([str(e) for e in eids])
-        query = 'DISTINCT Any S WHERE X concerns S, X eid IN (%s)'  % eids
-        w(u'<div><a href="%s"><button class="btn btn-primary" '
-          'type="button"><i class="icon-user icon-white"></i>%s'
-          '</button></a></div></br>'
-          % (self._cw.build_url(vid='list', rql=query),
-             self._cw._('Related subjects')))
+        # Only one Etype/at least one entity, cf selector
+        entity = self.cw_rset.get_entity(0,0)
+        for measure in ALL_MEASURES:
+            # Do not show current measure etype
+            if measure != entity.__regid__:
+                # Check if it exists some related measures
+                if entity.__regid__ == 'Study':
+                    rset = self._cw.execute('Any COUNT(X) WHERE X related_study S, X is %s, S eid %s'
+                                            % (measure, entity.eid))
+                    query = 'Any X WHERE X related_study S, X is %s, S eid IN (%s)' % (measure, eids)
+                elif entity.__regid__ == 'Subject':
+                    rset = self._cw.execute('Any COUNT(X) WHERE X concerns S, X is %s, S eid %s'
+                                            % (measure, entity.eid))
+                    query = 'Any X WHERE X concerns S, X is %s, S eid IN (%s)' % (measure, eids)
+                else:
+                    # Measure
+                    rset = self._cw.execute('Any COUNT(XX) WHERE X concerns S, X eid IN (%s), XX concerns S, '
+                                            'XX is %s'  % (eids, measure))
+                    query = ('DISTINCT Any XX WHERE X concerns S, X eid IN (%s), XX concerns S, '
+                             'XX is %s'  % (eids, measure))
+                # Show the link only if there are some related measure
+                if rset[0][0]:
+                    url = self._cw.build_url(vid='list', rql=query)
+                    state = 'active'
+                    w(u'<li class="%s"><a href="%s">%s</a></li>'
+                      % (state, url, self._cw._('Related %s' % measure)))