Add edit and delete partition

This commit is contained in:
augustin64 2022-12-18 22:40:07 +01:00
parent 6131235918
commit a0b7f4f0c3
6 changed files with 174 additions and 4 deletions

View File

@ -3,11 +3,11 @@
Partition module
"""
import os
from flask import Blueprint, abort, send_file, render_template, request, redirect
from flask import Blueprint, abort, send_file, render_template, request, redirect, flash, session
from .db import get_db
from .auth import login_required, admin_required
from .utils import get_all_partitions
from .utils import get_all_partitions, User, Partition
bp = Blueprint("partition", __name__, url_prefix="/partition")
@ -31,6 +31,68 @@ def partition(uuid):
download_name = f"{partition['name']}.pdf"
)
@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")
@bp.route("/search/<uuid>")
@login_required

View File

@ -232,6 +232,69 @@ class Album():
db.commit()
class Partition():
def __init__(self, uuid=None):
db = get_db()
if uuid is not None:
self.uuid = uuid
data = db.execute(
"""
SELECT * FROM partition
WHERE uuid = ?
""",
(self.uuid,)
).fetchone()
if data is None:
raise LookupError
self.name = data["name"]
self.author = data["author"]
self.body = data["body"]
self.user_id = data["user_id"]
else:
raise LookupError
def delete(self):
db = get_db()
db.execute(
"""
DELETE FROM contient_partition
WHERE partition_uuid = ?
""",
(self.uuid,)
)
db.commit()
os.remove(f"partitioncloud/partitions/{self.uuid}.pdf")
if os.path.exists(f"partitioncloud/static/thumbnails/{self.uuid}.jpg"):
os.remove(f"partitioncloud/static/thumbnails/{self.uuid}.jpg")
partitions = db.execute(
"""
DELETE FROM partition
WHERE uuid = ?
""",
(self.uuid,)
)
db.commit()
def update(self, name=None, author="", body=""):
if name is None:
return Exception("name cannot be None")
db = get_db()
db.execute(
"""
UPDATE partition
SET name = ?,
author = ?,
body = ?
WHERE uuid = ?
""",
(name, author, body, self.uuid)
)
db.commit()
def get_all_partitions():
db = get_db()
partitions = db.execute(

View File

@ -325,4 +325,9 @@ td {
border-width: 1px;
box-shadow: 1px 2px 2px #7f7f7f;
}
}
#paroles {
font-family: inherit;
font-size: 0.8rem;
}

View File

@ -11,7 +11,7 @@
Auteur
<input name="author" type="text" />
Paroles
<textarea name="body" type="text"></textarea>
<textarea id="paroles" name="body" type="text"></textarea>
{% if partition_uuid %}
<input name="partition-uuid" value="{{ partition_uuid }}" type="hidden">
{% else %}

View File

@ -0,0 +1,16 @@
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}Supprimer {{ partition.name }}{% endblock %}</h1>
{% endblock %}
{% block content %}
Êtes vous sûr de vouloir supprimer cette partition ?
<form method="post">
<input type="submit" value="Supprimer">
</form>
<a class="button-href" href="/albums/{{ partition.uuid }}/edit">
<button id="cancel-deletion">Annuler</button>
</a>
{% endblock %}

View File

@ -0,0 +1,24 @@
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}Edit - {{ partition.name }}{% endblock %}</h1>
{% endblock %}
{% block content %}
<br/>
<a href="/partition/{{ partition.uuid }}"><div>voir le fichier ↗</div></a>
<form action="/partition/{{ partition.uuid }}/edit" method="post" enctype="multipart/form-data">
Titre*
<input name="name" type="text" value="{{ partition.name }}" required />
Auteur
<input name="author" type="text" value="{{ partition.author }}" />
Paroles
<textarea id="paroles" name="body" type="text">{{ partition.body }}</textarea>
<input type="submit" value="Mettre à jour" />
</form>
<a href="/partition/{{ partition.uuid }}/delete">
<button id="supprimer-partition" style="width: 132px;">Supprimer</button>
</a>
{% endblock %}