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

Form tuning

This commit is contained in:
Mo 2021-04-27 23:28:47 +02:00
parent c9023bdfc9
commit 73d24c5c55
2 changed files with 35 additions and 17 deletions

View file

@ -4,6 +4,7 @@ from flask_admin.menu import MenuLink
from flask_admin.contrib.sqla.filters import BaseSQLAFilter from flask_admin.contrib.sqla.filters import BaseSQLAFilter
from flask_security import hash_password from flask_security import hash_password
from wtforms import BooleanField, SelectField, TextField from wtforms import BooleanField, SelectField, TextField
from wtforms.validators import DataRequired, Email
from advlabdb import admin, app, user_datastore, db from advlabdb import admin, app, user_datastore, db
from advlabdb.models import * from advlabdb.models import *
@ -17,14 +18,19 @@ class UserModelView(SecureModelView):
column_list = ["email", "active", "roles", "assistant"] column_list = ["email", "active", "roles", "assistant"]
column_searchable_list = ["email"] column_searchable_list = ["email"]
column_filters = ["active"] column_filters = ["active"]
form_columns = ["email", "roles"] form_columns = ["email", "active", "roles"]
form_args = {
"email": {"validators": [Email()]},
"active": {"default": True},
"roles": {"validators": [DataRequired(message="A role is required!")]}
}
def create_model(self, form): def create_model(self, form):
password = randomPassword() password = randomPassword()
passwordHash = hash_password(password) passwordHash = hash_password(password)
email = form.email.data.lower() email = form.email.data.lower()
print(form.roles.data[0].name)
roles = [role.name for role in form.roles.data] roles = [role.name for role in form.roles.data]
if "admin" in roles: if "admin" in roles:
@ -50,7 +56,10 @@ class SemesterModelView(SecureModelView):
} }
def after_model_change(self, form, model, is_created): def after_model_change(self, form, model, is_created):
if is_created and form.create_parts.data: if form.create_parts.data:
if is_created == False and model.parts != []:
flash("This semester already has parts!", "danger")
return
for partLabel in partsLabels: for partLabel in partsLabels:
db.session.add(Part(label=partLabel, semester=model)) db.session.add(Part(label=partLabel, semester=model))
db.session.commit() db.session.commit()
@ -61,20 +70,7 @@ class PartModelView(SecureModelView):
column_details_list = ["label", "semester", "part_experiments", "part_students", "groups"] column_details_list = ["label", "semester", "part_experiments", "part_students", "groups"]
form_columns = ["label", "semester"] form_columns = ["label", "semester"]
form_choices = {"label": list(zip(partsLabels, partsLabels))}
class PartFilter(BaseSQLAFilter):
def apply(self, query, value, alias=None):
return query.filter(self.column == partFromLabelInUserActiveSemester(value).id)
def operation(self):
return "equals"
def validate(self, value):
if partFromLabelInUserActiveSemester(value):
return True
else:
flash(f"Part {value} not found in your active semester {userActiveSemester()}!", "danger")
return False
class StudentModelView(SecureModelView): class StudentModelView(SecureModelView):
@ -84,6 +80,10 @@ class StudentModelView(SecureModelView):
column_searchable_list = ["student_number", "email", "first_name", "last_name"] column_searchable_list = ["student_number", "email", "first_name", "last_name"]
form_columns = column_details_list + ["new_part_student_part", "new_part_student_group_number"] form_columns = column_details_list + ["new_part_student_part", "new_part_student_group_number"]
form_args = {
"email": {"validators": [Email()]},
}
partChoices = ["-"] + partsLabels partChoices = ["-"] + partsLabels
form_extra_fields = { form_extra_fields = {
"new_part_student_part": SelectField("Part", choices=list(zip(partChoices, partChoices)), default=partChoices[0]), "new_part_student_part": SelectField("Part", choices=list(zip(partChoices, partChoices)), default=partChoices[0]),
@ -132,8 +132,25 @@ class StudentModelView(SecureModelView):
flash("Added part student.", "success") flash("Added part student.", "success")
class PartFilter(BaseSQLAFilter):
def apply(self, query, value, alias=None):
return query.filter(self.column == partFromLabelInUserActiveSemester(value).id)
def operation(self):
return "equals"
def validate(self, value):
if partFromLabelInUserActiveSemester(value):
return True
else:
flash(f"Part {value} not found in your active semester {userActiveSemester()}!", "danger")
return False
class PartStudentModelView(SecureModelView): class PartStudentModelView(SecureModelView):
column_filters = [PartFilter(PartStudent.part_id, "Part", options=list(zip(partsLabels, partsLabels)))] column_filters = [PartFilter(PartStudent.part_id, "Part", options=list(zip(partsLabels, partsLabels)))]
form_excluded_columns = ["experiment_marks"]
admin.add_view(StudentModelView(Student, db.session)) admin.add_view(StudentModelView(Student, db.session))
admin.add_view(PartStudentModelView(PartStudent, db.session)) admin.add_view(PartStudentModelView(PartStudent, db.session))

View file

@ -4,6 +4,7 @@ from flask_security import current_user
from flask import flash from flask import flash
from advlabdb.models import Semester from advlabdb.models import Semester
from advlabdb import db
def makeTable(headerAndDataList, rows, tableId="table"): def makeTable(headerAndDataList, rows, tableId="table"):
def cellString(cell): def cellString(cell):