1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-09-19 18:31:16 +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): class RegistrationForm(FlaskForm):
email = StringField("Email", email = StringField("Email",
validators=[DataRequired(), Email()]) validators=[DataRequired(), Email()])
assistant = BooleanField("Assistant", default=True)
admin = BooleanField("Admin", default=False) admin = BooleanField("Admin", default=False)
submit = SubmitField("Register") submit = SubmitField("Register")
def validate_email(self, email): def validate_email(form, field):
user = User.query.filter_by(email=email.data.lower()).first() user = User.query.filter_by(email=field.data.lower()).first()
if user: if user:
raise ValidationError("A user with this email address is already registered!") 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]"], ["Roles", "[role.name for role in row.roles]"],
["Assistant", "row.assistant"]] ["Assistant", "row.assistant"]]
table = makeTable(headerAndDataList=headerAndDataList, activeUsersTable = makeTable(headerAndDataList=headerAndDataList,
rows=User.query.filter(User.active == True).all(), rows=User.query.filter(User.active == True).all(),
tableId="usersTab") tableId="activeUsersTable")
inactiveUsersTable = makeTable(headerAndDataList=headerAndDataList,
rows=User.query.filter(User.active == False).all(),
tableId="inactiveUsersTable")
return render_template("users.html", return render_template("users.html",
table=table, activeUsersTable=activeUsersTable,
inactiveUsersTable=inactiveUsersTable,
) )
@ -248,21 +252,31 @@ def register():
passwordHash = hash_password(password) passwordHash = hash_password(password)
email = form.email.data.lower() email = form.email.data.lower()
assistant = form.assistant.data
admin = form.admin.data admin = form.admin.data
registered = True
if admin: if admin:
roles = ["admin"] if assistant:
roles = ["admin", "assistant"]
else:
roles = ["admin"]
flash("You have registered a new admin!", "danger") flash("You have registered a new admin!", "danger")
else: elif assistant:
roles = ["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", return render_template("register.html",
form=form, form=form,
) )

View file

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

View file

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

View file

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

View file

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