Generalization of drag & drop for attachments

This commit is contained in:
augustin64 2024-10-11 09:35:00 +02:00
parent 009be93b7a
commit ffbf1907ad
10 changed files with 160 additions and 106 deletions

View File

@ -75,6 +75,16 @@ class Partition():
)
db.commit()
def update_file(self, file, instance_path):
partition_path = os.path.join(
instance_path,
"partitions",
f"{self.uuid}.pdf"
)
file.save(partition_path)
if os.path.exists(f"{instance_path}/cache/thumbnails/{self.uuid}.jpg"):
os.remove(f"{instance_path}/cache/thumbnails/{self.uuid}.jpg")
def get_user(self):
db = get_db()
user = db.execute(

View File

@ -3,6 +3,7 @@
Partition module
"""
import os
import pypdf
from uuid import uuid4
from flask import (Blueprint, abort, send_file, render_template,
request, redirect, flash, session, current_app)
@ -160,6 +161,17 @@ def edit(uuid):
flash(error)
return redirect(f"/partition/{ uuid }/edit")
if request.files.get('file', None):
new_file = request.files["file"]
try:
pypdf.PdfReader(new_file)
new_file.seek(0)
except (pypdf.errors.PdfReadError, pypdf.errors.PdfStreamError):
flash(_("Invalid PDF file"))
return redirect(request.referrer)
partition.update_file(new_file, current_app.instance_path)
partition.update(
name=request.form["name"],
author=request.form["author"],

View File

@ -734,7 +734,7 @@ midi-player {
cursor: pointer;
}
.file-area .file-dummy {
.file-area .inner-file-area {
padding: 30px;
background: var(--color-mantle);
border: 2px dashed var(--color-red);
@ -742,20 +742,32 @@ midi-player {
transition: background 0.3s ease-in-out;
}
.file-area .file-dummy .success {
.file-area .inner-file-area .success {
display: none;
}
.file-area:hover > .file-dummy {
.file-area:hover > .inner-file-area {
background: var(--color-surface0);
}
.file-area input[type=file]:valid + .file-dummy {
.file-area input[type=file]:valid + .inner-file-area {
border-color: var(--color-green);
}
.file-area input[type=file]:valid + .file-dummy .success {
.file-area input[type=file]:not(:required) + .inner-file-area {
border-color: var(--color-blue);
}
.file-area input[type=file]:valid + .inner-file-area .success {
display: inline-block;
}
.file-area input[type=file]:valid + .file-dummy .default {
.file-area input[type=file]:valid + .inner-file-area .default {
display: none;
}
.file-area input[type=file]:not(:required) + .inner-file-area .success {
display: none;
}
.file-area input[type=file]:not(:required) + .inner-file-area .default {
display: inline-block;
}

View File

@ -7,7 +7,11 @@
{% if partition_uuid %}
<input name="partition-uuid" value="{{ partition_uuid }}" type="hidden">
{% else %}
{% include 'components/input_file.html' %}
{% block input_file %}
{% set required=true %}
{% set filetype=".pdf" %}
{% include 'components/input_file.html' %}
{% endblock %}
{% endif %}
<input type="submit" value="{{ _('Add') }}" />
</form>

View File

@ -1,7 +1,7 @@
<div class="file-area">
<input name="file" type="file" accept=".pdf" required="">
<div class="file-dummy">
<input name="file" type="file" accept="{{ filetype }}" {% if required %}required=""{% endif %}>
<div class="inner-file-area">
<div class="success">{{ _("Your file is selected.") }}</div>
<div class="default">{{ _("Select or drag & drop your file (pdf).") }}</div>
<div class="default">{{ _("Select or drag & drop your file") }} ({{ filetype }}).</div>
</div>
</div>

View File

@ -9,7 +9,11 @@
<h2>{{ _("Add an attachment to %(name)s", name=partition.name) }}</h2>
<form action="/partition/{{ partition.uuid }}/add-attachment" method="post" enctype="multipart/form-data">
<input type="text" name="name" id="name" placeholder="{{ _('Name') }}"><br/>
<input name="file" type="file" accept=".mp3,.mid" required=""><br/>
{% block input_file %}
{% set required=true %}
{% set filetype=".mp3,.mid" %}
{% include 'components/input_file.html' %}
{% endblock %}
<input type="submit" value="{{ _('Add') }}">
</form>
<a href="#!" class="close-dialog">Close</a>
@ -55,7 +59,7 @@
{% endif %}
<br/>
{% if user %}
{% if g.user %}
<div class="centered">
<a href="#create-attachment"><button>{{ _("Add an attachment") }}</button></a>
</div>

View File

@ -52,7 +52,13 @@
<td>{{ _("File") }}</td>
<td><a href="/partition/{{ partition.uuid }}">
<img class="partition-thumbnail" src="/thumbnails/{{ partition.uuid }}.jpg" loading="lazy">
</a></td>
</a><br/>
{% block input_file %}
{% set required=false %}
{% set filetype=".pdf" %}
{% include 'components/input_file.html' %}
{% endblock %}
</td>
</tr>
<tr>
<td>{{ _("Title") }}</td>

View File

@ -13,7 +13,13 @@
<td>{{ _("File") }}</td>
<td><a href="/partition/{{ partition.uuid }}">
<img class="partition-thumbnail" src="/thumbnails/{{ partition.uuid }}.jpg" loading="lazy">
</a></td>
</a><br/>
{% block input_file %}
{% set required=false %}
{% set filetype=".pdf" %}
{% include 'components/input_file.html' %}
{% endblock %}
</td>
</tr>
{% if partition.source != "unknown" and partition.source != "upload" %}
<tr>

View File

@ -75,12 +75,12 @@ msgstr "You don't own this album."
msgid "Album deleted."
msgstr "Album deleted."
#: partitioncloud/modules/albums.py:278 partitioncloud/modules/partition.py:153
#: partitioncloud/modules/partition.py:199
#: partitioncloud/modules/albums.py:278 partitioncloud/modules/partition.py:154
#: partitioncloud/modules/partition.py:211
msgid "Missing title"
msgstr "Missing title"
#: partitioncloud/modules/albums.py:280 partitioncloud/modules/partition.py:63
#: partitioncloud/modules/albums.py:280 partitioncloud/modules/partition.py:64
msgid "Missing file"
msgstr "Missing file"
@ -88,7 +88,7 @@ msgstr "Missing file"
msgid "Search results expired"
msgstr "Search results expired"
#: partitioncloud/modules/albums.py:302
#: partitioncloud/modules/albums.py:302 partitioncloud/modules/partition.py:170
msgid "Invalid PDF file"
msgstr ""
@ -183,43 +183,43 @@ msgstr "Group deleted."
msgid "You are not admin of this group."
msgstr "You are not admin of this group."
#: partitioncloud/modules/partition.py:58
#: partitioncloud/modules/partition.py:59
msgid "You don't own this score."
msgstr "You don't own this score."
#: partitioncloud/modules/partition.py:71
#: partitioncloud/modules/partition.py:72
msgid "Missing filename."
msgstr "Missing filename."
#: partitioncloud/modules/partition.py:76
#: partitioncloud/modules/partition.py:77
msgid "Unsupported file type."
msgstr "Unsupported file type."
#: partitioncloud/modules/partition.py:144
#: partitioncloud/modules/partition.py:145
msgid "You are not allowed to edit this file."
msgstr "You are not allowed to edit this file."
#: partitioncloud/modules/partition.py:155
#: partitioncloud/modules/partition.py:201
#: partitioncloud/modules/partition.py:156
#: partitioncloud/modules/partition.py:213
msgid "Missing author in request body (can be null)."
msgstr "Missing author in request body (can be null)."
#: partitioncloud/modules/partition.py:157
#: partitioncloud/modules/partition.py:203
#: partitioncloud/modules/partition.py:158
#: partitioncloud/modules/partition.py:215
msgid "Missing lyrics (can be null)."
msgstr "Missing lyrics (can be null)."
#: partitioncloud/modules/partition.py:169
#: partitioncloud/modules/partition.py:215
#: partitioncloud/modules/partition.py:181
#: partitioncloud/modules/partition.py:227
#, python-format
msgid "Successfully modified %(name)s"
msgstr "Successfully modified %(name)s"
#: partitioncloud/modules/partition.py:230
#: partitioncloud/modules/partition.py:242
msgid "You are not allowed to delete this score."
msgstr "You are not allowed to delete this score."
#: partitioncloud/modules/partition.py:238
#: partitioncloud/modules/partition.py:250
msgid "Score deleted."
msgstr "Score deleted."
@ -411,8 +411,8 @@ msgstr "Do you really want to delete this album?"
#: partitioncloud/templates/groupe/index.html:20
#: partitioncloud/templates/groupe/index.html:57
#: partitioncloud/templates/partition/delete.html:10
#: partitioncloud/templates/partition/details.html:86
#: partitioncloud/templates/partition/edit.html:57
#: partitioncloud/templates/partition/details.html:92
#: partitioncloud/templates/partition/edit.html:63
#: partitioncloud/templates/settings/index.html:19
msgid "Delete"
msgstr "Delete"
@ -526,9 +526,9 @@ msgstr "author"
msgid "lyrics"
msgstr "lyrics"
#: partitioncloud/templates/components/add_partition.html:12
#: partitioncloud/templates/components/add_partition.html:16
#: partitioncloud/templates/groupe/index.html:11
#: partitioncloud/templates/partition/attachments.html:13
#: partitioncloud/templates/partition/attachments.html:17
msgid "Add"
msgstr "Add"
@ -537,8 +537,8 @@ msgid "Your file is selected."
msgstr "Your file is selected."
#: partitioncloud/templates/components/input_file.html:5
msgid "Select or drag & drop your file (pdf)."
msgstr "Select or drag & drop your file (pdf)."
msgid "Select or drag & drop your file"
msgstr "Select or drag & drop your file"
#: partitioncloud/templates/groupe/index.html:8
#, python-format
@ -576,7 +576,7 @@ msgstr "Attachments of %(name)s"
msgid "Add an attachment to %(name)s"
msgstr "Add an attachment to %(name)s"
#: partitioncloud/templates/partition/attachments.html:22
#: partitioncloud/templates/partition/attachments.html:26
msgid ""
"No pdf viewer available in this browser.\n"
" You can use Firefox on Android."
@ -584,11 +584,11 @@ msgstr ""
"No pdf viewer available in this browser.\n"
" You can use Firefox on Android."
#: partitioncloud/templates/partition/attachments.html:46
#: partitioncloud/templates/partition/attachments.html:50
msgid "JavaScript is mandatory to read MIDI files"
msgstr "JavaScript is mandatory to read MIDI files"
#: partitioncloud/templates/partition/attachments.html:60
#: partitioncloud/templates/partition/attachments.html:64
msgid "Add an attachment"
msgstr "Add an attachment"
@ -618,45 +618,45 @@ msgstr "Type"
msgid "File"
msgstr "File"
#: partitioncloud/templates/partition/details.html:58
#: partitioncloud/templates/partition/details.html:59
#: partitioncloud/templates/partition/edit.html:29
#: partitioncloud/templates/partition/edit.html:30
#: partitioncloud/templates/partition/details.html:64
#: partitioncloud/templates/partition/details.html:65
#: partitioncloud/templates/partition/edit.html:35
#: partitioncloud/templates/partition/edit.html:36
msgid "Title"
msgstr "Title"
#: partitioncloud/templates/partition/details.html:62
#: partitioncloud/templates/partition/details.html:63
#: partitioncloud/templates/partition/edit.html:33
#: partitioncloud/templates/partition/edit.html:34
#: partitioncloud/templates/partition/details.html:68
#: partitioncloud/templates/partition/details.html:69
#: partitioncloud/templates/partition/edit.html:39
#: partitioncloud/templates/partition/edit.html:40
msgid "Author"
msgstr "Author"
#: partitioncloud/templates/partition/details.html:66
#: partitioncloud/templates/partition/details.html:67
#: partitioncloud/templates/partition/edit.html:37
#: partitioncloud/templates/partition/edit.html:38
#: partitioncloud/templates/partition/details.html:72
#: partitioncloud/templates/partition/details.html:73
#: partitioncloud/templates/partition/edit.html:43
#: partitioncloud/templates/partition/edit.html:44
msgid "Lyrics"
msgstr "Lyrics"
#: partitioncloud/templates/partition/details.html:70
#: partitioncloud/templates/partition/edit.html:41
#: partitioncloud/templates/partition/details.html:76
#: partitioncloud/templates/partition/edit.html:47
msgid "Attachments"
msgstr "Attachments"
#: partitioncloud/templates/partition/details.html:75
#: partitioncloud/templates/partition/edit.html:46
#: partitioncloud/templates/partition/details.html:81
#: partitioncloud/templates/partition/edit.html:52
#, python-format
msgid "Yes, %(number)s"
msgstr "Yes, %(number)s"
#: partitioncloud/templates/partition/details.html:77
#: partitioncloud/templates/partition/edit.html:48
#: partitioncloud/templates/partition/details.html:83
#: partitioncloud/templates/partition/edit.html:54
msgid "Add one"
msgstr "Add one"
#: partitioncloud/templates/partition/details.html:83
#: partitioncloud/templates/partition/edit.html:54
#: partitioncloud/templates/partition/details.html:89
#: partitioncloud/templates/partition/edit.html:60
msgid "Update"
msgstr "Update"
@ -665,7 +665,7 @@ msgstr "Update"
msgid "Modify \"%(name)s\""
msgstr "Modify \"%(name)s\""
#: partitioncloud/templates/partition/edit.html:21
#: partitioncloud/templates/partition/edit.html:27
msgid "Source"
msgstr "Source"

View File

@ -75,12 +75,12 @@ msgstr "Vous ne possédez pas cet album."
msgid "Album deleted."
msgstr "Album supprimé."
#: partitioncloud/modules/albums.py:278 partitioncloud/modules/partition.py:153
#: partitioncloud/modules/partition.py:199
#: partitioncloud/modules/albums.py:278 partitioncloud/modules/partition.py:154
#: partitioncloud/modules/partition.py:211
msgid "Missing title"
msgstr "Un titre est requis."
#: partitioncloud/modules/albums.py:280 partitioncloud/modules/partition.py:63
#: partitioncloud/modules/albums.py:280 partitioncloud/modules/partition.py:64
msgid "Missing file"
msgstr "Aucun fichier n'a été fourni."
@ -88,7 +88,7 @@ msgstr "Aucun fichier n'a été fourni."
msgid "Search results expired"
msgstr "Les résultats de la recherche ont expiré."
#: partitioncloud/modules/albums.py:302
#: partitioncloud/modules/albums.py:302 partitioncloud/modules/partition.py:170
msgid "Invalid PDF file"
msgstr "Fichier PDF invalide"
@ -185,43 +185,43 @@ msgstr "Groupe supprimé."
msgid "You are not admin of this group."
msgstr "Vous n'êtes pas administrateur de ce groupe"
#: partitioncloud/modules/partition.py:58
#: partitioncloud/modules/partition.py:59
msgid "You don't own this score."
msgstr "Cette partition ne vous appartient pas"
#: partitioncloud/modules/partition.py:71
#: partitioncloud/modules/partition.py:72
msgid "Missing filename."
msgstr "Pas de nom de fichier"
#: partitioncloud/modules/partition.py:76
#: partitioncloud/modules/partition.py:77
msgid "Unsupported file type."
msgstr "Extension de fichier non supportée"
#: partitioncloud/modules/partition.py:144
#: partitioncloud/modules/partition.py:145
msgid "You are not allowed to edit this file."
msgstr "Vous n'êtes pas autorisé à modifier cette partition."
#: partitioncloud/modules/partition.py:155
#: partitioncloud/modules/partition.py:201
#: partitioncloud/modules/partition.py:156
#: partitioncloud/modules/partition.py:213
msgid "Missing author in request body (can be null)."
msgstr "Un nom d'auteur est requis (à minima nul)"
#: partitioncloud/modules/partition.py:157
#: partitioncloud/modules/partition.py:203
#: partitioncloud/modules/partition.py:158
#: partitioncloud/modules/partition.py:215
msgid "Missing lyrics (can be null)."
msgstr "Des paroles sont requises (à minima nulles)"
#: partitioncloud/modules/partition.py:169
#: partitioncloud/modules/partition.py:215
#: partitioncloud/modules/partition.py:181
#: partitioncloud/modules/partition.py:227
#, python-format
msgid "Successfully modified %(name)s"
msgstr "Partition %(name)s modifiée avec succès."
#: partitioncloud/modules/partition.py:230
#: partitioncloud/modules/partition.py:242
msgid "You are not allowed to delete this score."
msgstr "Vous n'êtes pas autorisé à supprimer cette partition."
#: partitioncloud/modules/partition.py:238
#: partitioncloud/modules/partition.py:250
msgid "Score deleted."
msgstr "Partition supprimée."
@ -417,8 +417,8 @@ msgstr "Êtes vous sûr de vouloir supprimer cet album ?"
#: partitioncloud/templates/groupe/index.html:20
#: partitioncloud/templates/groupe/index.html:57
#: partitioncloud/templates/partition/delete.html:10
#: partitioncloud/templates/partition/details.html:86
#: partitioncloud/templates/partition/edit.html:57
#: partitioncloud/templates/partition/details.html:92
#: partitioncloud/templates/partition/edit.html:63
#: partitioncloud/templates/settings/index.html:19
msgid "Delete"
msgstr "Supprimer"
@ -532,9 +532,9 @@ msgstr "auteur"
msgid "lyrics"
msgstr "paroles"
#: partitioncloud/templates/components/add_partition.html:12
#: partitioncloud/templates/components/add_partition.html:16
#: partitioncloud/templates/groupe/index.html:11
#: partitioncloud/templates/partition/attachments.html:13
#: partitioncloud/templates/partition/attachments.html:17
msgid "Add"
msgstr "Ajouter"
@ -543,8 +543,8 @@ msgid "Your file is selected."
msgstr "Fichier sélectionné."
#: partitioncloud/templates/components/input_file.html:5
msgid "Select or drag & drop your file (pdf)."
msgstr "Sélectionner ou déposer un fichier (pdf)."
msgid "Select or drag & drop your file"
msgstr "Sélectionner ou déposer un fichier"
#: partitioncloud/templates/groupe/index.html:8
#, python-format
@ -585,7 +585,7 @@ msgstr "Attachments de %(name)s"
msgid "Add an attachment to %(name)s"
msgstr "Ajouter un attachment à %(name)s"
#: partitioncloud/templates/partition/attachments.html:22
#: partitioncloud/templates/partition/attachments.html:26
msgid ""
"No pdf viewer available in this browser.\n"
" You can use Firefox on Android."
@ -593,11 +593,11 @@ msgstr ""
"Impossible d'afficher le pdf dans ce navigateur.\n"
" Il est conseillé d'utiliser Firefox sur Android."
#: partitioncloud/templates/partition/attachments.html:46
#: partitioncloud/templates/partition/attachments.html:50
msgid "JavaScript is mandatory to read MIDI files"
msgstr "JavaScript est nécessaire pour lire les fichiers MIDI"
#: partitioncloud/templates/partition/attachments.html:60
#: partitioncloud/templates/partition/attachments.html:64
msgid "Add an attachment"
msgstr "Ajouter un attachment"
@ -627,45 +627,45 @@ msgstr "Type d'ajout"
msgid "File"
msgstr "Fichier"
#: partitioncloud/templates/partition/details.html:58
#: partitioncloud/templates/partition/details.html:59
#: partitioncloud/templates/partition/edit.html:29
#: partitioncloud/templates/partition/edit.html:30
#: partitioncloud/templates/partition/details.html:64
#: partitioncloud/templates/partition/details.html:65
#: partitioncloud/templates/partition/edit.html:35
#: partitioncloud/templates/partition/edit.html:36
msgid "Title"
msgstr "Titre"
#: partitioncloud/templates/partition/details.html:62
#: partitioncloud/templates/partition/details.html:63
#: partitioncloud/templates/partition/edit.html:33
#: partitioncloud/templates/partition/edit.html:34
#: partitioncloud/templates/partition/details.html:68
#: partitioncloud/templates/partition/details.html:69
#: partitioncloud/templates/partition/edit.html:39
#: partitioncloud/templates/partition/edit.html:40
msgid "Author"
msgstr "Auteur"
#: partitioncloud/templates/partition/details.html:66
#: partitioncloud/templates/partition/details.html:67
#: partitioncloud/templates/partition/edit.html:37
#: partitioncloud/templates/partition/edit.html:38
#: partitioncloud/templates/partition/details.html:72
#: partitioncloud/templates/partition/details.html:73
#: partitioncloud/templates/partition/edit.html:43
#: partitioncloud/templates/partition/edit.html:44
msgid "Lyrics"
msgstr "Paroles"
#: partitioncloud/templates/partition/details.html:70
#: partitioncloud/templates/partition/edit.html:41
#: partitioncloud/templates/partition/details.html:76
#: partitioncloud/templates/partition/edit.html:47
msgid "Attachments"
msgstr "Pièces jointes"
#: partitioncloud/templates/partition/details.html:75
#: partitioncloud/templates/partition/edit.html:46
#: partitioncloud/templates/partition/details.html:81
#: partitioncloud/templates/partition/edit.html:52
#, python-format
msgid "Yes, %(number)s"
msgstr "Oui, %(number)s"
#: partitioncloud/templates/partition/details.html:77
#: partitioncloud/templates/partition/edit.html:48
#: partitioncloud/templates/partition/details.html:83
#: partitioncloud/templates/partition/edit.html:54
msgid "Add one"
msgstr "En rajouter"
#: partitioncloud/templates/partition/details.html:83
#: partitioncloud/templates/partition/edit.html:54
#: partitioncloud/templates/partition/details.html:89
#: partitioncloud/templates/partition/edit.html:60
msgid "Update"
msgstr "Mettre à jour"
@ -674,7 +674,7 @@ msgstr "Mettre à jour"
msgid "Modify \"%(name)s\""
msgstr "Modifier \"%(name)s\""
#: partitioncloud/templates/partition/edit.html:21
#: partitioncloud/templates/partition/edit.html:27
msgid "Source"
msgstr "Source"