Implémentation de l'ajout de partitions

This commit is contained in:
augustin64 2022-08-16 00:00:20 +02:00
parent 260cef29d4
commit 45240c4347
8 changed files with 146 additions and 44 deletions

2
.gitignore vendored
View File

@ -7,4 +7,4 @@
# data # data
instance/partitioncloud.sqlite instance/partitioncloud.sqlite
partitioncloud/partitions partitioncloud/partitions
partitioncloud/thumbnails partitioncloud/static/thumbnails

View File

@ -5,7 +5,7 @@ Main file
import os import os
from flask import Flask, render_template, request, send_file, g, redirect from flask import Flask, render_template, request, send_file, g, redirect
from . import auth, albums, partition from . import auth, albums
app = Flask(__name__) app = Flask(__name__)
@ -18,7 +18,6 @@ app.config.from_mapping(
app.register_blueprint(auth.bp) app.register_blueprint(auth.bp)
app.register_blueprint(albums.bp) app.register_blueprint(albums.bp)
app.register_blueprint(partition.bp)
@app.route("/") @app.route("/")

View File

@ -240,6 +240,8 @@ def delete_album(uuid):
) )
for partition in partitions.fetchall(): for partition in partitions.fetchall():
os.remove(f"partitioncloud/partitions/{partition['uuid']}.pdf") os.remove(f"partitioncloud/partitions/{partition['uuid']}.pdf")
if os.path.exists(f"partitioncloud/static/thumbnails/{partition['uuid']}.jpg"):
os.remove(f"partitioncloud/static/thumbnails/{partition['uuid']}.jpg")
partitions = db.execute( partitions = db.execute(
""" """
@ -256,3 +258,94 @@ def delete_album(uuid):
db.commit() db.commit()
flash("Album supprimé.") flash("Album supprimé.")
return redirect("/albums") return redirect("/albums")
@bp.route("/<album_uuid>/add-partition", methods=["GET", "POST"])
def add_partition(album_uuid):
user_id = session.get("user_id")
db = get_db()
if user_id is None:
flash("Vous n'êtes pas connecté.")
return redirect(f"/albums/{album_uuid}")
if (not user.is_participant(user_id, album_uuid)) and (user.access_level(user_id) != 1):
flash("Vous ne participez pas à cet album.")
return redirect(f"/albums/{album_uuid}")
if request.method == "GET":
album = db.execute(
"""
SELECT * FROM album
WHERE uuid = ?
""",
(album_uuid,)
).fetchone()
return render_template("albums/add-partition.html", album=album)
error = None
if "file" not in request.files:
error = "Aucun fichier n'a été fourni."
elif "name" not in request.form:
error = "Un titre est requis."
if error is not None:
flash(error)
return redirect(f"/albums/{album_uuid}")
if "author" in request.form:
author = request.form["author"]
else:
author = ""
if "body" in request.form:
body = request.form["body"]
else:
body = ""
while True:
try:
partition_uuid = str(uuid4())
db.execute(
"""
INSERT INTO partition (uuid, name, author, body)
VALUES (?, ?, ?, ?)
""",
(partition_uuid, request.form["name"], author, body),
)
db.commit()
file = request.files["file"]
file.save(f"partitioncloud/partitions/{partition_uuid}.pdf")
os.system(
f'/usr/bin/convert -thumbnail\
"178^>" -background white -alpha \
remove -crop 178x178+0+0 \
partitioncloud/partitions/{partition_uuid}.pdf[0] \
partitioncloud/static/thumbnails/{partition_uuid}.jpg'
)
album_id = db.execute(
"""
SELECT id FROM album
WHERE uuid = ?
""",
(album_uuid,)
).fetchone()["id"]
db.execute(
"""
INSERT INTO contient_partition (partition_uuid, album_id)
VALUES (?, ?)
""",
(partition_uuid, album_id),
)
db.commit()
break
except db.IntegrityError:
pass
flash(f"Partition {request.form['name']} ajoutée")
return redirect(f"/albums/{album_uuid}")

View File

@ -1,27 +0,0 @@
#!/usr/bin/python3
"""
Partitions module
"""
import os
from flask import (Blueprint, render_template,
send_file)
bp = Blueprint("partition", __name__, url_prefix="/partition")
@bp.route("/<uuid>/preview")
def preview(uuid):
"""
Renvoie la prévisualisation d'un fichier pdf
"""
if not os.path.exists(f"partitioncloud/thumbnails/{uuid}.jpg"):
os.system(
f'/usr/bin/convert -thumbnail\
"178^>" -background white -alpha \
remove -crop 178x178+0+0 \
partitioncloud/partitions/{uuid}.pdf[0] \
partitioncloud/thumbnails/{uuid}.jpg'
)
return send_file(f"thumbnails/{uuid}.jpg")

View File

@ -0,0 +1,18 @@
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}Ajouter une partition à {{ album["name"] }}{% endblock %}</h1>
{% endblock %}
{% block content %}
<form method="post" enctype="multipart/form-data">
Titre*
<input name="name" type="text" required/>
Auteur
<input name="author" type="text"/>
Paroles
<input name="body" type="text"/>
<input name="file" type="file" required/>
<input type="submit" value="Ajouter"/>
</form>
{% endblock %}

View File

@ -4,22 +4,34 @@
<h1>{% block title %}{{ album["name"] }}{% endblock %}</h1> <h1>{% block title %}{{ album["name"] }}{% endblock %}</h1>
{% endblock %} {% endblock %}
{% block content %} {% block header_actions %}
{% if not_participant %} {% if not_participant %}
<a href="/albums/{{ album['uuid'] }}/join"> <a href="/albums/{{ album['uuid'] }}/join">
<button id="join-album">Rejoindre</button> <button id="join-album">Rejoindre</button>
</a> </a>
{% endif %} {% endif %}
{% if g.user %}
<a href="/albums/{{ album['uuid'] }}/add-partition">
<button id="add-partition">Ajouter une partition</button>
</a>
{% endif %}
{% endblock %}
{% block content %}
{% if partitions|length != 0 %} {% if partitions|length != 0 %}
<div id="partitions-grid">
{% for partition in partitions %} {% for partition in partitions %}
<a href="{{ album['uuid'] }}/{{ partition['uuid'] }}">
<div class="partition" id="partition-{{ partition['uuid'] }}"> <div class="partition" id="partition-{{ partition['uuid'] }}">
<img src="/partition/{{ partition['uuid'] }}/preview"> <a href="{{ album['uuid'] }}/{{ partition['uuid'] }}">
<img src="/static/thumbnails/{{ partition['uuid'] }}.jpg">
<div class="partition-description">
<div class="partition-name">{{ partition["name"] }}</div> <div class="partition-name">{{ partition["name"] }}</div>
<div class="partition-author">{{ partition["author"] }}</div> <div class="partition-author">{{ partition["author"] }}</div>
</div> </div>
</a> </a>
</div>
{% endfor %} {% endfor %}
</div>
{% else %} {% else %}
<div>Aucune partition disponible</div> <div>Aucune partition disponible</div>
{% endif %} {% endif %}

View File

@ -4,6 +4,12 @@
<h1>{% block title %}Albums{% endblock %}</h1> <h1>{% block title %}Albums{% endblock %}</h1>
{% endblock %} {% endblock %}
{% block header_actions %}
<a href="create-album">
<button id="new-album">Nouvel Album</button>
</a>
{% endblock %}
{% block content %} {% block content %}
{% if albums|length != 0 %} {% if albums|length != 0 %}
{% for album in albums %} {% for album in albums %}
@ -16,7 +22,4 @@
{% else %} {% else %}
<div>Aucun album disponible</div> <div>Aucun album disponible</div>
{% endif %} {% endif %}
<a href="create-album">
<button id="new-album">Nouvel Album</button>
</a>
{% endblock %} {% endblock %}

View File

@ -16,7 +16,11 @@
<section class="content"> <section class="content">
<header> <header>
{% block header %}{% endblock %} {% block header %}{% endblock %}
<div id="header-actions">
{% block header_actions %}{% endblock %}
</div>
</header> </header>
<br/>
{% for message in get_flashed_messages() %} {% for message in get_flashed_messages() %}
<div class="flash">{{ message }}</div> <div class="flash">{{ message }}</div>
{% endfor %} {% endfor %}