From 45240c434734992404e05b3b83d305d87ea65d32 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Tue, 16 Aug 2022 00:00:20 +0200 Subject: [PATCH] =?UTF-8?q?Impl=C3=A9mentation=20de=20l'ajout=20de=20parti?= =?UTF-8?q?tions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- partitioncloud/__init__.py | 3 +- partitioncloud/albums.py | 95 ++++++++++++++++++- partitioncloud/partition.py | 27 ------ .../templates/albums/add-partition.html | 18 ++++ partitioncloud/templates/albums/album.html | 32 +++++-- partitioncloud/templates/albums/index.html | 9 +- partitioncloud/templates/base.html | 4 + 8 files changed, 146 insertions(+), 44 deletions(-) delete mode 100644 partitioncloud/partition.py create mode 100644 partitioncloud/templates/albums/add-partition.html diff --git a/.gitignore b/.gitignore index 72e6b16..cac294a 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,4 @@ # data instance/partitioncloud.sqlite partitioncloud/partitions -partitioncloud/thumbnails +partitioncloud/static/thumbnails diff --git a/partitioncloud/__init__.py b/partitioncloud/__init__.py index 736be2b..0d9b0ec 100644 --- a/partitioncloud/__init__.py +++ b/partitioncloud/__init__.py @@ -5,7 +5,7 @@ Main file import os from flask import Flask, render_template, request, send_file, g, redirect -from . import auth, albums, partition +from . import auth, albums app = Flask(__name__) @@ -18,7 +18,6 @@ app.config.from_mapping( app.register_blueprint(auth.bp) app.register_blueprint(albums.bp) -app.register_blueprint(partition.bp) @app.route("/") diff --git a/partitioncloud/albums.py b/partitioncloud/albums.py index 7cbd793..ea50aee 100644 --- a/partitioncloud/albums.py +++ b/partitioncloud/albums.py @@ -240,6 +240,8 @@ def delete_album(uuid): ) for partition in partitions.fetchall(): 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( """ @@ -255,4 +257,95 @@ def delete_album(uuid): ) db.commit() flash("Album supprimé.") - return redirect("/albums") \ No newline at end of file + return redirect("/albums") + + +@bp.route("//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}") \ No newline at end of file diff --git a/partitioncloud/partition.py b/partitioncloud/partition.py deleted file mode 100644 index e6843fb..0000000 --- a/partitioncloud/partition.py +++ /dev/null @@ -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("//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") \ No newline at end of file diff --git a/partitioncloud/templates/albums/add-partition.html b/partitioncloud/templates/albums/add-partition.html new file mode 100644 index 0000000..5ebf342 --- /dev/null +++ b/partitioncloud/templates/albums/add-partition.html @@ -0,0 +1,18 @@ +{% extends 'base.html' %} + +{% block header %} +

{% block title %}Ajouter une partition à {{ album["name"] }}{% endblock %}

+{% endblock %} + +{% block content %} +
+ Titre* + + Auteur + + Paroles + + + +
+{% endblock %} \ No newline at end of file diff --git a/partitioncloud/templates/albums/album.html b/partitioncloud/templates/albums/album.html index 622b9fa..c23e774 100644 --- a/partitioncloud/templates/albums/album.html +++ b/partitioncloud/templates/albums/album.html @@ -4,22 +4,34 @@

{% block title %}{{ album["name"] }}{% endblock %}

{% endblock %} -{% block content %} +{% block header_actions %} {% if not_participant %} {% endif %} -{% if partitions|length != 0 %} -{% for partition in partitions %} - -
- -
{{ partition["name"] }}
-
{{ partition["author"] }}
-
+{% if g.user %} +
+ -{% endfor %} +{% endif %} +{% endblock %} + +{% block content %} +{% if partitions|length != 0 %} +
+ {% for partition in partitions %} + + {% endfor %} +
{% else %}
Aucune partition disponible
{% endif %} diff --git a/partitioncloud/templates/albums/index.html b/partitioncloud/templates/albums/index.html index aab6c47..f500dc8 100644 --- a/partitioncloud/templates/albums/index.html +++ b/partitioncloud/templates/albums/index.html @@ -4,6 +4,12 @@

{% block title %}Albums{% endblock %}

{% endblock %} +{% block header_actions %} + + + +{% endblock %} + {% block content %} {% if albums|length != 0 %} {% for album in albums %} @@ -16,7 +22,4 @@ {% else %}
Aucun album disponible
{% endif %} - - - {% endblock %} \ No newline at end of file diff --git a/partitioncloud/templates/base.html b/partitioncloud/templates/base.html index 9696748..2df065c 100644 --- a/partitioncloud/templates/base.html +++ b/partitioncloud/templates/base.html @@ -16,7 +16,11 @@
{% block header %}{% endblock %} +
+ {% block header_actions %}{% endblock %} +
+
{% for message in get_flashed_messages() %}
{{ message }}
{% endfor %}