mirror of
https://github.com/StefBuwalda/WebTech.git
synced 2025-10-30 11:19:58 +00:00
Refractoring
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
from flask_wtf import FlaskForm # type: ignore
|
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
|
from wtforms.validators import DataRequired
|
||||||
|
|
||||||
|
|
||||||
@@ -7,3 +7,13 @@ class LoginForm(FlaskForm):
|
|||||||
username = StringField("Username", validators=[DataRequired()])
|
username = StringField("Username", validators=[DataRequired()])
|
||||||
password = PasswordField("Password")
|
password = PasswordField("Password")
|
||||||
submit = SubmitField("Login")
|
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")
|
||||||
|
|||||||
@@ -1,14 +1,52 @@
|
|||||||
from flask import Blueprint, render_template, redirect, url_for
|
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 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")
|
auth_blueprint = Blueprint("auth", __name__, template_folder="templates")
|
||||||
|
|
||||||
|
|
||||||
# Routes
|
# 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"])
|
@auth_blueprint.route("/login", methods=["GET", "POST"])
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
from flask_wtf import FlaskForm # type: ignore
|
from flask_wtf import FlaskForm # type: ignore
|
||||||
from wtforms import (
|
from wtforms import (
|
||||||
StringField,
|
StringField,
|
||||||
PasswordField,
|
|
||||||
SubmitField,
|
SubmitField,
|
||||||
URLField,
|
URLField,
|
||||||
BooleanField,
|
|
||||||
)
|
)
|
||||||
from wtforms.validators import DataRequired
|
from wtforms.validators import DataRequired
|
||||||
|
|
||||||
@@ -13,13 +11,3 @@ class ServiceForm(FlaskForm):
|
|||||||
name = StringField("Service name:", validators=[DataRequired()])
|
name = StringField("Service name:", validators=[DataRequired()])
|
||||||
url = URLField("Service URL:", validators=[DataRequired()])
|
url = URLField("Service URL:", validators=[DataRequired()])
|
||||||
submit = SubmitField("Add")
|
submit = SubmitField("Add")
|
||||||
|
|
||||||
|
|
||||||
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")
|
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
from application import db
|
from application import db
|
||||||
from flask import Blueprint, render_template, redirect, url_for
|
from flask import Blueprint, render_template, redirect, url_for
|
||||||
from application.dash.forms import RegisterForm, ServiceForm
|
from application.dash.forms import ServiceForm
|
||||||
from flask_login import login_required, current_user # type: ignore
|
from flask_login import login_required, current_user # type: ignore
|
||||||
from application.dash.models import Service
|
from application.dash.models import Service
|
||||||
from application.auth.models import User
|
|
||||||
from application.decorators import admin_required
|
|
||||||
from werkzeug.security import generate_password_hash
|
|
||||||
|
|
||||||
dash_blueprint = Blueprint("dash", __name__, template_folder="templates")
|
dash_blueprint = Blueprint("dash", __name__, template_folder="templates")
|
||||||
|
|
||||||
@@ -19,43 +16,6 @@ def index():
|
|||||||
return render_template("dashboard.html", services=services)
|
return render_template("dashboard.html", services=services)
|
||||||
|
|
||||||
|
|
||||||
@dash_blueprint.route("/admin", methods=["GET", "POST"])
|
|
||||||
@admin_required
|
|
||||||
def admin():
|
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
@dash_blueprint.route("/delete_item/<int:service_id>", methods=["POST"])
|
@dash_blueprint.route("/delete_item/<int:service_id>", methods=["POST"])
|
||||||
@login_required
|
@login_required
|
||||||
def delete_item(service_id: int):
|
def delete_item(service_id: int):
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
@@ -11,6 +12,7 @@
|
|||||||
crossorigin="anonymous"></script>
|
crossorigin="anonymous"></script>
|
||||||
<title>{% block title %}{% endblock %}</title>
|
<title>{% block title %}{% endblock %}</title>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<nav class="navbar sticky-top navbar-expand-lg bg-body-tertiary" data-bs-theme="dark">
|
<nav class="navbar sticky-top navbar-expand-lg bg-body-tertiary" data-bs-theme="dark">
|
||||||
<div class="container-fluid">
|
<div class="container-fluid">
|
||||||
@@ -27,7 +29,7 @@
|
|||||||
{% 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('dash.admin')}}">Add user</a>
|
<a class="nav-link" href="{{url_for('auth.register')}}">Add user</a>
|
||||||
</li>
|
</li>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</ul>
|
</ul>
|
||||||
@@ -40,4 +42,5 @@
|
|||||||
{% block content %}
|
{% block content %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user