From 587fb649a65d2b6b1a99204e397223c4b3fa6976 Mon Sep 17 00:00:00 2001 From: Stef Date: Thu, 17 Apr 2025 15:15:05 +0200 Subject: [PATCH 1/2] 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 %} From d83b59953b6cb5dfbceedb18a14ca5ad3924a2cf Mon Sep 17 00:00:00 2001 From: Stef Date: Thu, 17 Apr 2025 15:38:20 +0200 Subject: [PATCH 2/2] Better flash msgs --- application/auth/views.py | 2 ++ application/dash/views.py | 15 +++++++-------- application/templates/base_template.html | 11 ++++++++++- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/application/auth/views.py b/application/auth/views.py index 9096bbf..05c0a8e 100644 --- a/application/auth/views.py +++ b/application/auth/views.py @@ -85,6 +85,7 @@ def update(): ) db.session.commit() logout_user() + flash("Password changed succesfully, please log back in") return redirect(url_for("auth.login")) return render_template("update_user.html", form=form, active_page="update") @@ -104,6 +105,7 @@ def login(): user.password, password # type: ignore ): login_user(user) # type: ignore + flash("Logged in succesfully") return redirect("/") else: feedback = "Username or password is incorrect" diff --git a/application/dash/views.py b/application/dash/views.py index a39c8d9..7fb36b2 100644 --- a/application/dash/views.py +++ b/application/dash/views.py @@ -1,5 +1,5 @@ from application import db -from flask import Blueprint, render_template, redirect, url_for +from flask import Blueprint, render_template, redirect, url_for, flash from application.dash.forms import ServiceForm from flask_login import current_user # type: ignore from application.dash.models import Service @@ -28,10 +28,12 @@ def delete_service(service_id: int): # Check ownership if service.user_id != current_user.id: + flash("This is not your service!") return redirect(url_for("dash.index")) db.session.delete(service) db.session.commit() + flash("Service deleted") return redirect(url_for("dash.index")) @@ -57,12 +59,8 @@ def add_service(): ) # type: ignore db.session.add(new_service) db.session.commit() - return render_template( - "add_service.html", - form=ServiceForm(formdata=None), - feedback="Service succesfully added", - active_page="service", - ) + flash("Service added") + return redirect(url_for("dash.index")) return render_template( "add_service.html", form=service_form, active_page="service" ) @@ -94,7 +92,8 @@ def edit_service(service_id: int): commit = True if commit: db.session.commit() - return redirect(url_for("dash.index")) + flash("Service edited") + return redirect(url_for("dash.index")) # Fill in correct data form = ServiceForm(name=service.name, url=service.url) return render_template("edit_service.html", form=form) diff --git a/application/templates/base_template.html b/application/templates/base_template.html index 0962c6b..f88d4c6 100644 --- a/application/templates/base_template.html +++ b/application/templates/base_template.html @@ -59,10 +59,19 @@ + + + + +
{% for message in get_flashed_messages() %} -