src/pyams_content/component/gallery/zmi/__init__.py
changeset 315 8654d1faa27d
parent 140 67bad9f880ee
child 362 8fc21a7ef206
--- a/src/pyams_content/component/gallery/zmi/__init__.py	Fri Dec 08 10:47:40 2017 +0100
+++ b/src/pyams_content/component/gallery/zmi/__init__.py	Fri Dec 08 10:48:16 2017 +0100
@@ -9,15 +9,18 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from pyams_file.interfaces import IFileInfo
 
 __docformat__ = 'restructuredtext'
 
 
 # import standard library
 import json
+import zipfile
+from io import BytesIO
 
 # import interfaces
-from pyams_content.component.gallery.interfaces import IGallery, IGalleryRenderer
+from pyams_content.component.gallery.interfaces import IGallery, IGalleryRenderer, IGalleryFile
 from pyams_content.component.gallery.zmi.interfaces import IGalleryImageAddFields, IGalleryImagesView
 from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
 from pyams_form.interfaces.form import IWidgetsPrefixViewletsManager
@@ -35,6 +38,7 @@
 from pyams_zmi.form import AdminDialogEditForm, AdminDialogDisplayForm
 from pyramid.exceptions import NotFound
 from pyramid.renderers import render_to_response
+from pyramid.response import Response
 from pyramid.view import view_config
 from z3c.form import field
 from zope.interface import implementer, Interface
@@ -160,3 +164,23 @@
 
     label = _("Default gallery renderer")
     weight = 1
+
+
+#
+# Gallery images downloader
+#
+
+@view_config(name='get-images.zip', context=IGallery, request_type=IPyAMSLayer,
+             permission=VIEW_SYSTEM_PERMISSION)
+def get_images_archive(request):
+    """Get all gallery images as ZIP file"""
+    zip_data = BytesIO()
+    zip_file = zipfile.ZipFile(zip_data, mode='w')
+    for image in IGallery(request.context).values():
+        zip_file.writestr(IFileInfo(image.data).filename, image.data.data)
+    zip_file.close()
+    zip_data.seek(0)
+    response = Response(content_type='application/zip',
+                        content_disposition='attachment; filename="gallery.zip"')
+    response.body_file = zip_data
+    return response