88 lines
2.7 KiB
Python
88 lines
2.7 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 photologue.views import GalleryArchiveIndexView, GalleryYearArchiveView
|
|
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__slug=current_tag)
|
|
return context
|
|
|
|
|
|
class CustomGalleryArchiveIndexView(GalleryArchiveIndexView):
|
|
"""
|
|
Override to use event date
|
|
"""
|
|
date_field = 'extended__date_start'
|
|
uses_datetime_field = False # Fix related object access
|
|
|
|
|
|
class CustomGalleryYearArchiveView(GalleryYearArchiveView):
|
|
"""
|
|
Override to use event date
|
|
"""
|
|
date_field = 'extended__date_start'
|
|
uses_datetime_field = False # Fix related object access
|
|
|
|
|
|
class CustomGalleryDetailView(DetailView):
|
|
"""
|
|
Custom gallery detail view to filter on photo owner
|
|
"""
|
|
queryset = Gallery.objects.on_site().is_public()
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context['photos'] = self.object.public()
|
|
|
|
# List owners
|
|
context['owners'] = []
|
|
for photo in context['photos']:
|
|
if photo.extented.owner not in context['owners']:
|
|
context['owners'].append(photo.extented.owner)
|
|
|
|
# Filter on owner
|
|
if 'owner' in self.kwargs:
|
|
context['photos'] = context['photos'].filter(extented__owner__id=self.kwargs['owner'])
|
|
|
|
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
|