2022-08-16 18:13:00 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
"""
|
|
|
|
Partition module
|
|
|
|
"""
|
|
|
|
import os
|
2022-12-18 22:40:07 +01:00
|
|
|
from flask import Blueprint, abort, send_file, render_template, request, redirect, flash, session
|
2022-08-16 18:13:00 +02:00
|
|
|
|
|
|
|
from .db import get_db
|
2022-08-16 18:44:52 +02:00
|
|
|
from .auth import login_required, admin_required
|
2022-12-18 22:40:07 +01:00
|
|
|
from .utils import get_all_partitions, User, Partition
|
2022-08-16 18:13:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
bp = Blueprint("partition", __name__, url_prefix="/partition")
|
|
|
|
|
|
|
|
@bp.route("/<uuid>")
|
|
|
|
@login_required
|
|
|
|
def partition(uuid):
|
|
|
|
db = get_db()
|
|
|
|
partition = db.execute(
|
|
|
|
"""
|
|
|
|
SELECT * FROM partition
|
|
|
|
WHERE uuid = ?
|
|
|
|
""",
|
|
|
|
(uuid,)
|
|
|
|
).fetchone()
|
|
|
|
|
|
|
|
if partition is None:
|
|
|
|
abort(404)
|
2022-10-03 20:45:10 +02:00
|
|
|
return send_file(
|
|
|
|
os.path.join("partitions", f"{uuid}.pdf"),
|
|
|
|
download_name = f"{partition['name']}.pdf"
|
|
|
|
)
|
2022-08-16 18:44:52 +02:00
|
|
|
|
2022-12-18 22:40:07 +01:00
|
|
|
@bp.route("/<uuid>/edit", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def edit(uuid):
|
|
|
|
db = get_db()
|
|
|
|
try:
|
|
|
|
partition = Partition(uuid=uuid)
|
|
|
|
except LookupError:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
user = User(session.get("user_id"))
|
|
|
|
if user.access_level != 1 and partition.user_id != user.id:
|
|
|
|
flash("Vous n'êtes pas autorisé à modifier cette partition.")
|
|
|
|
return redirect("/albums")
|
|
|
|
|
|
|
|
if request.method == "GET":
|
|
|
|
return render_template("partition/edit.html", partition=partition)
|
|
|
|
|
|
|
|
error = None
|
|
|
|
|
|
|
|
if "name" not in request.form or request.form["name"].strip() == "":
|
|
|
|
error = "Un titre est requis."
|
|
|
|
elif "author" not in request.form:
|
|
|
|
error = "Un nom d'auteur est requis (à minima nul)"
|
|
|
|
elif "body" not in request.form:
|
|
|
|
error = "Des paroles sont requises (à minima nulles)"
|
|
|
|
|
|
|
|
if error is not None:
|
|
|
|
flash(error)
|
|
|
|
return redirect(f"/partition/{ uuid }/edit")
|
|
|
|
|
|
|
|
partition.update(
|
|
|
|
name=request.form["name"],
|
|
|
|
author=request.form["author"],
|
|
|
|
body=request.form["body"]
|
|
|
|
)
|
|
|
|
|
|
|
|
flash(f"Partition {request.form['name']} modifiée avec succès.")
|
|
|
|
return redirect("/albums")
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/<uuid>/delete", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def delete(uuid):
|
|
|
|
try:
|
|
|
|
partition = Partition(uuid=uuid)
|
|
|
|
except LookupError:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
user = User(session.get("user_id"))
|
|
|
|
|
|
|
|
if user.access_level != 1 and partition.user_id != user.id:
|
|
|
|
flash("Vous n'êtes pas autorisé à supprimer cette partition.")
|
|
|
|
return redirect("/albums")
|
|
|
|
|
|
|
|
if request.method == "GET":
|
|
|
|
return render_template("partition/delete.html", partition=partition)
|
|
|
|
|
|
|
|
partition.delete()
|
|
|
|
|
|
|
|
flash("Partition supprimée.")
|
|
|
|
return redirect("/albums")
|
|
|
|
|
2022-08-16 18:44:52 +02:00
|
|
|
|
2022-08-17 09:44:31 +02:00
|
|
|
@bp.route("/search/<uuid>")
|
|
|
|
@login_required
|
|
|
|
def partition_search(uuid):
|
|
|
|
db = get_db()
|
|
|
|
partition = db.execute(
|
|
|
|
"""
|
2022-10-28 23:01:02 +02:00
|
|
|
SELECT uuid, url FROM search_results
|
2022-08-17 09:44:31 +02:00
|
|
|
WHERE uuid = ?
|
|
|
|
""",
|
|
|
|
(uuid,)
|
|
|
|
).fetchone()
|
|
|
|
|
|
|
|
if partition is None:
|
|
|
|
abort(404)
|
2022-10-28 23:01:02 +02:00
|
|
|
if request.args.get("redirect") == "true" and partition["url"] is not None:
|
|
|
|
return redirect(partition["url"])
|
2022-08-17 09:44:31 +02:00
|
|
|
return send_file(os.path.join("search-partitions", f"{uuid}.pdf"))
|
|
|
|
|
|
|
|
|
2022-08-16 18:44:52 +02:00
|
|
|
@bp.route("/")
|
|
|
|
@admin_required
|
|
|
|
def index():
|
|
|
|
partitions = get_all_partitions().fetchall()
|
2022-08-31 13:54:13 +02:00
|
|
|
return render_template("admin/partitions.html", partitions=partitions)
|