From 587fb649a65d2b6b1a99204e397223c4b3fa6976 Mon Sep 17 00:00:00 2001 From: Stef Date: Thu, 17 Apr 2025 15:15:05 +0200 Subject: [PATCH] Adding flash functionality, had to make my own decorator --- application/auth/views.py | 6 +++--- application/dash/views.py | 3 ++- application/decorators.py | 12 ++++++++++- application/templates/base_template.html | 27 +++++++++++++++++++----- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/application/auth/views.py b/application/auth/views.py index 6c442e4..9096bbf 100644 --- a/application/auth/views.py +++ b/application/auth/views.py @@ -1,16 +1,15 @@ -from flask import Blueprint, render_template, redirect, url_for +from flask import Blueprint, render_template, redirect, url_for, flash from application import db from application.auth.models import User from application.auth.forms import LoginForm from flask_login import ( # type: ignore - login_required, # type: ignore login_user, # type: ignore logout_user, current_user, ) from werkzeug.security import check_password_hash, generate_password_hash -from application.decorators import admin_required +from application.decorators import admin_required, login_required from application.auth.forms import RegisterForm, UpdateForm auth_blueprint = Blueprint("auth", __name__, template_folder="templates") @@ -117,4 +116,5 @@ def login(): @login_required def logout(): logout_user() + flash("Logged out succesfully") return redirect(url_for("index")) diff --git a/application/dash/views.py b/application/dash/views.py index 5d3f4bc..a39c8d9 100644 --- a/application/dash/views.py +++ b/application/dash/views.py @@ -1,9 +1,10 @@ from application import db from flask import Blueprint, render_template, redirect, url_for from application.dash.forms import ServiceForm -from flask_login import login_required, current_user # type: ignore +from flask_login import current_user # type: ignore from application.dash.models import Service from application.utils import saveImage +from application.decorators import login_required # Dashboard blueprint dash_blueprint = Blueprint("dash", __name__, template_folder="templates") diff --git a/application/decorators.py b/application/decorators.py index a56be7c..cd03e02 100644 --- a/application/decorators.py +++ b/application/decorators.py @@ -11,9 +11,19 @@ def admin_required(f: Callable[..., Any]) -> Callable[..., Any]: @wraps(f) def decorated_function(*args: ..., **kwargs: ...): if not current_user.is_authenticated: - return redirect(url_for("login")) + return redirect(url_for("auth.login")) if not current_user.is_admin: return redirect(url_for("index")) return f(*args, **kwargs) return decorated_function + + +def login_required(f: Callable[..., Any]) -> Callable[..., Any]: + @wraps(f) + def decorated_function(*args: ..., **kwargs: ...): + if not current_user.is_authenticated: + return redirect(url_for("auth.login")) + return f(*args, **kwargs) + + return decorated_function diff --git a/application/templates/base_template.html b/application/templates/base_template.html index 7b273e2..0962c6b 100644 --- a/application/templates/base_template.html +++ b/application/templates/base_template.html @@ -38,20 +38,37 @@ {% if current_user.is_authenticated %}
- -
{% endif %} + + +
+ {% for message in get_flashed_messages() %} + + {% endfor %} +
+ {% block content %} {% endblock %}