Add download gallery as zip
This commit is contained in:
parent
e2c826ec1d
commit
52d63128fc
3 changed files with 31 additions and 3 deletions
|
|
@ -38,5 +38,5 @@
|
||||||
</a>
|
</a>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
<a href="#" class="btn btn-secondary">{% trans 'Download all gallery' %}</a>
|
<a href="{% url 'gallery-download' gallery.slug %}" class="btn btn-secondary">{% trans 'Download all gallery' %}</a>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
from .views import TagDetail
|
from .views import TagDetail, GalleryDownload
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('tag/<slug:slug>/', TagDetail.as_view(), name='tag-detail'),
|
path('tag/<slug:slug>/', TagDetail.as_view(), name='tag-detail'),
|
||||||
|
path('gallery/<slug:slug>/download/', GalleryDownload.as_view(), name='gallery-download'),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,15 @@
|
||||||
# Copyright (C) 2021 by BDE ENS Paris-Saclay
|
# Copyright (C) 2021 by BDE ENS Paris-Saclay
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
import os
|
||||||
|
import zipfile
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||||
|
from django.http import HttpResponse
|
||||||
from django.views.generic import DetailView
|
from django.views.generic import DetailView
|
||||||
from taggit.models import Tag
|
|
||||||
from photologue.models import Gallery
|
from photologue.models import Gallery
|
||||||
|
from taggit.models import Tag
|
||||||
|
|
||||||
|
|
||||||
class TagDetail(LoginRequiredMixin, DetailView):
|
class TagDetail(LoginRequiredMixin, DetailView):
|
||||||
|
|
@ -19,3 +24,25 @@ class TagDetail(LoginRequiredMixin, DetailView):
|
||||||
context['galleries'] = Gallery.objects.on_site().is_public() \
|
context['galleries'] = Gallery.objects.on_site().is_public() \
|
||||||
.filter(extended__tags__name=current_tag)
|
.filter(extended__tags__name=current_tag)
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
|
class GalleryDownload(LoginRequiredMixin, DetailView):
|
||||||
|
model = Gallery
|
||||||
|
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
Download a zip file of the gallery on GET request.
|
||||||
|
"""
|
||||||
|
# Create zip file with pictures
|
||||||
|
gallery = self.get_object()
|
||||||
|
byte_data = BytesIO()
|
||||||
|
zip_file = zipfile.ZipFile(byte_data, "w")
|
||||||
|
for photo in gallery.public():
|
||||||
|
filename = os.path.basename(os.path.normpath(photo.image.path))
|
||||||
|
zip_file.write(photo.image.path, filename)
|
||||||
|
zip_file.close()
|
||||||
|
|
||||||
|
# Return zip file
|
||||||
|
response = HttpResponse(byte_data.getvalue(), content_type='application/x-zip-compressed')
|
||||||
|
response['Content-Disposition'] = f"attachment; filename={gallery.slug}.zip"
|
||||||
|
return response
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue