mirror of
https://github.com/StefBuwalda/WebTech.git
synced 2025-10-30 11:19:58 +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 import db
|
||||||
from application.auth.models import User
|
from application.auth.models import User
|
||||||
from application.auth.forms import LoginForm
|
from application.auth.forms import LoginForm
|
||||||
from flask_login import ( # type: ignore
|
from flask_login import ( # type: ignore
|
||||||
login_required, # type: ignore
|
|
||||||
login_user, # type: ignore
|
login_user, # type: ignore
|
||||||
logout_user,
|
logout_user,
|
||||||
current_user,
|
current_user,
|
||||||
)
|
)
|
||||||
from werkzeug.security import check_password_hash, generate_password_hash
|
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
|
from application.auth.forms import RegisterForm, UpdateForm
|
||||||
|
|
||||||
auth_blueprint = Blueprint("auth", __name__, template_folder="templates")
|
auth_blueprint = Blueprint("auth", __name__, template_folder="templates")
|
||||||
@@ -117,4 +116,5 @@ def login():
|
|||||||
@login_required
|
@login_required
|
||||||
def logout():
|
def logout():
|
||||||
logout_user()
|
logout_user()
|
||||||
|
flash("Logged out succesfully")
|
||||||
return redirect(url_for("index"))
|
return redirect(url_for("index"))
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
from application import db
|
from application import db
|
||||||
from flask import Blueprint, render_template, redirect, url_for
|
from flask import Blueprint, render_template, redirect, url_for
|
||||||
from application.dash.forms import ServiceForm
|
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.dash.models import Service
|
||||||
from application.utils import saveImage
|
from application.utils import saveImage
|
||||||
|
from application.decorators import login_required
|
||||||
|
|
||||||
# Dashboard blueprint
|
# Dashboard blueprint
|
||||||
dash_blueprint = Blueprint("dash", __name__, template_folder="templates")
|
dash_blueprint = Blueprint("dash", __name__, template_folder="templates")
|
||||||
|
|||||||
@@ -11,9 +11,19 @@ def admin_required(f: Callable[..., Any]) -> Callable[..., Any]:
|
|||||||
@wraps(f)
|
@wraps(f)
|
||||||
def decorated_function(*args: ..., **kwargs: ...):
|
def decorated_function(*args: ..., **kwargs: ...):
|
||||||
if not current_user.is_authenticated:
|
if not current_user.is_authenticated:
|
||||||
return redirect(url_for("login"))
|
return redirect(url_for("auth.login"))
|
||||||
if not current_user.is_admin:
|
if not current_user.is_admin:
|
||||||
return redirect(url_for("index"))
|
return redirect(url_for("index"))
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
|
|
||||||
return decorated_function
|
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>
|
</ul>
|
||||||
{% if current_user.is_authenticated %}
|
{% if current_user.is_authenticated %}
|
||||||
<div class="dropstart">
|
<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
|
Profile
|
||||||
</button>
|
</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">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><a class="dropdown-item {% if active_page == 'update' %}active{% endif %}"
|
||||||
<li><hr class="dropdown-divider"></li>
|
href="{{url_for('auth.update')}}">Change password</a></li>
|
||||||
<li><a class="dropdown-item" style="color: tomato;" data-bs-theme="dark" href="{{url_for('auth.logout')}}">Logout</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>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
</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 %}
|
{% block content %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
Reference in New Issue
Block a user