[components] Add support for demo
authorVincent Michel <vincent.michel@logilab.fr>
Thu, 10 Jan 2013 16:41:06 +0100
changeset 28 97972227a016
parent 27 5e71dea419ef
child 29 cdb83802f32b
[components] Add support for demo
views/components.py
--- a/views/components.py	Fri Jan 11 09:15:14 2013 +0100
+++ b/views/components.py	Thu Jan 10 16:41:06 2013 +0100
@@ -6,7 +6,8 @@
                                  has_related_entities, match_view)
 from cubicweb.web import component
 
-from cubes.medicalexp.views.components import MedicalExpRelatedDataAbstract
+from cubes.medicalexp.views.components import (MedicalExpRelatedDataAbstract,
+                                               MedicalExpAbstractDownloadBox)
 
 from cubes.brainomics.schema import ALL_MEASURES
 
@@ -14,128 +15,39 @@
 ###############################################################################
 ### DOWNLOAD BOXES ############################################################
 ###############################################################################
-class ScanZipFileBox(component.CtxComponent):
-    __regid__ = 'scan.zipfile'
-    __select__ = component.CtxComponent.__select__  & is_instance('Scan', 'GenomicMeasure')
-    title = _('Download as a Zip file')
+class ScanZipFileBox(MedicalExpAbstractDownloadBox):
+    __regid__ = 'scan_zipfile'
+    __select__ = MedicalExpAbstractDownloadBox.__select__  & is_instance('Scan', 'GenomicMeasure')
+    title = _('Download Zip')
+    download_vid = 'data-zip'
     order = 0
-    context = 'left'
-
-    def render(self, w, **kwargs):
-        print self.cw_rset.printable_rql()
-        w(u'<div><a href="%s"><button class="btn btn-primary" '
-          'type="button"><i class="icon-download icon-white"></i>%s'
-          '</button></a></div></br>'
-          % (self._cw.build_url(vid='data-zip',
-                                rql=self.cw_rset.printable_rql()),
-             self._cw._('Download Zip')))
-
-class CsvFileBox(component.CtxComponent):
-    __regid__ = 'measure.csvfile'
-    __select__ = component.CtxComponent.__select__  & is_instance('Subject', 'Scan', 'QuestionnaireRun', 'Answer', 'GenomicMeasure', 'Snp')
-    title = _('Download as a CSV file')
-    order = 0
-    context = 'left'
-
-    def render(self, w, **kwargs):
-        w(u'<div><a href="%s"><button class="btn btn-primary" '
-          'type="button"><i class="icon-list-alt icon-white"></i>%s'
-          '</button></a></div></br>'
-          % (self._cw.build_url(vid='csvexport',
-                                rql=self.cw_rset.printable_rql()),
-             self._cw._('Download CSV')))
 
 
-class SeeSlicesBox(component.CtxComponent):
-    __regid__ = 'see-slices'
-    __select__ = component.CtxComponent.__select__  & is_instance('Scan')
-    title = _('View images')
-    order = 1
-    context = 'left'
+class CsvFileBox(MedicalExpAbstractDownloadBox):
+    __regid__ = 'measure_csvfile'
+    __select__ = component.CtxComponent.__select__  & is_instance('Subject', 'Scan',
+                                                                  'QuestionnaireRun', 'Answer',
+                                                                  'GenomicMeasure', 'Snp')
+    title = _('Download CSV')
+    order = 0
+    always_available = True
+    download_vid = 'csvexport'
 
-    def render(self, w, **kwargs):
-        w(u'<div><a href="%s"><button class="btn btn-primary" '
-          'type="button"><i class="icon-eye-open icon-white"></i>%s'
-          '</button></a></div></br>'
-          % (self._cw.build_url(vid='slices-view',
-                                rql=self.cw_rset.printable_rql()),
-             self._cw._('View images')))
+class SeeSlicesBox(MedicalExpAbstractDownloadBox):
+    __regid__ = 'see_slices'
+    __select__ = MedicalExpAbstractDownloadBox.__select__  & is_instance('Scan')
+    title = _('View images')
+    download_vid = 'slices-view'
+    order = 0
 
 
 ###############################################################################
 ### 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__ = 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])
-        # 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)))
+        super(BrainomicsRelatedMeasures, self).render(w, measures=ALL_MEASURES)