1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-11-08 21:21:06 +00:00
AdvLabDB/advlabdb/modelViews.py

53 lines
2.1 KiB
Python

from flask import url_for, render_template, flash
from advlabdb.customClasses import SecureModelView
from flask_admin.menu import MenuLink
from flask_security import hash_password
from wtforms import BooleanField
from advlabdb import admin, app, user_datastore
from advlabdb.models import *
from advlabdb.utils import randomPassword
class UserModelView(SecureModelView):
column_list = ["email", "active", "roles", "assistant"]
column_searchable_list = ["email"]
column_filters = ["active"]
form_columns = ["email", "roles"]
def create_model(self, form):
password = randomPassword()
passwordHash = hash_password(password)
email = form.email.data.lower()
print(form.roles.data[0].name)
roles = [role.name for role in form.roles.data]
if "admin" in roles:
flash("You have registered a new admin!", "danger")
newUser = user_datastore.create_user(email=email, password=passwordHash, roles=roles)
db.session.commit()
flash(f"{email} registered with roles: {', '.join([role.name for role in form.roles.data])}.", category="success")
flash(f"Random password: {password}", category="warning")
return newUser
class RoleModelView(SecureModelView):
column_exclude_list = ["update_datetime"]
admin.add_view(SecureModelView(Student, db.session))
admin.add_view(SecureModelView(PartStudent, db.session))
admin.add_view(SecureModelView(Group, db.session))
admin.add_view(SecureModelView(GroupExperiment, db.session))
admin.add_view(SecureModelView(Experiment, db.session))
admin.add_view(SecureModelView(PartExperiment, db.session))
admin.add_view(SecureModelView(Assistant, db.session))
admin.add_view(SecureModelView(Appointment, db.session))
admin.add_view(SecureModelView(Part, db.session))
admin.add_view(SecureModelView(Semester, db.session))
admin.add_view(SecureModelView(ExperimentMark, db.session))
admin.add_view(UserModelView(User, db.session))
admin.add_view(RoleModelView(Role, db.session))
with app.app_context():
admin.add_link(MenuLink(name="Home", url=url_for("index"), category="Links"))