src/pyams_content/component/gallery/zmi/__init__.py
changeset 315 8654d1faa27d
parent 140 67bad9f880ee
child 362 8fc21a7ef206
equal deleted inserted replaced
314:18da24db44b6 315:8654d1faa27d
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
    10 # FOR A PARTICULAR PURPOSE.
    10 # FOR A PARTICULAR PURPOSE.
    11 #
    11 #
       
    12 from pyams_file.interfaces import IFileInfo
    12 
    13 
    13 __docformat__ = 'restructuredtext'
    14 __docformat__ = 'restructuredtext'
    14 
    15 
    15 
    16 
    16 # import standard library
    17 # import standard library
    17 import json
    18 import json
       
    19 import zipfile
       
    20 from io import BytesIO
    18 
    21 
    19 # import interfaces
    22 # import interfaces
    20 from pyams_content.component.gallery.interfaces import IGallery, IGalleryRenderer
    23 from pyams_content.component.gallery.interfaces import IGallery, IGalleryRenderer, IGalleryFile
    21 from pyams_content.component.gallery.zmi.interfaces import IGalleryImageAddFields, IGalleryImagesView
    24 from pyams_content.component.gallery.zmi.interfaces import IGalleryImageAddFields, IGalleryImagesView
    22 from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
    25 from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
    23 from pyams_form.interfaces.form import IWidgetsPrefixViewletsManager
    26 from pyams_form.interfaces.form import IWidgetsPrefixViewletsManager
    24 from pyams_i18n.interfaces import II18n
    27 from pyams_i18n.interfaces import II18n
    25 from pyams_skin.layer import IPyAMSLayer
    28 from pyams_skin.layer import IPyAMSLayer
    33 from pyams_utils.adapter import adapter_config, ContextRequestAdapter
    36 from pyams_utils.adapter import adapter_config, ContextRequestAdapter
    34 from pyams_viewlet.viewlet import viewlet_config, Viewlet
    37 from pyams_viewlet.viewlet import viewlet_config, Viewlet
    35 from pyams_zmi.form import AdminDialogEditForm, AdminDialogDisplayForm
    38 from pyams_zmi.form import AdminDialogEditForm, AdminDialogDisplayForm
    36 from pyramid.exceptions import NotFound
    39 from pyramid.exceptions import NotFound
    37 from pyramid.renderers import render_to_response
    40 from pyramid.renderers import render_to_response
       
    41 from pyramid.response import Response
    38 from pyramid.view import view_config
    42 from pyramid.view import view_config
    39 from z3c.form import field
    43 from z3c.form import field
    40 from zope.interface import implementer, Interface
    44 from zope.interface import implementer, Interface
    41 
    45 
    42 from pyams_content import _
    46 from pyams_content import _
   158 class DefaultGalleryRenderer(BaseGalleryRenderer):
   162 class DefaultGalleryRenderer(BaseGalleryRenderer):
   159     """Default gallery renderer"""
   163     """Default gallery renderer"""
   160 
   164 
   161     label = _("Default gallery renderer")
   165     label = _("Default gallery renderer")
   162     weight = 1
   166     weight = 1
       
   167 
       
   168 
       
   169 #
       
   170 # Gallery images downloader
       
   171 #
       
   172 
       
   173 @view_config(name='get-images.zip', context=IGallery, request_type=IPyAMSLayer,
       
   174              permission=VIEW_SYSTEM_PERMISSION)
       
   175 def get_images_archive(request):
       
   176     """Get all gallery images as ZIP file"""
       
   177     zip_data = BytesIO()
       
   178     zip_file = zipfile.ZipFile(zip_data, mode='w')
       
   179     for image in IGallery(request.context).values():
       
   180         zip_file.writestr(IFileInfo(image.data).filename, image.data.data)
       
   181     zip_file.close()
       
   182     zip_data.seek(0)
       
   183     response = Response(content_type='application/zip',
       
   184                         content_disposition='attachment; filename="gallery.zip"')
       
   185     response.body_file = zip_data
       
   186     return response