From b5d83e3c508e00f7357b64dc2ebfc8665da555b1 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Sat, 10 Jun 2023 16:49:07 +0200 Subject: [PATCH] Pass current user to every render_template --- partitioncloud/__init__.py | 4 +++- partitioncloud/modules/admin.py | 4 +++- partitioncloud/modules/albums.py | 24 +++++++++++++++--------- partitioncloud/modules/partition.py | 12 +++++++----- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/partitioncloud/__init__.py b/partitioncloud/__init__.py index fda5557..15148eb 100644 --- a/partitioncloud/__init__.py +++ b/partitioncloud/__init__.py @@ -41,6 +41,8 @@ def add_user(): """ Ajouter un utilisateur en tant qu'administrateur """ + current_user = User(user_id=session.get("user_id")) + if request.method == "POST": username = request.form["username"] password = request.form["password"] @@ -77,7 +79,7 @@ def add_user(): return redirect("/albums") flash(error) - return render_template("auth/register.html", albums=get_all_albums()) + return render_template("auth/register.html", albums=get_all_albums(), user=current_user) if __name__ == "__main__": diff --git a/partitioncloud/modules/admin.py b/partitioncloud/modules/admin.py index 950c65c..924f22e 100644 --- a/partitioncloud/modules/admin.py +++ b/partitioncloud/modules/admin.py @@ -15,6 +15,7 @@ bp = Blueprint("admin", __name__, url_prefix="/admin") @bp.route("/") @admin_required def index(): + current_user = User(user_id=session.get("user_id")) db = get_db() users_id = db.execute( """ @@ -29,5 +30,6 @@ def index(): return render_template( "admin/index.html", - users=users + users=users, + user=current_user ) \ No newline at end of file diff --git a/partitioncloud/modules/albums.py b/partitioncloud/modules/albums.py index 32e372e..cc07a5e 100644 --- a/partitioncloud/modules/albums.py +++ b/partitioncloud/modules/albums.py @@ -28,7 +28,7 @@ def index(): else: max_queries = current_app.config["MAX_ONLINE_QUERIES"] - return render_template("albums/index.html", albums=albums, MAX_QUERIES=max_queries) + return render_template("albums/index.html", user=user) @bp.route("/search", methods=["POST"]) @@ -54,12 +54,14 @@ def search_page(): else: google_results = [] + user.get_albums() + return render_template( "albums/search.html", partitions=partitions_local, google_results=google_results, query=query, - albums=user.get_albums() + user=user ) @bp.route("/") @@ -82,7 +84,8 @@ def album(uuid): "albums/album.html", album=album, partitions=partitions, - not_participant=not_participant + not_participant=not_participant, + user=user ) except LookupError: @@ -118,6 +121,8 @@ def partition(album_uuid, partition_uuid): @bp.route("/create-album", methods=["GET", "POST"]) @login_required def create_album(): + current_user = User(user_id=session.get("user_id")) + if request.method == "POST": name = request.form["name"] db = get_db() @@ -156,9 +161,9 @@ def create_album(): return redirect(f"/albums/{uuid}") flash(error) - return render_template("albums/create-album.html") + return render_template("albums/create-album.html", user=current_user) - return render_template("albums/create-album.html") + return render_template("albums/create-album.html", user=current_user) @bp.route("//join") @@ -199,13 +204,13 @@ def quit_album(uuid): def delete_album(uuid): db = get_db() album = Album(uuid=uuid) + user = User(user_id=session.get("user_id")) if request.method == "GET": - return render_template("albums/delete-album.html", album=album) + return render_template("albums/delete-album.html", album=album, user=user) error = None users = album.get_users() - user = User(user_id=session.get("user_id")) if len(users) > 1: error = "Vous n'ĂȘtes pas seul dans cet album." elif len(users) == 1 and users[0]["id"] != user.id: @@ -236,7 +241,7 @@ def add_partition(album_uuid): return redirect(f"/albums/{album.uuid}") if request.method == "GET": - return render_template("albums/add-partition.html", album=album) + return render_template("albums/add-partition.html", album=album, user=user) error = None @@ -362,7 +367,8 @@ def add_partition_from_search(): return render_template( "albums/add-partition.html", album=album, - partition_uuid=request.form["partition-uuid"] + partition_uuid=request.form["partition-uuid"], + user=user ) else: diff --git a/partitioncloud/modules/partition.py b/partitioncloud/modules/partition.py index 000abb9..711cfb2 100644 --- a/partitioncloud/modules/partition.py +++ b/partitioncloud/modules/partition.py @@ -46,7 +46,7 @@ def edit(uuid): return redirect("/albums") if request.method == "GET": - return render_template("partition/edit.html", partition=partition) + return render_template("partition/edit.html", partition=partition, user=user) error = None @@ -90,8 +90,9 @@ def details(uuid): return render_template( "partition/details.html", partition=partition, - user=partition_user, - albums=partition.get_albums() + partition_user=partition_user, + albums=partition.get_albums(), + user=user ) error = None @@ -132,7 +133,7 @@ def delete(uuid): return redirect("/albums") if request.method == "GET": - return render_template("partition/delete.html", partition=partition) + return render_template("partition/delete.html", partition=partition, user=user) partition.delete() @@ -163,4 +164,5 @@ def partition_search(uuid): @admin_required def index(): partitions = get_all_partitions() - return render_template("admin/partitions.html", partitions=partitions) \ No newline at end of file + user = User(user_id=session.get("user_id")) + return render_template("admin/partitions.html", partitions=partitions, user=user) \ No newline at end of file