diff --git a/application/auth/forms.py b/application/auth/forms.py index 287a369..90c31fb 100644 --- a/application/auth/forms.py +++ b/application/auth/forms.py @@ -3,17 +3,27 @@ from wtforms import StringField, SubmitField, PasswordField, BooleanField from wtforms.validators import DataRequired -class LoginForm(FlaskForm): - username = StringField("Username", validators=[DataRequired()]) - password = PasswordField("Password") - submit = SubmitField("Login") - - -class RegisterForm(FlaskForm): +class defaultForm(FlaskForm): username = StringField("Username", validators=[DataRequired()]) password = PasswordField("Password", validators=[DataRequired()]) + submit = SubmitField("Submit") + + +class LoginForm(defaultForm): + pass + + +class RegisterForm(defaultForm): confirm_password = PasswordField( "Confirm Password", validators=[DataRequired()] ) is_admin = BooleanField("Admin") - submit = SubmitField("Add") + + +class UpdateForm(defaultForm): + confirm_password = PasswordField( + "Confirm Password", validators=[DataRequired()] + ) + current_password = PasswordField( + "Current Password", validators=[DataRequired()] + ) diff --git a/application/auth/templates/update_user.html b/application/auth/templates/update_user.html new file mode 100644 index 0000000..be558f9 --- /dev/null +++ b/application/auth/templates/update_user.html @@ -0,0 +1,27 @@ +{% extends 'base_template.html' %} + +{% block title %} +Update +{% endblock %} + +{% block content %} +
+ {{ form.hidden_tag() }} + {% if feedback %} +

{{feedback}}

+ {% endif %} +
+ Current password
{{ form.current_password() }} +
+
+ New password
{{ form.password() }} +
+
+ Confirm new password
{{ form.confirm_password() }} +
+ +
+ {{ form.submit() }} +
+
+{% endblock %} \ No newline at end of file diff --git a/application/auth/views.py b/application/auth/views.py index 8ac7f84..7652eea 100644 --- a/application/auth/views.py +++ b/application/auth/views.py @@ -3,10 +3,15 @@ from flask import Blueprint, render_template, redirect, url_for 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 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.auth.forms import RegisterForm +from application.auth.forms import RegisterForm, UpdateForm auth_blueprint = Blueprint("auth", __name__, template_folder="templates") @@ -27,12 +32,14 @@ def register(): "admin.html", form=register_form, feedback="Passwords don't match, please try again", + active_page="register", ) if User.query.filter_by(username=username).first(): return render_template( "admin.html", form=register_form, feedback="Username is already taken", + active_page="register", ) new_user = User( username=username, # type: ignore @@ -45,8 +52,41 @@ def register(): "admin.html", form=RegisterForm(formdata=None), 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"]) diff --git a/application/dash/views.py b/application/dash/views.py index be2d929..48f3f6f 100644 --- a/application/dash/views.py +++ b/application/dash/views.py @@ -6,14 +6,14 @@ from application.dash.models import Service dash_blueprint = Blueprint("dash", __name__, template_folder="templates") -# Routes - @dash_blueprint.route("/", methods=["GET", "POST"]) @login_required def index(): 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/", methods=["POST"]) @@ -49,5 +49,8 @@ def service(): "add_service.html", form=ServiceForm(formdata=None), 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" + ) diff --git a/application/templates/base_template.html b/application/templates/base_template.html index e540697..58996f9 100644 --- a/application/templates/base_template.html +++ b/application/templates/base_template.html @@ -21,15 +21,22 @@