From 73d24c5c55bb7d6b0cef2563ca61448c5fef85a6 Mon Sep 17 00:00:00 2001 From: Mo8it Date: Tue, 27 Apr 2021 23:28:47 +0200 Subject: [PATCH] Form tuning --- advlabdb/modelViews.py | 51 ++++++++++++++++++++++++++++-------------- advlabdb/utils.py | 1 + 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/advlabdb/modelViews.py b/advlabdb/modelViews.py index a4e38a9..c22770a 100644 --- a/advlabdb/modelViews.py +++ b/advlabdb/modelViews.py @@ -4,6 +4,7 @@ from flask_admin.menu import MenuLink from flask_admin.contrib.sqla.filters import BaseSQLAFilter from flask_security import hash_password from wtforms import BooleanField, SelectField, TextField +from wtforms.validators import DataRequired, Email from advlabdb import admin, app, user_datastore, db from advlabdb.models import * @@ -17,14 +18,19 @@ class UserModelView(SecureModelView): column_list = ["email", "active", "roles", "assistant"] column_searchable_list = ["email"] 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): 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: @@ -50,7 +56,10 @@ class SemesterModelView(SecureModelView): } 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: db.session.add(Part(label=partLabel, semester=model)) db.session.commit() @@ -61,20 +70,7 @@ class PartModelView(SecureModelView): column_details_list = ["label", "semester", "part_experiments", "part_students", "groups"] form_columns = ["label", "semester"] - -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 + form_choices = {"label": list(zip(partsLabels, partsLabels))} class StudentModelView(SecureModelView): @@ -84,6 +80,10 @@ class StudentModelView(SecureModelView): 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_args = { + "email": {"validators": [Email()]}, + } + partChoices = ["-"] + partsLabels form_extra_fields = { "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") +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): 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(PartStudentModelView(PartStudent, db.session)) diff --git a/advlabdb/utils.py b/advlabdb/utils.py index d62e02e..01d0f87 100644 --- a/advlabdb/utils.py +++ b/advlabdb/utils.py @@ -4,6 +4,7 @@ from flask_security import current_user from flask import flash from advlabdb.models import Semester +from advlabdb import db def makeTable(headerAndDataList, rows, tableId="table"): def cellString(cell):