mirror of
https://github.com/partitioncloud/partitioncloud-server.git
synced 2025-02-02 13:49:40 +01:00
Add edit and delete partition
This commit is contained in:
parent
6131235918
commit
a0b7f4f0c3
@ -3,11 +3,11 @@
|
|||||||
Partition module
|
Partition module
|
||||||
"""
|
"""
|
||||||
import os
|
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 .db import get_db
|
||||||
from .auth import login_required, admin_required
|
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")
|
bp = Blueprint("partition", __name__, url_prefix="/partition")
|
||||||
@ -31,6 +31,68 @@ def partition(uuid):
|
|||||||
download_name = f"{partition['name']}.pdf"
|
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>")
|
@bp.route("/search/<uuid>")
|
||||||
@login_required
|
@login_required
|
||||||
|
@ -232,6 +232,69 @@ class Album():
|
|||||||
db.commit()
|
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():
|
def get_all_partitions():
|
||||||
db = get_db()
|
db = get_db()
|
||||||
partitions = db.execute(
|
partitions = db.execute(
|
||||||
|
@ -326,3 +326,8 @@ td {
|
|||||||
|
|
||||||
box-shadow: 1px 2px 2px #7f7f7f;
|
box-shadow: 1px 2px 2px #7f7f7f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#paroles {
|
||||||
|
font-family: inherit;
|
||||||
|
font-size: 0.8rem;
|
||||||
|
}
|
@ -11,7 +11,7 @@
|
|||||||
Auteur
|
Auteur
|
||||||
<input name="author" type="text" />
|
<input name="author" type="text" />
|
||||||
Paroles
|
Paroles
|
||||||
<textarea name="body" type="text"></textarea>
|
<textarea id="paroles" name="body" type="text"></textarea>
|
||||||
{% if partition_uuid %}
|
{% if partition_uuid %}
|
||||||
<input name="partition-uuid" value="{{ partition_uuid }}" type="hidden">
|
<input name="partition-uuid" value="{{ partition_uuid }}" type="hidden">
|
||||||
{% else %}
|
{% else %}
|
||||||
|
16
partitioncloud/templates/partition/delete.html
Normal file
16
partitioncloud/templates/partition/delete.html
Normal 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 %}
|
24
partitioncloud/templates/partition/edit.html
Normal file
24
partitioncloud/templates/partition/edit.html
Normal 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 %}
|
Loading…
Reference in New Issue
Block a user