Check upload form

This commit is contained in:
Alexandre Iooss 2021-10-15 12:43:17 +02:00
parent 727387566d
commit d2fa5ce02f
5 changed files with 54 additions and 22 deletions

View file

@ -203,6 +203,7 @@ EMAIL_HOST_PASSWORD = os.getenv('EMAIL_PASSWORD', None)
# Mail will be sent from this address
SERVER_EMAIL = "photos@crans.org"
DEFAULT_FROM_EMAIL = f"Serveur photos <{SERVER_EMAIL}>"
EMAIL_SUBJECT_PREFIX = '[Serveur photos] '
# After login redirect user to transfer page
LOGIN_REDIRECT_URL = '/'

View file

@ -35,6 +35,12 @@ SPDX-License-Identifier: GPL-3.0-or-later
{% url 'photologue:pl-gallery-archive' as url %}
<a class="nav-link {% if request.path_info == url %}active{% endif %}" href="{{ url }}">{% trans 'Galleries' %}</a>
</li>
{% if perms.photologue.add_gallery %}
<li class="nav-item">
{% url 'gallery-upload' as url %}
<a class="nav-link {% if request.path_info == url %}active{% endif %}" href="{{ url }}">{% trans 'Upload' %}</a>
</li>
{% endif %}
{% if request.user.is_staff %}
<li class="nav-item">
<a class="nav-link" href="{% url 'admin:index' %}">{% trans 'Manage' %}</a>

View file

@ -6,7 +6,11 @@ from photologue.models import Gallery
class UploadForm(forms.Form):
file_field = forms.FileField(
label="",
widget=forms.ClearableFileInput(attrs={'multiple': True}),
widget=forms.FileInput(attrs={
'accept': 'image/*',
'multiple': True,
'class': 'mb-3',
}),
)
gallery = forms.ModelChoiceField(
Gallery.objects.all(),
@ -20,3 +24,23 @@ class UploadForm(forms.Form):
max_length=250,
required=False,
)
def clean_new_gallery_title(self):
title = self.cleaned_data['new_gallery_title']
if title and Gallery.objects.filter(title=title).exists():
raise forms.ValidationError(_('A gallery with that title already exists.'))
return title
def clean(self):
cleaned_data = super().clean()
# Check that either an existing gallery is chosen, or new_gallery_title is filled
if not (bool(cleaned_data['gallery']) ^ bool(cleaned_data.get('new_gallery_title', None))):
raise forms.ValidationError(
_('Select an existing gallery, or enter a title for a new gallery.'))
return cleaned_data
def save(self, files):
# TODO: upload
print(type(files))

View file

@ -54,7 +54,7 @@ dropZone.ondragleave = function() {
<h1>{% trans "Upload" %}</h1>
<div class="card">
<div class="card-body">
<form method="post">{% csrf_token %}
<form method="post" enctype="multipart/form-data">{% csrf_token %}
<div class="upload-drop-zone" id="drop-zone">
{% trans "Drag and drop photos here" %}
</div>

View file

@ -6,10 +6,12 @@ import zipfile
from io import BytesIO
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib import messages
from django.core.mail import mail_managers
from django.http import HttpResponse
from django.views.generic.detail import DetailView
from django.views.generic.edit import FormView
from django.urls import reverse_lazy
from photologue.models import Gallery
from photologue.views import GalleryArchiveIndexView, GalleryYearArchiveView
from taggit.models import Tag
@ -96,27 +98,26 @@ class GalleryDownload(LoginRequiredMixin, DetailView):
class GalleryUpload(FormView):
"""
Form to upload new photos in a gallery
"""
form_class = UploadForm
template_name = "photologue/upload.html"
# success_url = '...' # Replace with your URL or reverse().
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
files = request.FILES.getlist('file_field')
if form.is_valid():
for f in files:
print("upload", f)
return self.form_valid(form)
else:
return self.form_invalid(form)
success_url = reverse_lazy("gallery-upload")
def form_valid(self, form):
"""
Notify moderators about successful upload
"""
# Upload photos
# We take files from the request to support multiple upload
files = self.request.FILES.getlist('file_field')
form.save(files)
# Notify user then managers
messages.success(self.request, "Photos has been successfully uploaded.")
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 upload",
message="", # TODO: put username, gallery and photo names
subject="New photos upload",
message=f"{self.request.user.username} has uploaded in `{gallery_title}`: {photos}",
)
return super().form_valid(form)