[views] Add csvexport view to deal with csv download when not logged in
authorVladimir Popescu <vladimir.popescu@logilab.fr>
Wed, 17 Jul 2013 11:54:05 +0200
changeset 281 77fce4bb1856
parent 280 04047bb24ad8
child 282 46a66880dbce
[views] Add csvexport view to deal with csv download when not logged in
views/actions.py
views/download.py
--- a/views/actions.py	Mon Jul 15 16:08:20 2013 +0200
+++ b/views/actions.py	Wed Jul 17 11:54:05 2013 +0200
@@ -23,6 +23,8 @@
 
 
 from cubes.brainomics import ALL_MEASURES, COMMENTED_ENTITIES, WIKI_ENTITIES
+from cubes.brainomics.views.download import (CSV_DOWNLOADABLE, XCEDE_DOWNLOADABLE,
+                                             ZIP_DOWNLOADABLE)
 
 
 ###############################################################################
@@ -74,27 +76,21 @@
 
 class ScanZipFileBox(BrainomicsAbstractDownloadAction):
     __regid__ = 'scan_zipfile'
-    __select__ = BrainomicsAbstractDownloadAction.__select__  & is_instance('Scan', 'GenomicMeasure')
+    __select__ = BrainomicsAbstractDownloadAction.__select__  & is_instance(*ZIP_DOWNLOADABLE)
     title = _('Download Zip')
     download_vid = 'data-zip'
 
 
 class CsvFileBox(BrainomicsAbstractDownloadAction):
     __regid__ = 'measure_csvfile'
-    __select__ = BrainomicsAbstractDownloadAction.__select__  & is_instance('Subject', 'Scan',
-                                                                            'ScoreDef',
-                                                                            'QuestionnaireRun', 'Answer',
-                                                                            'GenomicMeasure', 'Snp')
+    __select__ = BrainomicsAbstractDownloadAction.__select__  & is_instance(*CSV_DOWNLOADABLE)
     title = _('Download CSV')
     download_vid = 'csvexport'
 
 
 class XcedeBox(BrainomicsAbstractDownloadAction):
     __regid__ = 'xcede_file'
-    __select__ = BrainomicsAbstractDownloadAction.__select__  & is_instance('Subject', 'Scan',
-                                                                            'GenomicMeasure',
-                                                                            'QuestionnaireRun',
-                                                                            'Questionnaire')
+    __select__ = BrainomicsAbstractDownloadAction.__select__  & is_instance(*XCEDE_DOWNLOADABLE)
     title = _('Download Xcede')
     download_vid = 'xcede'
 
--- a/views/download.py	Mon Jul 15 16:08:20 2013 +0200
+++ b/views/download.py	Wed Jul 17 11:54:05 2013 +0200
@@ -22,8 +22,17 @@
 from contextlib import closing
 from zipfile import ZipFile, ZIP_DEFLATED
 
-from cubicweb.predicates import is_instance, authenticated_user, anonymous_user
+from logilab.mtconverter import xml_escape
+
+from cubicweb.predicates import is_instance, authenticated_user, anonymous_user, nonempty_rset
 from cubicweb.view import EntityView
+from cubicweb.web.views.csvexport import CSVRsetView
+
+
+CSV_DOWNLOADABLE = ('Subject', 'Scan', 'ScoreDef', 'QuestionnaireRun',
+                    'Answer', 'GenomicMeasure', 'Snp')
+XCEDE_DOWNLOADABLE = ('Subject', 'Scan', 'GenomicMeasure', 'QuestionnaireRun', 'Questionnaire')
+ZIP_DOWNLOADABLE = ('Scan', 'GenomicMeasure')
 
 
 ###############################################################################
@@ -54,6 +63,15 @@
         os.unlink(archive_filepath)
         raise
 
+def build_register_for_download(session):
+        ahref_login = u'<a href="%s">%s</a>' % (session.build_url('login'),
+                                                xml_escape(session._('sign in')))
+        ahref_register = u'<a href="%s">%s</a>' % (session.build_url('register'),
+                                                   xml_escape(session._('sign up')))
+        msg = session._('Please %(login)s or %(register)s before downloading these data.'
+                          % {'login': ahref_login, 'register': ahref_register})
+        return u'<h2>%s</h2>' % msg
+
 
 ###############################################################################
 ### ZIP VIEWS #################################################################
@@ -70,6 +88,7 @@
     def set_request_content_type(self):
         self._cw.set_content_type('application/zip', filename=self.archive_name)
 
+
 class DataZipAuthenticatedView(DataZipAbstractView):
     __select__ = DataZipAbstractView.__select__ & authenticated_user()
 
@@ -105,23 +124,41 @@
     templatable = True
 
     def call(self):
-        ahref_login = u'<a href="%s">%s</a>' % (self._cw.build_url('login'),
-                                                self._cw._('sign in'))
-        ahref_register = u'<a href="%s">%s</a>' % (self._cw.build_url('register'),
-                                                   self._cw._('sign up'))
-        msg = self._cw._('Please %(login)s or %(register)s before downloading the archive.'
-                          % {'login': ahref_login, 'register': ahref_register})
-        self.w(u'<h2>%s</h2>' % msg)
+        self.w(build_register_for_download(self._cw))
+
+
+###############################################################################
+### CSV VIEWS #################################################################
+###############################################################################
+class CsvExportAnonymousView(CSVRsetView):
+    __select__ = CSVRsetView.__select__ & anonymous_user() & is_instance(*CSV_DOWNLOADABLE)
+    binary = False
+    templatable = True
+
+    def call(self):
+        self.w(build_register_for_download(self._cw))
 
 
 ###############################################################################
 ### REGISTRATION CALLBACK #####################################################
 ###############################################################################
 def registration_callback(vreg):
-    vreg.register_all(globals().values(), __name__, (DataZipAnonymousView,))
+    vreg.register_all(globals().values(), __name__, (DataZipAnonymousView, CsvExportAnonymousView))
     if not vreg.config['allow-anon-download']:
         # Register the anon view if do not allow anon download
         vreg.register(DataZipAnonymousView)
+        # Change CSV views selector
+        from cubes.medicalexp.views.csvexport import SubjectCSVView, ScoreDefCSVView
+        SubjectCSVView.__select__ = SubjectCSVView.__select__ & ~anonymous_user()
+        ScoreDefCSVView.__select__ = ScoreDefCSVView.__select__ & ~anonymous_user()
+        from cubes.genomics.views.csvexport import GenomicMeasureCSVView
+        GenomicMeasureCSVView = GenomicMeasureCSVView.__select__ & ~anonymous_user()
+        from cubes.neuroimaging.views.csvexport import ScanCSVView
+        ScanCSVView.__select__ = ScanCSVView.__select__ & ~anonymous_user()
+        from cubes.questionnaire.views.csvexport import QuestionnaireCSVView
+        QuestionnaireCSVView.__select__ = QuestionnaireCSVView.__select__ & ~anonymous_user()
+        # Register anonymous CSV export view
+        vreg.register(CsvExportAnonymousView)
     else:
         # Change the selector of DataZipAuthenticatedView
         DataZipAuthenticatedView.__select__ = DataZipAbstractView.__select__