Format code using black
This commit is contained in:
parent
2ad0c8dbc7
commit
59136050fb
14 changed files with 809 additions and 413 deletions
|
|
@ -7,18 +7,17 @@ from io import BytesIO
|
|||
from pathlib import Path
|
||||
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.mixins import (LoginRequiredMixin,
|
||||
PermissionRequiredMixin)
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
|
||||
from django.core.mail import mail_managers
|
||||
from django.db import IntegrityError
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import redirect
|
||||
from django.urls import reverse_lazy
|
||||
from django.utils.text import slugify
|
||||
from django.views.generic.dates import ArchiveIndexView, YearArchiveView
|
||||
from django.views.generic.detail import DetailView
|
||||
from django.views.generic.edit import FormView, DeleteView
|
||||
from django.views.generic.edit import DeleteView, FormView
|
||||
from PIL import Image
|
||||
from django.shortcuts import redirect
|
||||
|
||||
from .forms import UploadForm
|
||||
from .models import Gallery, Photo, Tag
|
||||
|
|
@ -26,7 +25,7 @@ from .models import Gallery, Photo, Tag
|
|||
|
||||
class GalleryDateView(LoginRequiredMixin):
|
||||
model = Gallery
|
||||
date_field = 'date_start'
|
||||
date_field = "date_start"
|
||||
|
||||
def get_queryset(self):
|
||||
"""Hide galleries with only private photos"""
|
||||
|
|
@ -59,19 +58,19 @@ class PhotoDetailView(LoginRequiredMixin, DetailView):
|
|||
|
||||
class PhotoDeleteView(PermissionRequiredMixin, DeleteView):
|
||||
model = Photo
|
||||
permission_required = 'photologue.delete_photo'
|
||||
permission_required = "photologue.delete_photo"
|
||||
|
||||
def get_success_url(self):
|
||||
galleries = self.object.galleries.all()
|
||||
if not galleries:
|
||||
return reverse_lazy('photologue:pl-gallery-archive')
|
||||
return reverse_lazy("photologue:pl-gallery-archive")
|
||||
slug = galleries[0].slug
|
||||
return reverse_lazy('photologue:pl-gallery', args=[slug])
|
||||
return reverse_lazy("photologue:pl-gallery", args=[slug])
|
||||
|
||||
|
||||
class PhotoReportView(LoginRequiredMixin, DetailView):
|
||||
model = Photo
|
||||
template_name = 'photologue/photo_confirm_report.html'
|
||||
template_name = "photologue/photo_confirm_report.html"
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
"""
|
||||
|
|
@ -84,10 +83,10 @@ class PhotoReportView(LoginRequiredMixin, DetailView):
|
|||
|
||||
# Get gallery
|
||||
galleries = photo.galleries.all()
|
||||
gallery_slug = galleries[0].slug if galleries else ''
|
||||
gallery_slug = galleries[0].slug if galleries else ""
|
||||
if not gallery_slug:
|
||||
url = reverse_lazy('photologue:pl-gallery-archive')
|
||||
url = reverse_lazy('photologue:pl-gallery', args=[gallery_slug])
|
||||
url = reverse_lazy("photologue:pl-gallery-archive")
|
||||
url = reverse_lazy("photologue:pl-gallery", args=[gallery_slug])
|
||||
|
||||
# Send mail to managers
|
||||
mail_managers(
|
||||
|
|
@ -108,8 +107,9 @@ class TagDetail(LoginRequiredMixin, DetailView):
|
|||
"""
|
||||
current_tag = self.get_object().slug
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['galleries'] = Gallery.objects.filter(tags__slug=current_tag) \
|
||||
.order_by('-date_start')
|
||||
context["galleries"] = Gallery.objects.filter(tags__slug=current_tag).order_by(
|
||||
"-date_start"
|
||||
)
|
||||
return context
|
||||
|
||||
|
||||
|
|
@ -117,6 +117,7 @@ class GalleryDetailView(LoginRequiredMixin, DetailView):
|
|||
"""
|
||||
Gallery detail view to filter on photo owner
|
||||
"""
|
||||
|
||||
model = Gallery
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
|
|
@ -124,19 +125,19 @@ class GalleryDetailView(LoginRequiredMixin, DetailView):
|
|||
|
||||
# Non-staff members only see public photos
|
||||
if self.request.user.is_staff:
|
||||
context['photos'] = self.object.photos.all()
|
||||
context["photos"] = self.object.photos.all()
|
||||
else:
|
||||
context['photos'] = self.object.photos.filter(is_public=True)
|
||||
context["photos"] = self.object.photos.filter(is_public=True)
|
||||
|
||||
# List owners
|
||||
context['owners'] = []
|
||||
for photo in context['photos']:
|
||||
if photo.owner not in context['owners']:
|
||||
context['owners'].append(photo.owner)
|
||||
context["owners"] = []
|
||||
for photo in context["photos"]:
|
||||
if photo.owner not in context["owners"]:
|
||||
context["owners"].append(photo.owner)
|
||||
|
||||
# Filter on owner
|
||||
if 'owner' in self.kwargs:
|
||||
context['photos'] = context['photos'].filter(owner__id=self.kwargs['owner'])
|
||||
if "owner" in self.kwargs:
|
||||
context["photos"] = context["photos"].filter(owner__id=self.kwargs["owner"])
|
||||
|
||||
return context
|
||||
|
||||
|
|
@ -158,8 +159,10 @@ class GalleryDownload(LoginRequiredMixin, DetailView):
|
|||
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"
|
||||
response = HttpResponse(
|
||||
byte_data.getvalue(), content_type="application/x-zip-compressed"
|
||||
)
|
||||
response["Content-Disposition"] = f"attachment; filename={gallery.slug}.zip"
|
||||
return response
|
||||
|
||||
|
||||
|
|
@ -167,15 +170,16 @@ class GalleryUpload(PermissionRequiredMixin, FormView):
|
|||
"""
|
||||
Form to upload new photos in a gallery
|
||||
"""
|
||||
|
||||
form_class = UploadForm
|
||||
template_name = "photologue/upload.html"
|
||||
success_url = reverse_lazy("photologue:pl-gallery-upload")
|
||||
permission_required = 'photologue.add_gallery'
|
||||
permission_required = "photologue.add_gallery"
|
||||
|
||||
def form_valid(self, form):
|
||||
# Upload photos
|
||||
# We take files from the request to support multiple upload
|
||||
files = self.request.FILES.getlist('file_field')
|
||||
files = self.request.FILES.getlist("file_field")
|
||||
gallery = form.get_or_create_gallery()
|
||||
gallery_year = Path(str(gallery.date_start.year))
|
||||
gallery_dir = gallery_year / gallery.slug
|
||||
|
|
@ -187,7 +191,9 @@ class GalleryUpload(PermissionRequiredMixin, FormView):
|
|||
opened.verify()
|
||||
except Exception:
|
||||
# Pillow doesn't recognize it as an image, skip it
|
||||
messages.error(self.request, f"{photo_file.name} was not recognized as an image")
|
||||
messages.error(
|
||||
self.request, f"{photo_file.name} was not recognized as an image"
|
||||
)
|
||||
failed_upload += 1
|
||||
continue
|
||||
|
||||
|
|
@ -203,7 +209,10 @@ class GalleryUpload(PermissionRequiredMixin, FormView):
|
|||
photo.save()
|
||||
photo.galleries.set([gallery])
|
||||
except IntegrityError:
|
||||
messages.error(self.request, f"{photo_file.name} was not uploaded. Maybe the photo was already uploaded.")
|
||||
messages.error(
|
||||
self.request,
|
||||
f"{photo_file.name} was not uploaded. Maybe the photo was already uploaded.",
|
||||
)
|
||||
failed_upload += 1
|
||||
|
||||
# Notify user then managers
|
||||
|
|
@ -211,9 +220,13 @@ class GalleryUpload(PermissionRequiredMixin, FormView):
|
|||
messages.success(self.request, "All photos has been successfully uploaded.")
|
||||
else:
|
||||
n_success = len(files) - failed_upload
|
||||
messages.warning(self.request, f"Only {n_success} photos were successfully uploaded !")
|
||||
messages.warning(
|
||||
self.request, f"Only {n_success} photos were successfully uploaded !"
|
||||
)
|
||||
|
||||
gallery_title = form.cleaned_data['gallery'] or form.cleaned_data.get('new_gallery_title', '')
|
||||
gallery_title = form.cleaned_data["gallery"] or form.cleaned_data.get(
|
||||
"new_gallery_title", ""
|
||||
)
|
||||
photos = ", ".join(f.name for f in files)
|
||||
mail_managers(
|
||||
subject="New photos upload",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue