[zip] Add an option to trigger the behavior of download views
authorVincent Michel <vincent.michel@logilab.fr>
Wed, 17 Jul 2013 11:50:11 +0200
changeset 279 9eb199a02da8
parent 278 9101f6d48451
child 280 04047bb24ad8
[zip] Add an option to trigger the behavior of download views
migration/0.3.0_Any.py
site_cubicweb.py
views/components.py
views/zipview.py
--- a/migration/0.3.0_Any.py	Fri Jul 05 15:34:34 2013 +0200
+++ b/migration/0.3.0_Any.py	Wed Jul 17 11:50:11 2013 +0200
@@ -18,3 +18,5 @@
 
 add_relation_type('comments')
 add_relation_type('wiki')
+add_cube('registration')
+add_cube('cwbootstrap')
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/site_cubicweb.py	Wed Jul 17 11:50:11 2013 +0200
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+# copyright 2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# contact http://www.logilab.fr -- mailto:contact@logilab.fr
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 2.1 of the License, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License along
+# with this program. If not, see <http://www.gnu.org/licenses/>.
+
+options = (
+    ('allow-anon-download',
+     {'type' : 'yn',
+      'default': False,
+      'help': 'Disallow the download by anonymous user (e.g. zipview and csview)',
+      'group': 'brainomics',
+      'level': 1,
+      }),
+)
--- a/views/components.py	Fri Jul 05 15:34:34 2013 +0200
+++ b/views/components.py	Wed Jul 17 11:50:11 2013 +0200
@@ -275,7 +275,7 @@
 ###############################################################################
 ### DOWNLOAD BOX ##############################################################
 ###############################################################################
-class BrainomicsAbstractDownloadBox(component.CtxComponent):
+class BrainomicsDownloadBox(component.CtxComponent):
     __select__ = component.CtxComponent.__select__ & nonempty_rset()
     context = 'left'
     order = 1
@@ -305,7 +305,8 @@
 BookmarksBox.__select__ = BookmarksBox.__select__ & ~anonymous_user()
 
 def registration_callback(vreg):
-    vreg.register_all(globals().values(), __name__, (BrainomicsSearchBox, BrainomicsEditBox))
+    vreg.register_all(globals().values(), __name__, (BrainomicsSearchBox,
+                                                     BrainomicsEditBox))
     vreg.register_and_replace(BrainomicsSearchBox, SearchBox)
     vreg.register_and_replace(BrainomicsEditBox, EditBox)
     # Unregister breadcrumbs
--- a/views/zipview.py	Fri Jul 05 15:34:34 2013 +0200
+++ b/views/zipview.py	Wed Jul 17 11:50:11 2013 +0200
@@ -26,6 +26,9 @@
 from cubicweb.view import EntityView
 
 
+###############################################################################
+### UTILITY FUNCTIONS #########################################################
+###############################################################################
 def zipfiles(filepaths):
     """generates a zip archive from `filepaths`
 
@@ -51,6 +54,10 @@
         os.unlink(archive_filepath)
         raise
 
+
+###############################################################################
+### ZIP VIEWS #################################################################
+###############################################################################
 class DataZipAbstractView(EntityView):
     """Abstract base class for the zip view"""
     __abstract__ = True
@@ -73,16 +80,16 @@
         for entity in self.cw_rset.entities():
             if entity.__regid__ == 'GenomicMeasure':
                 for ext in ('.bim', '.bed', '.fam'):
-                    filepaths.add((entity.filepath.split('/')[-1]+ext, 
-                                   entity.concerns[0].identifier, 
+                    filepaths.add((osp.basename(entity.filepath)+ext,
+                                   entity.concerns[0].identifier,
                                    entity.full_filepath+ext))
             else:
                 filename = entity.type+'_'+entity.filepath.split('/')[-1]
                 filename = filename.replace(' ', '_')
                 filepaths.add((filename, entity.concerns[0].identifier, entity.full_filepath))
             for external_resource in entity.external_resources:
-                filepaths.add((external_resource.filepath.split('/')[-1], 
-                               entity.concerns[0].identifier, 
+                filepaths.add((osp.basename(external_resource.filepath),
+                               entity.concerns[0].identifier,
                                external_resource.full_filepath))
         fileobj, archive_filepath = zipfiles(list(filepaths))
         try:
@@ -98,9 +105,23 @@
     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'))
-        self.w(u'<h2>%s</h2>' 
-               % self._cw._('Please %(login)s or %(register)s before downloading the archive.' 
-                            % {'login': ahref_login, 'register': ahref_register}))
+        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)
+
 
+###############################################################################
+### REGISTRATION CALLBACK #####################################################
+###############################################################################
+def registration_callback(vreg):
+    vreg.register_all(globals().values(), __name__, (DataZipAnonymousView,))
+    if not vreg.config['allow-anon-download']:
+        # Register the anon view if do not allow anon download
+        vreg.register(DataZipAnonymousView)
+    else:
+        # Change the selector of DataZipAuthenticatedView
+        DataZipAuthenticatedView.__select__ = DataZipAbstractView.__select__