mirror of
https://github.com/StefBuwalda/WebTech.git
synced 2025-10-29 19:00:00 +00:00
Adding flash functionality, had to make my own decorator
This commit is contained in:
@@ -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"))
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -38,20 +38,37 @@
|
||||
</ul>
|
||||
{% if current_user.is_authenticated %}
|
||||
<div class="dropstart">
|
||||
<button class="btn btn-outline-info" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
|
||||
<button class="btn btn-outline-info" type="button" id="dropdownMenuButton1"
|
||||
data-bs-toggle="dropdown" aria-expanded="false">
|
||||
Profile
|
||||
</button>
|
||||
<ul class="dropdown-menu dropdown-menu-end dropdown-menu-lg-start" aria-labelledby="dropdownMenuButton1">
|
||||
<ul class="dropdown-menu dropdown-menu-end dropdown-menu-lg-start"
|
||||
aria-labelledby="dropdownMenuButton1">
|
||||
<li><a class="dropdown-item">Username: {{current_user.username}}</a></li>
|
||||
<li><a class="dropdown-item {% if active_page == 'update' %}active{% endif %}" href="{{url_for('auth.update')}}">Change password</a></li>
|
||||
<li><hr class="dropdown-divider"></li>
|
||||
<li><a class="dropdown-item" style="color: tomato;" data-bs-theme="dark" href="{{url_for('auth.logout')}}">Logout</a></li>
|
||||
<li><a class="dropdown-item {% if active_page == 'update' %}active{% endif %}"
|
||||
href="{{url_for('auth.update')}}">Change password</a></li>
|
||||
<li>
|
||||
<hr class="dropdown-divider">
|
||||
</li>
|
||||
<li><a class="dropdown-item" style="color: tomato;" data-bs-theme="dark"
|
||||
href="{{url_for('auth.logout')}}">Logout</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
|
||||
<div class="position-fixed bottom-0 end-0 p-3" style="z-index: 1050;">
|
||||
{% for message in get_flashed_messages() %}
|
||||
<div class="alert alert-warning alert-dismissible fade show" role="alert">
|
||||
{{message}}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
{% block content %}
|
||||
{% endblock %}
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user