Refractoring

This commit is contained in:
2025-04-16 09:57:05 +02:00
parent 5f083928ba
commit e089805f31
6 changed files with 56 additions and 57 deletions

View File

@@ -1,5 +1,5 @@
from flask_wtf import FlaskForm # type: ignore
from wtforms import StringField, SubmitField, PasswordField
from wtforms import StringField, SubmitField, PasswordField, BooleanField
from wtforms.validators import DataRequired
@@ -7,3 +7,13 @@ class LoginForm(FlaskForm):
username = StringField("Username", validators=[DataRequired()])
password = PasswordField("Password")
submit = SubmitField("Login")
class RegisterForm(FlaskForm):
username = StringField("Username", validators=[DataRequired()])
password = PasswordField("Password", validators=[DataRequired()])
confirm_password = PasswordField(
"Confirm Password", validators=[DataRequired()]
)
is_admin = BooleanField("Admin")
submit = SubmitField("Add")

View File

@@ -0,0 +1,29 @@
{% extends 'base_template.html' %}
{% block title %}
Register
{% endblock %}
{% block content %}
<form class="bg-body-tertiary" method="POST">
{{ form.hidden_tag() }}
{% if feedback %}
<p class="feedback">{{feedback}}</p>
{% endif %}
<div>
{{ form.username.label }} <br> {{ form.username() }}
</div>
<div>
{{ form.password.label }} <br> {{ form.password() }}
</div>
<div>
{{ form.confirm_password.label }} <br> {{ form.confirm_password() }}
</div>
<div>
{{ form.is_admin }} {{ form.is_admin.label }}
</div>
<div class="submit">
{{ form.submit() }}
</div>
</form>
{% endblock %}

View File

@@ -1,14 +1,52 @@
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.forms import LoginForm
from flask_login import login_required, login_user, logout_user # type: ignore
from werkzeug.security import check_password_hash
from werkzeug.security import check_password_hash, generate_password_hash
from application.decorators import admin_required
from application.auth.forms import RegisterForm
auth_blueprint = Blueprint("auth", __name__, template_folder="templates")
# Routes
@auth_blueprint.route("/register", methods=["GET", "POST"])
@admin_required
def register():
register_form = RegisterForm()
if register_form.validate_on_submit(): # type: ignore
username = register_form.username.data
password = register_form.password.data
confirm_password = register_form.confirm_password.data
is_admin = register_form.is_admin.data
if confirm_password != password:
return render_template(
"admin.html",
form=register_form,
feedback="Passwords don't match, please try again",
)
if User.query.filter_by(username=username).first():
return render_template(
"admin.html",
form=register_form,
feedback="Username is already taken",
)
new_user = User(
username=username, # type: ignore
password=generate_password_hash(password), # type: ignore
is_admin=is_admin,
)
db.session.add(new_user)
db.session.commit()
return render_template(
"admin.html",
form=RegisterForm(formdata=None),
feedback="User succesfully added",
)
return render_template("admin.html", form=register_form)
@auth_blueprint.route("/login", methods=["GET", "POST"])