Pass current user to every render_template

This commit is contained in:
augustin64 2023-06-10 16:49:07 +02:00
parent f73e5bbd42
commit b5d83e3c50
4 changed files with 28 additions and 16 deletions

View File

@ -41,6 +41,8 @@ def add_user():
""" """
Ajouter un utilisateur en tant qu'administrateur Ajouter un utilisateur en tant qu'administrateur
""" """
current_user = User(user_id=session.get("user_id"))
if request.method == "POST": if request.method == "POST":
username = request.form["username"] username = request.form["username"]
password = request.form["password"] password = request.form["password"]
@ -77,7 +79,7 @@ def add_user():
return redirect("/albums") return redirect("/albums")
flash(error) 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__": if __name__ == "__main__":

View File

@ -15,6 +15,7 @@ bp = Blueprint("admin", __name__, url_prefix="/admin")
@bp.route("/") @bp.route("/")
@admin_required @admin_required
def index(): def index():
current_user = User(user_id=session.get("user_id"))
db = get_db() db = get_db()
users_id = db.execute( users_id = db.execute(
""" """
@ -29,5 +30,6 @@ def index():
return render_template( return render_template(
"admin/index.html", "admin/index.html",
users=users users=users,
user=current_user
) )

View File

@ -28,7 +28,7 @@ def index():
else: else:
max_queries = current_app.config["MAX_ONLINE_QUERIES"] 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"]) @bp.route("/search", methods=["POST"])
@ -54,12 +54,14 @@ def search_page():
else: else:
google_results = [] google_results = []
user.get_albums()
return render_template( return render_template(
"albums/search.html", "albums/search.html",
partitions=partitions_local, partitions=partitions_local,
google_results=google_results, google_results=google_results,
query=query, query=query,
albums=user.get_albums() user=user
) )
@bp.route("/<uuid>") @bp.route("/<uuid>")
@ -82,7 +84,8 @@ def album(uuid):
"albums/album.html", "albums/album.html",
album=album, album=album,
partitions=partitions, partitions=partitions,
not_participant=not_participant not_participant=not_participant,
user=user
) )
except LookupError: except LookupError:
@ -118,6 +121,8 @@ def partition(album_uuid, partition_uuid):
@bp.route("/create-album", methods=["GET", "POST"]) @bp.route("/create-album", methods=["GET", "POST"])
@login_required @login_required
def create_album(): def create_album():
current_user = User(user_id=session.get("user_id"))
if request.method == "POST": if request.method == "POST":
name = request.form["name"] name = request.form["name"]
db = get_db() db = get_db()
@ -156,9 +161,9 @@ def create_album():
return redirect(f"/albums/{uuid}") return redirect(f"/albums/{uuid}")
flash(error) 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("/<uuid>/join") @bp.route("/<uuid>/join")
@ -199,13 +204,13 @@ def quit_album(uuid):
def delete_album(uuid): def delete_album(uuid):
db = get_db() db = get_db()
album = Album(uuid=uuid) album = Album(uuid=uuid)
user = User(user_id=session.get("user_id"))
if request.method == "GET": 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 error = None
users = album.get_users() users = album.get_users()
user = User(user_id=session.get("user_id"))
if len(users) > 1: if len(users) > 1:
error = "Vous n'êtes pas seul dans cet album." error = "Vous n'êtes pas seul dans cet album."
elif len(users) == 1 and users[0]["id"] != user.id: 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}") return redirect(f"/albums/{album.uuid}")
if request.method == "GET": 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 error = None
@ -362,7 +367,8 @@ def add_partition_from_search():
return render_template( return render_template(
"albums/add-partition.html", "albums/add-partition.html",
album=album, album=album,
partition_uuid=request.form["partition-uuid"] partition_uuid=request.form["partition-uuid"],
user=user
) )
else: else:

View File

@ -46,7 +46,7 @@ def edit(uuid):
return redirect("/albums") return redirect("/albums")
if request.method == "GET": if request.method == "GET":
return render_template("partition/edit.html", partition=partition) return render_template("partition/edit.html", partition=partition, user=user)
error = None error = None
@ -90,8 +90,9 @@ def details(uuid):
return render_template( return render_template(
"partition/details.html", "partition/details.html",
partition=partition, partition=partition,
user=partition_user, partition_user=partition_user,
albums=partition.get_albums() albums=partition.get_albums(),
user=user
) )
error = None error = None
@ -132,7 +133,7 @@ def delete(uuid):
return redirect("/albums") return redirect("/albums")
if request.method == "GET": if request.method == "GET":
return render_template("partition/delete.html", partition=partition) return render_template("partition/delete.html", partition=partition, user=user)
partition.delete() partition.delete()
@ -163,4 +164,5 @@ def partition_search(uuid):
@admin_required @admin_required
def index(): def index():
partitions = get_all_partitions() partitions = get_all_partitions()
return render_template("admin/partitions.html", partitions=partitions) user = User(user_id=session.get("user_id"))
return render_template("admin/partitions.html", partitions=partitions, user=user)