Added a bunch of stuff

This commit is contained in:
2025-04-16 11:05:58 +02:00
parent e089805f31
commit e52d8097db
5 changed files with 105 additions and 18 deletions

View File

@@ -3,17 +3,27 @@ from wtforms import StringField, SubmitField, PasswordField, BooleanField
from wtforms.validators import DataRequired from wtforms.validators import DataRequired
class LoginForm(FlaskForm): class defaultForm(FlaskForm):
username = StringField("Username", validators=[DataRequired()])
password = PasswordField("Password")
submit = SubmitField("Login")
class RegisterForm(FlaskForm):
username = StringField("Username", validators=[DataRequired()]) username = StringField("Username", validators=[DataRequired()])
password = PasswordField("Password", validators=[DataRequired()]) password = PasswordField("Password", validators=[DataRequired()])
submit = SubmitField("Submit")
class LoginForm(defaultForm):
pass
class RegisterForm(defaultForm):
confirm_password = PasswordField( confirm_password = PasswordField(
"Confirm Password", validators=[DataRequired()] "Confirm Password", validators=[DataRequired()]
) )
is_admin = BooleanField("Admin") is_admin = BooleanField("Admin")
submit = SubmitField("Add")
class UpdateForm(defaultForm):
confirm_password = PasswordField(
"Confirm Password", validators=[DataRequired()]
)
current_password = PasswordField(
"Current Password", validators=[DataRequired()]
)

View File

@@ -0,0 +1,27 @@
{% extends 'base_template.html' %}
{% block title %}
Update
{% endblock %}
{% block content %}
<form class="bg-body-tertiary" method="POST">
{{ form.hidden_tag() }}
{% if feedback %}
<p class="feedback">{{feedback}}</p>
{% endif %}
<div>
Current password <br> {{ form.current_password() }}
</div>
<div>
New password <br> {{ form.password() }}
</div>
<div>
Confirm new password <br> {{ form.confirm_password() }}
</div>
<div class="submit">
{{ form.submit() }}
</div>
</form>
{% endblock %}

View File

@@ -3,10 +3,15 @@ from flask import Blueprint, render_template, redirect, url_for
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 login_required, login_user, logout_user # type: ignore 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 werkzeug.security import check_password_hash, generate_password_hash
from application.decorators import admin_required from application.decorators import admin_required
from application.auth.forms import RegisterForm from application.auth.forms import RegisterForm, UpdateForm
auth_blueprint = Blueprint("auth", __name__, template_folder="templates") auth_blueprint = Blueprint("auth", __name__, template_folder="templates")
@@ -27,12 +32,14 @@ def register():
"admin.html", "admin.html",
form=register_form, form=register_form,
feedback="Passwords don't match, please try again", feedback="Passwords don't match, please try again",
active_page="register",
) )
if User.query.filter_by(username=username).first(): if User.query.filter_by(username=username).first():
return render_template( return render_template(
"admin.html", "admin.html",
form=register_form, form=register_form,
feedback="Username is already taken", feedback="Username is already taken",
active_page="register",
) )
new_user = User( new_user = User(
username=username, # type: ignore username=username, # type: ignore
@@ -45,8 +52,41 @@ def register():
"admin.html", "admin.html",
form=RegisterForm(formdata=None), form=RegisterForm(formdata=None),
feedback="User succesfully added", feedback="User succesfully added",
active_page="register",
) )
return render_template("admin.html", form=register_form) return render_template(
"admin.html", form=register_form, active_page="register"
)
@auth_blueprint.route("/update_user", methods=["GET", "POST"])
@login_required
def update():
form = UpdateForm(username=current_user.username)
if form.validate_on_submit(): # type: ignore
if not check_password_hash(
current_user.password, form.current_password.data # type: ignore
):
return render_template(
"update_user.html",
form=form,
feedback="Current password incorrect",
active_page="update",
)
if form.password.data != form.confirm_password.data:
return render_template(
"update_user.html",
form=form,
feedback="New password mismatched",
active_page="update",
)
current_user.password = generate_password_hash(
form.password.data # type: ignore
)
db.session.commit()
logout_user()
return redirect(url_for("auth.login"))
return render_template("update_user.html", form=form, active_page="update")
@auth_blueprint.route("/login", methods=["GET", "POST"]) @auth_blueprint.route("/login", methods=["GET", "POST"])

View File

@@ -6,14 +6,14 @@ from application.dash.models import Service
dash_blueprint = Blueprint("dash", __name__, template_folder="templates") dash_blueprint = Blueprint("dash", __name__, template_folder="templates")
# Routes
@dash_blueprint.route("/", methods=["GET", "POST"]) @dash_blueprint.route("/", methods=["GET", "POST"])
@login_required @login_required
def index(): def index():
services = current_user.services # type: ignore services = current_user.services # type: ignore
return render_template("dashboard.html", services=services) return render_template(
"dashboard.html", services=services, active_page="dashboard"
)
@dash_blueprint.route("/delete_item/<int:service_id>", methods=["POST"]) @dash_blueprint.route("/delete_item/<int:service_id>", methods=["POST"])
@@ -49,5 +49,8 @@ def service():
"add_service.html", "add_service.html",
form=ServiceForm(formdata=None), form=ServiceForm(formdata=None),
feedback="Service succesfully added", feedback="Service succesfully added",
active_page="service",
) )
return render_template("add_service.html", form=service_form) return render_template(
"add_service.html", form=service_form, active_page="service"
)

View File

@@ -21,15 +21,22 @@
<ul class="navbar-nav me-auto mb-2 mb-lg-0"> <ul class="navbar-nav me-auto mb-2 mb-lg-0">
{% if current_user.is_authenticated %} {% if current_user.is_authenticated %}
<li class="nav-item"> <li class="nav-item">
<a class="nav-link active" aria-current="page" href="{{url_for('dash.index')}}">Dashboard</a> <a class="nav-link {% if active_page == 'dashboard' %}active{% endif %}" aria-current=" page"
href="{{url_for('dash.index')}}">Dashboard</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="{{url_for('dash.service')}}">Add service</a> <a class="nav-link {% if active_page == 'service' %}active{% endif %}"
href="{{url_for('dash.service')}}">Add service</a>
</li>
<li class="nav-item">
<a class="nav-link {% if active_page == 'update' %}active{% endif %}"
href="{{url_for('auth.update')}}">Update password</a>
</li> </li>
{% endif %} {% endif %}
{% if current_user.is_admin %} {% if current_user.is_admin %}
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="{{url_for('auth.register')}}">Add user</a> <a class="nav-link {% if active_page == 'register' %}active{% endif %}"
href="{{url_for('auth.register')}}">Add user</a>
</li> </li>
{% endif %} {% endif %}
</ul> </ul>