48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
# Copyright (C) 2021 by BDE ENS Paris-Saclay
|
|
# 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.http import HttpResponse
|
|
from django.views.generic import DetailView
|
|
from photologue.models import Gallery
|
|
from taggit.models import Tag
|
|
|
|
|
|
class TagDetail(LoginRequiredMixin, DetailView):
|
|
model = Tag
|
|
|
|
def get_context_data(self, **kwargs):
|
|
"""
|
|
Insert the single object into the context dict.
|
|
"""
|
|
current_tag = self.get_object().slug
|
|
context = super().get_context_data(**kwargs)
|
|
context['galleries'] = Gallery.objects.on_site().is_public() \
|
|
.filter(extended__tags__name=current_tag)
|
|
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
|