Cleanup gang

This commit is contained in:
2025-04-17 14:28:25 +02:00
parent 7755a150e4
commit aa90134fb4
5 changed files with 21 additions and 2 deletions

View File

@@ -3,16 +3,21 @@ from wtforms import StringField, SubmitField, PasswordField, BooleanField
from wtforms.validators import DataRequired
# Default Form that inherits from FlaskForm and
# contains a username, password and submit button
class defaultForm(FlaskForm):
username = StringField("Username", validators=[DataRequired()])
password = PasswordField("Password", validators=[DataRequired()])
submit = SubmitField("Submit")
# LoginForm, contains exactly the same as defaultForm
class LoginForm(defaultForm):
pass
# RegisterForm that inherits from the default.
# Adds a password confirmation and if the user is an admin or not.
class RegisterForm(defaultForm):
confirm_password = PasswordField(
"Confirm Password", validators=[DataRequired()]
@@ -20,6 +25,8 @@ class RegisterForm(defaultForm):
is_admin = BooleanField("Admin")
# Form to update password information.
# Needs a confirmation password and the current password
class UpdateForm(defaultForm):
confirm_password = PasswordField(
"Confirm Password", validators=[DataRequired()]

View File

@@ -2,14 +2,18 @@ from application import db
from flask_login import UserMixin # type: ignore
# User model
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(150), unique=True, nullable=False)
password = db.Column(db.String(150), nullable=False)
is_admin = db.Column(db.Boolean, default=False)
# Purely a relationship not a column,
# makes all the services accessible through User.services
services = db.relationship("Service", backref="user", lazy="joined")
# Initialize user, prevents red stuff
def __init__(self, username: str, password: str, is_admin: bool = False):
self.username = username
self.password = password

View File

@@ -16,8 +16,8 @@ from application.auth.forms import RegisterForm, UpdateForm
auth_blueprint = Blueprint("auth", __name__, template_folder="templates")
# Routes
@auth_blueprint.route("/register", methods=["GET", "POST"])
# Add user
@auth_blueprint.route("/register_user", methods=["GET", "POST"])
@admin_required
def register():
register_form = RegisterForm()
@@ -59,6 +59,7 @@ def register():
)
# Update user (specifically password)
@auth_blueprint.route("/update_user", methods=["GET", "POST"])
@login_required
def update():
@@ -89,6 +90,7 @@ def update():
return render_template("update_user.html", form=form, active_page="update")
# Login as user or admin
@auth_blueprint.route("/login", methods=["GET", "POST"])
def login():
login_form = LoginForm()
@@ -110,6 +112,7 @@ def login():
return render_template("login.html", form=login_form, feedback=feedback)
# Logout
@auth_blueprint.route("/logout")
@login_required
def logout():