backport 0.11.5 branch
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Tue, 25 Nov 2014 18:47:13 +0100
changeset 1219 50b7da0706bb
parent 1211 3a340472b377 (current diff)
parent 1218 8b4736550db5 (diff)
child 1220 26760f5af070
backport 0.11.5 branch
.hgtags
__pkginfo__.py
debian/changelog
--- a/.hgtags	Fri Oct 24 20:36:43 2014 +0200
+++ b/.hgtags	Tue Nov 25 18:47:13 2014 +0100
@@ -44,6 +44,8 @@
 07979b039ecfc011f3396ae7d7188695c0f99144 cubicweb-brainomics-centos-version-0.11.4-1
 07979b039ecfc011f3396ae7d7188695c0f99144 cubicweb-brainomics-version-0.11.4
 07979b039ecfc011f3396ae7d7188695c0f99144 cubicweb-brainomics-debian-version-0.11.4-1
+cd2454cc5cf8ddbe9fc2dd13464fc5f9aa736d96 cubicweb-brainomics-version-0.11.5
+cd2454cc5cf8ddbe9fc2dd13464fc5f9aa736d96 cubicweb-brainomics-debian-version-0.11.5-1
 ab0c557dd3ad5fd7e5ea9709647af760b2b2f49e cubicweb-brainomics-version-0.12.0
 ab0c557dd3ad5fd7e5ea9709647af760b2b2f49e cubicweb-brainomics-debian-version-0.12.0-1
 ab0c557dd3ad5fd7e5ea9709647af760b2b2f49e cubicweb-brainomics-centos-version-0.12.0-1
--- a/debian/changelog	Fri Oct 24 20:36:43 2014 +0200
+++ b/debian/changelog	Tue Nov 25 18:47:13 2014 +0100
@@ -4,6 +4,12 @@
 
  -- Adrien Di Mascio <Adrien.DiMascio@logilab.fr>  Fri, 24 Oct 2014 20:26:57 +0200
 
+cubicweb-brainomics (0.11.5-1) unstable; urgency=low
+
+  * new upstream release
+
+ -- Vladimir Popescu <vladimir.popescu@logilab.fr>  Wed, 15 Oct 2014 12:25:04 +0200
+
 cubicweb-brainomics (0.11.4-1) unstable; urgency=low
 
   * new upstream release
--- a/views/download.py	Fri Oct 24 20:36:43 2014 +0200
+++ b/views/download.py	Tue Nov 25 18:47:13 2014 +0100
@@ -35,35 +35,6 @@
 
 
 ###############################################################################
-### UTILITY FUNCTIONS #########################################################
-###############################################################################
-def zipfiles(filepaths):
-    """generates a zip archive from `filepaths`
-
-    :param filepaths: is a dictionary mapping basename (as it will appear in the
-                      archive) to corresponding absolute filepath (where it must
-                      read from on the filesystem)
-
-    :return: a couple fileobj, filepath where `fileobj` is a standard python
-             open file objet on the generated archive, and `filepath` is an
-             absolute file path to this archive on the filesystem
-    """
-    fd, archive_filepath = tempfile.mkstemp()
-    try:
-        noext_archivename = osp.splitext(DataZipView.archive_name)[0]
-        with closing(ZipFile(archive_filepath, "w", ZIP_DEFLATED, True)) as zip:
-            for filename, subj, _fileobj in filepaths:
-                zip.writestr('%s/%s/%s' % (noext_archivename, subj, filename), _fileobj.read())
-        fileobj = os.fdopen(fd)
-        fileobj.seek(0)
-        return fileobj, archive_filepath
-    except:
-        os.close(fd)
-        os.unlink(archive_filepath)
-        raise
-
-
-###############################################################################
 ### ZIP VIEWS #################################################################
 ###############################################################################
 class DataZipView(EntityView):
@@ -74,21 +45,54 @@
     binary = True
     archive_name = 'brainomics_data.zip'
 
+    def _zipfiles(self, filepaths):
+        """generates a zip archive from `filepaths`
+
+        :param filepaths: is a set containing tuples with relative file paths
+                          as they appear in the archive, and CubicWeb file
+                          objects, whose content is put in the archive.
+
+        :return: a couple fileobj, filepath where `fileobj` is a standard python
+                 open file objet on the generated archive, and `filepath` is an
+                 absolute file path to this archive on the filesystem
+        """
+        fd, archive_filepath = tempfile.mkstemp()
+        try:
+            noext_archivename = osp.splitext(self.archive_name)[0]
+            with closing(ZipFile(archive_filepath, "w", ZIP_DEFLATED, True)) as zip:
+                for filename, gmes_type, subjs, _fileobj in filepaths:
+                    for subj in subjs:
+                        zip.writestr('%s/%s/%s/%s' % (noext_archivename, gmes_type,
+                                                      subj, filename),
+                                     _fileobj.read())
+            fileobj = os.fdopen(fd)
+            fileobj.seek(0)
+            return fileobj, archive_filepath
+        except:
+            os.close(fd)
+            os.unlink(archive_filepath)
+            raise
+
     def set_request_content_type(self):
         self._cw.set_content_type('application/zip', filename=self.archive_name)
 
+    def make_entity_labels(self, entity, prefix):
+        return ('{0}_{1}'.format(prefix, entity.concerns[0].identifier),)
+
     def call(self):
         if not self.cw_rset:
             return
         filepaths = set()
+        prefix = self._cw._('Subject')
         for entity in self.cw_rset.entities():
+            subj = self.make_entity_labels(entity, prefix)
             for _file in entity.results_files:
                 if _file.__regid__ == 'FileSet':
                     for _f in _file.file_entries:
-                        filepaths.add(_file.dc_title(), subj, _f)
+                        filepaths.add((_file.dc_title(), entity.dc_title(), subj, _f))
                 else:
-                        filepaths.add(_file.dc_title(), subj, _file)
-        fileobj, archive_filepath = zipfiles(list(filepaths))
+                        filepaths.add((_file.dc_title(), entity.dc_title(), subj, _file))
+        fileobj, archive_filepath = self._zipfiles(list(filepaths))
         try:
             self.w(fileobj.read())
         finally: