From 870d71fc5b8ed85e7ed8c10a087dda70e66738e5 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Tue, 16 Aug 2022 18:44:52 +0200 Subject: [PATCH] Ajout d'une vue de toutes les partitions --- partitioncloud/albums.py | 2 +- partitioncloud/auth.py | 18 +++++++++++++ partitioncloud/partition.py | 14 ++++++++--- .../templates/partitions/view-all.html | 25 +++++++++++++++++++ 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 partitioncloud/templates/partitions/view-all.html diff --git a/partitioncloud/albums.py b/partitioncloud/albums.py index 5f9f53a..c8ac232 100644 --- a/partitioncloud/albums.py +++ b/partitioncloud/albums.py @@ -93,7 +93,7 @@ def create_album(): db = get_db() error = None - if not name: + if not name or name.strip() == "": error = "Un nom est requis." if error is None: diff --git a/partitioncloud/auth.py b/partitioncloud/auth.py index 1ffaf69..83fa1dd 100644 --- a/partitioncloud/auth.py +++ b/partitioncloud/auth.py @@ -17,6 +17,7 @@ from flask import ( from werkzeug.security import check_password_hash, generate_password_hash from .db import get_db +from .utils import User bp = Blueprint("auth", __name__, url_prefix="/auth") @@ -34,6 +35,23 @@ def login_required(view): return wrapped_view +def admin_required(view): + """View decorator that redirects anonymous users to the login page.""" + + @functools.wraps(view) + def wrapped_view(**kwargs): + if g.user is None: + return redirect(url_for("auth.login")) + + user = User(session.get("user_id")) + if user.access_level != 1: + return redirect("/albums") + + return view(**kwargs) + + return wrapped_view + + @bp.before_app_request def load_logged_in_user(): """If a user id is stored in the session, load the user object from diff --git a/partitioncloud/partition.py b/partitioncloud/partition.py index 08c17c4..1982b54 100644 --- a/partitioncloud/partition.py +++ b/partitioncloud/partition.py @@ -3,10 +3,11 @@ Partition module """ import os -from flask import Blueprint, abort, send_file +from flask import Blueprint, abort, send_file, render_template from .db import get_db -from .auth import login_required +from .auth import login_required, admin_required +from .utils import get_all_partitions bp = Blueprint("partition", __name__, url_prefix="/partition") @@ -25,4 +26,11 @@ def partition(uuid): if partition is None: abort(404) - return send_file(os.path.join("partitions", f"{uuid}.pdf")) \ No newline at end of file + return send_file(os.path.join("partitions", f"{uuid}.pdf")) + + +@bp.route("/") +@admin_required +def index(): + partitions = get_all_partitions().fetchall() + return render_template("partitions/view-all.html", partitions=partitions) \ No newline at end of file diff --git a/partitioncloud/templates/partitions/view-all.html b/partitioncloud/templates/partitions/view-all.html new file mode 100644 index 0000000..5014d6b --- /dev/null +++ b/partitioncloud/templates/partitions/view-all.html @@ -0,0 +1,25 @@ +{% extends 'base.html' %} + +{% block header %} +

{% block title %}Liste des partitions{% endblock %}

+{% endblock %} + +{% block content %} +{% if partitions|length != 0 %} +
+ {% for partition in partitions %} + +
+ +
+
{{ partition["name"] }}
+
{{ partition["author"] }}
+
+
+
+ {% endfor %} +
+{% else %} +
Aucune partition disponible
+{% endif %} +{% endblock %} \ No newline at end of file