Added images download button
authorThierry Florac <thierry.florac@onf.fr>
Fri, 08 Dec 2017 10:48:16 +0100
changeset 315 8654d1faa27d
parent 314 18da24db44b6
child 316 b50158f7050d
Added images download button
src/pyams_content/component/gallery/zmi/__init__.py
src/pyams_content/component/gallery/zmi/templates/gallery-images.pt
--- 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
--- a/src/pyams_content/component/gallery/zmi/templates/gallery-images.pt	Fri Dec 08 10:47:40 2017 +0100
+++ b/src/pyams_content/component/gallery/zmi/templates/gallery-images.pt	Fri Dec 08 10:48:16 2017 +0100
@@ -9,6 +9,11 @@
 			tal:attributes="data-ams-switcher-state 'open' if context.values() else None">
 			<i18n:var translate="">Gallery images</i18n:var>
 		</legend>
+		<div class="btn-group pull-right">
+			<a class="btn btn-xs btn-primary"
+			   tal:attributes="href extension:absolute_url(context, 'get-images.zip')"
+			   i18n:translate="">Download images</a>
+		</div>
 		<div class="pull-left persistent">
 			<tal:var content="structure provider:pyams.widget_title" />
 		</div>
@@ -31,13 +36,13 @@
 						<a class="fancyimg hint" data-toggle
 						   data-ams-hint-gravity="sw"
 						   title="Zoom image" i18n:attributes="title"
-						   tal:define="target thumbnails.get_thumbnail('800x600', 'jpeg')"
+						   tal:define="target thumbnails.get_thumbnail('800x600')"
 						   tal:attributes="class 'fancyimg hint {0}'.format('not-visible' if not image.visible else '');
 										   href extension:absolute_url(target);
 										   rel string:gallery_${context.__name__};">
 							<img class="thumbnail"
 								 data-ams-hint-gravity="s"
-								 tal:define="thumbnail thumbnails.get_thumbnail('128x128', 'jpeg');
+								 tal:define="thumbnail thumbnails.get_thumbnail('128x128');
 											 image_size thumbnail.get_image_size();
 											 margin_left 64 - image_size[0] / 2;
 											 margin_top 64 - image_size[1] / 2;