1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-09-17 18:31:15 +00:00

Added registering as admin and assistant, added inactive users table.

This commit is contained in:
Mo 2021-04-18 17:24:40 +02:00
parent 4967496d49
commit 66f4a056b7
6 changed files with 44 additions and 25 deletions

View file

@ -7,11 +7,12 @@ from advlabdb.models import User
class RegistrationForm(FlaskForm):
email = StringField("Email",
validators=[DataRequired(), Email()])
assistant = BooleanField("Assistant", default=True)
admin = BooleanField("Admin", default=False)
submit = SubmitField("Register")
def validate_email(self, email):
user = User.query.filter_by(email=email.data.lower()).first()
def validate_email(form, field):
user = User.query.filter_by(email=field.data.lower()).first()
if user:
raise ValidationError("A user with this email address is already registered!")

View file

@ -188,12 +188,16 @@ def users():
["Roles", "[role.name for role in row.roles]"],
["Assistant", "row.assistant"]]
table = makeTable(headerAndDataList=headerAndDataList,
rows=User.query.filter(User.active == True).all(),
tableId="usersTab")
activeUsersTable = makeTable(headerAndDataList=headerAndDataList,
rows=User.query.filter(User.active == True).all(),
tableId="activeUsersTable")
inactiveUsersTable = makeTable(headerAndDataList=headerAndDataList,
rows=User.query.filter(User.active == False).all(),
tableId="inactiveUsersTable")
return render_template("users.html",
table=table,
activeUsersTable=activeUsersTable,
inactiveUsersTable=inactiveUsersTable,
)
@ -248,21 +252,31 @@ def register():
passwordHash = hash_password(password)
email = form.email.data.lower()
assistant = form.assistant.data
admin = form.admin.data
registered = True
if admin:
roles = ["admin"]
if assistant:
roles = ["admin", "assistant"]
else:
roles = ["admin"]
flash("You have registered a new admin!", "danger")
else:
elif assistant:
roles = ["assistant"]
else:
flash("The user has to be assistant and/or admin!", "warning")
registered = False
if registered:
newUser = user_datastore.create_user(email=email, password=passwordHash, roles=roles)
db.session.commit()
return render_template("registered.html",
email=email,
password=password,
roles=[role.name for role in newUser.roles],
)
user_datastore.create_user(email=email, password=passwordHash, roles=roles)
db.session.commit()
return render_template("registered.html",
email=email,
password=password,
admin=admin,
)
return render_template("register.html",
form=form,
)

View file

@ -3,6 +3,7 @@
{% block content %}
<div class="content-section">
<form method="POST">
{{form.hidden_tag()}}
<fieldset>
<div>
{{form.email.label(class="form-control-label")}}
@ -18,7 +19,12 @@
{{form.email(class="form-control form-control-lg")}}
{% endif %}
</div>
<br>
Roles
<div class="form-check">
{{form.assistant(class="form-check-input")}}
{{form.assistant.label(class="form-check-label")}}
<br>
{{form.admin(class="form-check-input")}}
{{form.admin.label(class="form-check-label")}}
</div>

View file

@ -2,13 +2,7 @@
{% extends "layout.html" %}
{% block content %}
<h3>
Registered as
{% if admin %}
<strong>Admin</strong>
{% else %}
Assistant
{% endif %}
!
New user registered with roles {{roles}}.
</h3>
<p>
Email: {{email}}

View file

@ -2,7 +2,8 @@
{% extends "layout.html" %}
{% block content %}
{{table|safe}}
<h3>Active users:</h3>
{{activeUsersTable|safe}}
<div>
<button id="deactivateUsersButton" class="btn btn-warning">Deactivate users</button>
</div>
@ -11,11 +12,14 @@
<button type="submit" name="registerUser" class="btn btn-primary">Register a new user</button>
</form>
<hr>
<h3>Inactive users:</h3>
{{inactiveUsersTable|safe}}
{% endblock content %}
{% block scripts %}
<script>
let $table = $('#usersTab')
let $table = $('#activeUsersTable')
let $button = $('#deactivateUsersButton')
$(function() {

View file

@ -1,7 +1,7 @@
from random import choice
from string import digits, ascii_letters
def makeTable(headerAndDataList, rows, tableId="tab"):
def makeTable(headerAndDataList, rows, tableId="table"):
def cellString(cell):
cell = str(cell)
if cell == "[]":