Better admin notification on uploads
This commit is contained in:
parent
73f80de794
commit
fbbb7651b1
2 changed files with 19 additions and 20 deletions
|
|
@ -42,11 +42,6 @@ ADMINS = [
|
||||||
("admin", "photos-admin@lists.crans.org"),
|
("admin", "photos-admin@lists.crans.org"),
|
||||||
]
|
]
|
||||||
|
|
||||||
# Managers receive uploads notification
|
|
||||||
MANAGERS = [
|
|
||||||
('admin', 'photos-admin@lists.crans.org'),
|
|
||||||
]
|
|
||||||
|
|
||||||
# Use secure cookies in production
|
# Use secure cookies in production
|
||||||
SESSION_COOKIE_SECURE = not DEBUG
|
SESSION_COOKIE_SECURE = not DEBUG
|
||||||
CSRF_COOKIE_SECURE = not DEBUG
|
CSRF_COOKIE_SECURE = not DEBUG
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ from pathlib import Path
|
||||||
|
|
||||||
from django.contrib import messages
|
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.core.mail import mail_admins
|
||||||
from django.db import IntegrityError
|
from django.db import IntegrityError
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
|
|
@ -189,10 +189,14 @@ class GalleryUpload(PermissionRequiredMixin, FormView):
|
||||||
# Upload photos
|
# Upload photos
|
||||||
# We take files from the request to support multiple upload
|
# We take files from the request to support multiple upload
|
||||||
files = self.request.FILES.getlist("file_field")
|
files = self.request.FILES.getlist("file_field")
|
||||||
|
|
||||||
|
# Get or create gallery
|
||||||
gallery = form.get_or_create_gallery()
|
gallery = form.get_or_create_gallery()
|
||||||
gallery_year = Path(str(gallery.date_start.year))
|
gallery_year = Path(str(gallery.date_start.year))
|
||||||
gallery_dir = gallery_year / gallery.slug
|
gallery_dir = gallery_year / gallery.slug
|
||||||
failed_upload = 0
|
|
||||||
|
# Upload pictures
|
||||||
|
uploaded_photo_name = []
|
||||||
already_exists = 0
|
already_exists = 0
|
||||||
for photo_file in files:
|
for photo_file in files:
|
||||||
# Check that we have a valid image
|
# Check that we have a valid image
|
||||||
|
|
@ -204,7 +208,6 @@ class GalleryUpload(PermissionRequiredMixin, FormView):
|
||||||
messages.error(
|
messages.error(
|
||||||
self.request, f"{photo_file.name} was not recognized as an image"
|
self.request, f"{photo_file.name} was not recognized as an image"
|
||||||
)
|
)
|
||||||
failed_upload += 1
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
title = f"{gallery.title} - {photo_file.name}"
|
title = f"{gallery.title} - {photo_file.name}"
|
||||||
|
|
@ -222,25 +225,26 @@ class GalleryUpload(PermissionRequiredMixin, FormView):
|
||||||
photo.image.save(photo_name, photo_file)
|
photo.image.save(photo_name, photo_file)
|
||||||
except IntegrityError:
|
except IntegrityError:
|
||||||
already_exists += 1
|
already_exists += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
uploaded_photo_name.append(photo_file.name)
|
||||||
|
|
||||||
# Notify user then managers
|
# Notify user then managers
|
||||||
n_success = len(files) - failed_upload - already_exists
|
n_success = len(uploaded_photo_name)
|
||||||
if already_exists:
|
if already_exists:
|
||||||
messages.success(self.request, f"{n_success} photos has been successfully uploaded, {already_exists} photos were skipped as they already exist in this gallery.")
|
messages.success(self.request, f"{n_success} photo(s) uploaded, {already_exists} photo(s) skipped as they already exist in this gallery.")
|
||||||
elif not failed_upload:
|
|
||||||
messages.success(self.request, f"All {n_success} photos has been successfully uploaded.")
|
|
||||||
else:
|
else:
|
||||||
messages.warning(
|
messages.success(self.request, f"{n_success} photo(s) uploaded.")
|
||||||
self.request, f"Only {n_success} photos were successfully uploaded !"
|
|
||||||
)
|
|
||||||
|
|
||||||
gallery_title = form.cleaned_data["gallery"] or form.cleaned_data.get(
|
gallery_title = form.cleaned_data["gallery"] or form.cleaned_data.get(
|
||||||
"new_gallery_title", ""
|
"new_gallery_title", ""
|
||||||
)
|
)
|
||||||
photos = ", ".join(f.name for f in files)
|
if uploaded_photo_name:
|
||||||
mail_managers(
|
# Notify administrators
|
||||||
subject="New photos upload",
|
photos = ", ".join(uploaded_photo_name)
|
||||||
message=f"{self.request.user.username} has uploaded in `{gallery_title}`: {photos}",
|
mail_admins(
|
||||||
)
|
subject=f"New upload from {self.request.user.username}",
|
||||||
|
message=f"{self.request.user.username} has uploaded in `{gallery_title}`:\n{photos}",
|
||||||
|
)
|
||||||
|
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue