mirror of
https://github.com/StefBuwalda/WebTech.git
synced 2025-10-30 03:10:00 +00:00
Added a bunch of stuff
This commit is contained in:
@@ -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()]
|
||||
)
|
||||
|
||||
27
application/auth/templates/update_user.html
Normal file
27
application/auth/templates/update_user.html
Normal 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 %}
|
||||
@@ -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"])
|
||||
|
||||
@@ -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/<int:service_id>", 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"
|
||||
)
|
||||
|
||||
@@ -21,15 +21,22 @@
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
{% if current_user.is_authenticated %}
|
||||
<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 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>
|
||||
{% endif %}
|
||||
{% if current_user.is_admin %}
|
||||
<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>
|
||||
{% endif %}
|
||||
</ul>
|
||||
|
||||
Reference in New Issue
Block a user