Add upload page
This commit is contained in:
parent
583a1ffce8
commit
727387566d
5 changed files with 185 additions and 33 deletions
22
photologue_custom/forms.py
Normal file
22
photologue_custom/forms.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
from django import forms
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from photologue.models import Gallery
|
||||
|
||||
|
||||
class UploadForm(forms.Form):
|
||||
file_field = forms.FileField(
|
||||
label="",
|
||||
widget=forms.ClearableFileInput(attrs={'multiple': True}),
|
||||
)
|
||||
gallery = forms.ModelChoiceField(
|
||||
Gallery.objects.all(),
|
||||
label=_('Gallery'),
|
||||
required=False,
|
||||
help_text=_('Select a gallery to add these images to. Leave this empty to '
|
||||
'create a new gallery from the supplied title.')
|
||||
)
|
||||
new_gallery_title = forms.CharField(
|
||||
label=_('New gallery title'),
|
||||
max_length=250,
|
||||
required=False,
|
||||
)
|
||||
69
photologue_custom/templates/photologue/upload.html
Normal file
69
photologue_custom/templates/photologue/upload.html
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
{% extends "base.html" %}
|
||||
{% comment %}
|
||||
SPDX-License-Identifier: GPL-3.0-or-later
|
||||
{% endcomment %}
|
||||
{% load i18n crispy_forms_tags %}
|
||||
{% block title %}{% trans "Upload" %}{% endblock %}
|
||||
|
||||
{% block extracss %}
|
||||
<style>
|
||||
.upload-drop-zone {
|
||||
height: 10em;
|
||||
line-height: 10em;
|
||||
border-width: 2px;
|
||||
color: #a3a3a3;
|
||||
border-style: dashed;
|
||||
border-color: #a3a3a3;
|
||||
border-radius: 0.5em;
|
||||
text-align: center;
|
||||
margin-bottom: 0.5em;
|
||||
}
|
||||
.upload-drop-zone.drop {
|
||||
color: #222;
|
||||
border-color: #222;
|
||||
background-color: rgba(163, 163, 163, 0.274);
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
{% block extrajs %}
|
||||
<script>
|
||||
const dropZone = document.getElementById('drop-zone');
|
||||
const uploadInput = document.getElementById('id_file_field');
|
||||
|
||||
dropZone.ondrop = function(e) {
|
||||
e.preventDefault();
|
||||
this.className = 'upload-drop-zone';
|
||||
console.log(e.dataTransfer.files)
|
||||
uploadInput.files = e.dataTransfer.files;
|
||||
}
|
||||
|
||||
dropZone.ondragover = function() {
|
||||
this.className = 'upload-drop-zone drop';
|
||||
return false;
|
||||
}
|
||||
|
||||
dropZone.ondragleave = function() {
|
||||
this.className = 'upload-drop-zone';
|
||||
return false;
|
||||
}
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h1>{% trans "Upload" %}</h1>
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<form method="post">{% csrf_token %}
|
||||
<div class="upload-drop-zone" id="drop-zone">
|
||||
{% trans "Drag and drop photos here" %}
|
||||
</div>
|
||||
{{ form|crispy }}
|
||||
<p class="mt-3">
|
||||
{% trans "Owner will be" %} <code>{{ request.user.get_full_name }} ({{ request.user.username}})</code>.
|
||||
</p>
|
||||
<button type="submit" class="btn btn-success">{% trans "Upload" %}</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
from django.urls import path, re_path
|
||||
|
||||
from .views import (CustomGalleryArchiveIndexView,
|
||||
CustomGalleryYearArchiveView, CustomGalleryDetailView,
|
||||
GalleryDownload, TagDetail)
|
||||
from .views import (CustomGalleryArchiveIndexView, CustomGalleryDetailView,
|
||||
CustomGalleryYearArchiveView, GalleryDownload,
|
||||
GalleryUpload, TagDetail)
|
||||
|
||||
urlpatterns = [
|
||||
path('tag/<slug:slug>/', TagDetail.as_view(), name='tag-detail'),
|
||||
|
|
@ -11,4 +11,5 @@ urlpatterns = [
|
|||
path('gallery/<slug:slug>/', CustomGalleryDetailView.as_view(), name='pl-gallery'),
|
||||
path('gallery/<slug:slug>/<int:owner>/', CustomGalleryDetailView.as_view(), name='pl-gallery-owner'),
|
||||
path('gallery/<slug:slug>/download/', GalleryDownload.as_view(), name='gallery-download'),
|
||||
path('upload/', GalleryUpload.as_view(), name='gallery-upload'),
|
||||
]
|
||||
|
|
|
|||
|
|
@ -6,12 +6,16 @@ import zipfile
|
|||
from io import BytesIO
|
||||
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.core.mail import mail_managers
|
||||
from django.http import HttpResponse
|
||||
from django.views.generic import DetailView
|
||||
from django.views.generic.detail import DetailView
|
||||
from django.views.generic.edit import FormView
|
||||
from photologue.models import Gallery
|
||||
from photologue.views import GalleryArchiveIndexView, GalleryYearArchiveView
|
||||
from taggit.models import Tag
|
||||
|
||||
from .forms import UploadForm
|
||||
|
||||
|
||||
class TagDetail(LoginRequiredMixin, DetailView):
|
||||
model = Tag
|
||||
|
|
@ -89,3 +93,30 @@ class GalleryDownload(LoginRequiredMixin, DetailView):
|
|||
response = HttpResponse(byte_data.getvalue(), content_type='application/x-zip-compressed')
|
||||
response['Content-Disposition'] = f"attachment; filename={gallery.slug}.zip"
|
||||
return response
|
||||
|
||||
|
||||
class GalleryUpload(FormView):
|
||||
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)
|
||||
|
||||
def form_valid(self, form):
|
||||
"""
|
||||
Notify moderators about successful upload
|
||||
"""
|
||||
mail_managers(
|
||||
subject="New upload",
|
||||
message="", # TODO: put username, gallery and photo names
|
||||
)
|
||||
return super().form_valid(form)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue