mirror of
https://github.com/StefBuwalda/WebTech.git
synced 2025-10-30 19:29:58 +00:00
YOU CAN ADD ACCOUNTS
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
from flask_wtf import FlaskForm # type: ignore
|
from flask_wtf import FlaskForm # type: ignore
|
||||||
from wtforms import StringField, PasswordField, SubmitField, URLField, BooleanField
|
from wtforms import (
|
||||||
|
StringField,
|
||||||
|
PasswordField,
|
||||||
|
SubmitField,
|
||||||
|
URLField,
|
||||||
|
BooleanField,
|
||||||
|
)
|
||||||
from wtforms.validators import DataRequired
|
from wtforms.validators import DataRequired
|
||||||
|
|
||||||
|
|
||||||
@@ -8,8 +14,12 @@ class ServiceForm(FlaskForm):
|
|||||||
url = URLField("Service URL:", validators=[DataRequired()])
|
url = URLField("Service URL:", validators=[DataRequired()])
|
||||||
submit = SubmitField("Add")
|
submit = SubmitField("Add")
|
||||||
|
|
||||||
|
|
||||||
class RegisterForm(FlaskForm):
|
class RegisterForm(FlaskForm):
|
||||||
username = StringField("Username", validators=[DataRequired()])
|
username = StringField("Username", validators=[DataRequired()])
|
||||||
password = PasswordField("Password", validators=[DataRequired()])
|
password = PasswordField("Password", validators=[DataRequired()])
|
||||||
admin = BooleanField("Admin")
|
confirm_password = PasswordField(
|
||||||
|
"Confirm Password", validators=[DataRequired()]
|
||||||
|
)
|
||||||
|
is_admin = BooleanField("Admin")
|
||||||
submit = SubmitField("Add")
|
submit = SubmitField("Add")
|
||||||
@@ -18,10 +18,10 @@ Register
|
|||||||
{{ form.password.label }} <br> {{ form.password() }}
|
{{ form.password.label }} <br> {{ form.password() }}
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
Confirm {{ form.password.label }} <br> {{ form.password() }}
|
{{ form.confirm_password.label }} <br> {{ form.confirm_password() }}
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
{{ form.admin }} {{ form.admin.label }}
|
{{ form.is_admin }} {{ form.is_admin.label }}
|
||||||
</div>
|
</div>
|
||||||
<div class="submit">
|
<div class="submit">
|
||||||
{{ form.submit() }}
|
{{ form.submit() }}
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
|
from application import db
|
||||||
from flask import Blueprint, render_template
|
from flask import Blueprint, render_template
|
||||||
from application.dash.forms import RegisterForm, ServiceForm
|
from application.dash.forms import RegisterForm, ServiceForm
|
||||||
from flask_login import login_required # type: ignore
|
from flask_login import login_required # 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 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")
|
||||||
|
|
||||||
@@ -17,15 +20,39 @@ def index():
|
|||||||
|
|
||||||
|
|
||||||
@dash_blueprint.route("/admin", methods=["GET", "POST"])
|
@dash_blueprint.route("/admin", methods=["GET", "POST"])
|
||||||
# @admin_required
|
@admin_required
|
||||||
def admin():
|
def admin():
|
||||||
register_form = RegisterForm()
|
register_form = RegisterForm()
|
||||||
|
|
||||||
if register_form.validate_on_submit:
|
if register_form.validate_on_submit():
|
||||||
username = register_form.username.data
|
username = register_form.username.data
|
||||||
password = register_form.password.data
|
password = register_form.password.data
|
||||||
check_admin = register_form.admin.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,
|
||||||
|
password=generate_password_hash(password),
|
||||||
|
is_admin=is_admin,
|
||||||
|
)
|
||||||
|
db.session.add(new_user)
|
||||||
|
db.session.commit()
|
||||||
|
return render_template(
|
||||||
|
"admin.html",
|
||||||
|
form=RegisterForm(formdata=None),
|
||||||
|
feedback="Account succesvol toegevoegd",
|
||||||
|
)
|
||||||
return render_template("admin.html", form=register_form)
|
return render_template("admin.html", form=register_form)
|
||||||
|
|
||||||
@dash_blueprint.route("/service", methods=["GET", "POST"])
|
@dash_blueprint.route("/service", methods=["GET", "POST"])
|
||||||
|
|||||||
Reference in New Issue
Block a user