Ajout d'une vue de toutes les partitions

This commit is contained in:
augustin64 2022-08-16 18:44:52 +02:00
parent b1052563f7
commit 870d71fc5b
4 changed files with 55 additions and 4 deletions

View File

@ -93,7 +93,7 @@ def create_album():
db = get_db() db = get_db()
error = None error = None
if not name: if not name or name.strip() == "":
error = "Un nom est requis." error = "Un nom est requis."
if error is None: if error is None:

View File

@ -17,6 +17,7 @@ from flask import (
from werkzeug.security import check_password_hash, generate_password_hash from werkzeug.security import check_password_hash, generate_password_hash
from .db import get_db from .db import get_db
from .utils import User
bp = Blueprint("auth", __name__, url_prefix="/auth") bp = Blueprint("auth", __name__, url_prefix="/auth")
@ -34,6 +35,23 @@ def login_required(view):
return wrapped_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 @bp.before_app_request
def load_logged_in_user(): def load_logged_in_user():
"""If a user id is stored in the session, load the user object from """If a user id is stored in the session, load the user object from

View File

@ -3,10 +3,11 @@
Partition module Partition module
""" """
import os import os
from flask import Blueprint, abort, send_file from flask import Blueprint, abort, send_file, render_template
from .db import get_db 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") bp = Blueprint("partition", __name__, url_prefix="/partition")
@ -26,3 +27,10 @@ def partition(uuid):
if partition is None: if partition is None:
abort(404) abort(404)
return send_file(os.path.join("partitions", f"{uuid}.pdf")) 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)

View File

@ -0,0 +1,25 @@
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}Liste des partitions{% endblock %}</h1>
{% endblock %}
{% block content %}
{% if partitions|length != 0 %}
<div id="partitions-grid">
{% for partition in partitions %}
<a href="/partition/{{ partition['uuid'] }}">
<div class="partition" id="partition-{{ partition['uuid'] }}">
<img class="partition-thumbnail" src="/static/thumbnails/{{ partition['uuid'] }}.jpg">
<div class="partition-description">
<div class="partition-name">{{ partition["name"] }}</div>
<div class="partition-author">{{ partition["author"] }}</div>
</div>
</div>
</a>
{% endfor %}
</div>
{% else %}
<div>Aucune partition disponible</div>
{% endif %}
{% endblock %}