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

766 lines
25 KiB
Python
Raw Normal View History

2021-06-02 21:43:41 +00:00
from flask import flash, request, url_for
2021-07-30 12:20:54 +00:00
from flask_admin.contrib.sqla.fields import QuerySelectField, QuerySelectMultipleField
from flask_admin.contrib.sqla.filters import BaseSQLAFilter
2021-07-30 12:20:54 +00:00
from flask_admin.helpers import get_form_data
2021-06-02 21:43:41 +00:00
from flask_admin.menu import MenuLink
2021-07-01 14:38:37 +00:00
from flask_admin.model.template import EndpointLinkRowAction
2021-07-30 12:20:54 +00:00
from flask_security import admin_change_password, current_user, hash_password
2021-07-29 21:18:24 +00:00
from sqlalchemy import func, or_
2021-07-30 12:20:54 +00:00
from wtforms import BooleanField, Form, RadioField, SelectField, TextField
2021-07-01 11:12:43 +00:00
from wtforms.fields.html5 import DateField
from wtforms.validators import URL, DataRequired, Email, NumberRange, Optional
from advlabdb import adminSpace, app, assistantSpace, db, user_datastore
2021-06-01 23:56:49 +00:00
from advlabdb.configUtils import getConfig
2021-07-30 00:03:44 +00:00
from advlabdb.customClasses import SecureAdminModelView
2021-07-30 12:20:54 +00:00
from advlabdb.exceptions import DataBaseException, ModelViewException
2021-06-02 21:43:41 +00:00
from advlabdb.models import (
Appointment,
Assistant,
Experiment,
ExperimentMark,
Group,
GroupExperiment,
Part,
PartStudent,
2021-07-30 12:20:54 +00:00
Program,
2021-06-02 21:43:41 +00:00
Role,
Semester,
2021-07-30 12:20:54 +00:00
SemesterExperiment,
2021-06-02 21:43:41 +00:00
Student,
User,
)
from advlabdb.utils import (
initActiveSemesterMenuLinks,
randomPassword,
setUserActiveSemester,
userActiveSemester,
)
2021-06-02 21:43:41 +00:00
2021-08-15 23:15:19 +00:00
def semesterExperimentQueryFactory():
return SemesterExperiment.query.filter(SemesterExperiment.semester == userActiveSemester())
2021-07-30 00:03:44 +00:00
class UserView(SecureAdminModelView):
2021-07-29 18:55:22 +00:00
class CreateForm(Form):
def roleQueryFactory():
return Role.query
def semesterQueryFactory():
return Semester.query
def activeSemesterDefault():
return userActiveSemester()
2021-07-29 18:55:22 +00:00
email = TextField("Email", validators=[DataRequired(), Email()])
roles = QuerySelectMultipleField(
"Roles",
query_factory=roleQueryFactory,
validators=[DataRequired()],
default=[Role.query.filter(Role.name == "assistant").first()],
)
2021-08-15 23:15:19 +00:00
first_name = TextField("First Name", validators=[DataRequired()])
last_name = TextField("Last Name", validators=[DataRequired()])
phone_number = TextField("Phone Number")
mobile_phone_number = TextField("Mobile Phone Number")
room = TextField("Room")
building = TextField("Building")
semester_experiments = QuerySelectMultipleField(
"Semester Experiments",
query_factory=semesterExperimentQueryFactory,
allow_blank=True,
blank_text="-",
description="Only needed if the user has the assistant role",
)
2021-07-29 18:55:22 +00:00
active = BooleanField("Active", default=True)
active_semester = QuerySelectField(
"Active Semester",
query_factory=semesterQueryFactory,
allow_blank=True,
blank_text="-",
default=activeSemesterDefault,
2021-07-29 18:55:22 +00:00
)
class EditForm(CreateForm):
2021-08-15 23:15:19 +00:00
semester_experiments = None
2021-07-29 18:55:22 +00:00
generate_new_password = BooleanField("Generate new random password", default=False)
form = EditForm
2021-07-29 18:03:41 +00:00
can_view_details = True
2021-08-15 23:15:19 +00:00
column_list = ["first_name", "last_name", "email", "active", "roles", "assistant", "active_semester"]
column_details_list = column_list + [
"phone_number",
"mobile_phone_number",
"room",
"building",
"create_datetime",
"update_datetime",
]
column_searchable_list = ["first_name", "last_name", "email"]
column_filters = ["active", "active_semester"]
2021-07-01 11:12:43 +00:00
column_editable_list = ["active"]
2021-04-27 21:28:47 +00:00
2021-07-29 18:55:22 +00:00
def create_form(self, obj=None):
form = self.CreateForm
return form(get_form_data(), obj=obj)
def flashPassword(password):
flash(f"Random password: {password}", category="warning")
def create_model(self, form):
2021-07-29 22:24:10 +00:00
try:
password = randomPassword()
hashedPassword = hash_password(password)
2021-07-29 22:24:10 +00:00
email = form.email.data.lower()
2021-07-29 22:24:10 +00:00
roles = [role.name for role in form.roles.data]
model = user_datastore.create_user(
2021-08-15 23:15:19 +00:00
email=email,
password=hashedPassword,
roles=roles,
first_name=form.first_name.data,
last_name=form.last_name.data,
phone_number=form.phone_number.data,
mobile_phone_number=form.mobile_phone_number.data,
room=form.room.data,
building=form.building.data,
active=form.active.data,
active_semester=form.active_semester.data,
)
2021-08-15 23:15:19 +00:00
if "assistant" in roles:
assistant = Assistant(user=model, semester_experiments=form.semester_experiments.data)
self.session.add(assistant)
self.on_model_change(form, model, True)
self.session.commit()
except Exception as ex:
2021-07-11 12:27:00 +00:00
flash(str(ex), "error")
self.session.rollback()
else:
flash(
f"{email} registered with roles: {', '.join([role.name for role in form.roles.data])}.",
category="success",
)
2021-07-29 18:55:22 +00:00
UserView.flashPassword(password)
self.after_model_change(form, model, True)
return model
2021-06-09 00:22:37 +00:00
def on_model_delete(self, model):
if model == current_user:
raise ModelViewException("Tried to delete yourself as user!")
2021-06-09 00:22:37 +00:00
def on_model_change(self, form, model, is_created):
2021-07-29 22:24:10 +00:00
if not is_created:
if model == current_user and not form.active.data:
raise ModelViewException("Tried to deactiavte yourself as user!")
2021-06-09 00:22:37 +00:00
2021-07-30 13:14:35 +00:00
if hasattr(form, "generate_new_password") and form.generate_new_password.data:
2021-07-29 22:24:10 +00:00
password = randomPassword()
2021-07-29 18:55:22 +00:00
2021-07-29 22:24:10 +00:00
UserView.flashPassword(password)
2021-07-29 18:55:22 +00:00
2021-07-29 22:24:10 +00:00
admin_change_password(
model, password, notify=False
) # Password is automatically hashed with this method
2021-07-29 18:55:22 +00:00
2021-07-30 00:03:44 +00:00
class RoleView(SecureAdminModelView):
2021-06-09 00:22:37 +00:00
can_create = False
can_edit = False
can_delete = False
column_display_actions = False
column_list = ["name", "description"]
2021-04-24 11:38:03 +00:00
2021-07-30 00:03:44 +00:00
class SemesterView(SecureAdminModelView):
2021-07-29 14:27:15 +00:00
class CreateForm(Form):
def labelDefault():
if userActiveSemester().label == "WS":
return "SS"
else:
return "WS"
def yearDefault():
activeSemester = userActiveSemester()
if activeSemester.label == "WS":
return activeSemester.year + 1
else:
return activeSemester.year
label = RadioField("Semester", choices=["WS", "SS"], validators=[DataRequired()], default=labelDefault)
year = TextField("Year", validators=[DataRequired()], default=yearDefault)
2021-07-29 14:27:15 +00:00
transfer_parts = BooleanField(
2021-07-01 14:38:37 +00:00
"Transfer parts",
description="This option transfers the parts you have in your current active semester. Make sure that your semester is the last semester before creating a new one (recommended)!",
default=True,
2021-07-29 14:27:15 +00:00
)
transfer_assistants = BooleanField(
2021-07-01 14:38:37 +00:00
"Transfer Assistants",
2021-07-11 01:17:21 +00:00
description="This option transfers assistants of your active semester to active experiments in the new semester. Make sure that your semester is the last semester before creating a new one (recommended)! Active experiments are transfered anyway. If you do not want an experiment to be transfered, set it to inactive before creating the new semester. Experiments which are switched to active before creating the new semester will be created in the new semester without assistants. It is important to check the assistants of all experiments after creating a new semester.",
2021-07-01 14:38:37 +00:00
default=True,
2021-07-29 14:27:15 +00:00
)
form = CreateForm
can_edit = False
can_delete = False
can_view_details = True
column_list = ["label", "year", "parts"]
column_details_list = column_list + ["semester_experiments", "active_users", "groups"]
column_searchable_list = ["label", "year"]
2021-04-24 11:38:03 +00:00
2021-07-29 22:24:10 +00:00
def customCreateModel(self, form):
return Semester.customInit(
label=form.label.data,
year=form.year.data,
oldSemester=userActiveSemester(),
transferParts=form.transfer_parts.data,
transferAssistants=form.transfer_assistants.data,
)
def addMenuLink(space, newSemester):
space.add_link(
MenuLink(
name=newSemester.repr(),
url=url_for("set_semester") + "?semester_id=" + str(newSemester.id),
category="Active semester",
)
)
def after_model_change(self, form, model, is_created):
setUserActiveSemester(model.id)
SemesterView.addMenuLink(adminSpace, model)
SemesterView.addMenuLink(assistantSpace, model)
2021-04-24 11:38:03 +00:00
2021-07-29 22:24:10 +00:00
def programQueryFactory():
return Program.query
2021-07-30 00:03:44 +00:00
class PartView(SecureAdminModelView):
2021-04-24 11:38:03 +00:00
can_view_details = True
2021-07-28 22:58:50 +00:00
2021-07-29 14:27:15 +00:00
column_sortable_list = []
column_list = ["program", "number", "semester"]
column_details_list = column_list + ["part_students"]
2021-07-29 22:24:10 +00:00
form_columns = ["program", "number"]
form_extra_fields = {
"program": QuerySelectField(
"Program", query_factory=programQueryFactory, validators=[DataRequired()], allow_blank=True, blank_text="-"
)
}
2021-04-24 11:38:03 +00:00
2021-07-12 14:42:11 +00:00
def queryFilter(self):
2021-07-13 01:37:00 +00:00
return Part.semester == userActiveSemester()
2021-06-10 01:14:30 +00:00
2021-07-29 22:24:10 +00:00
def customCreateModel(self, form):
return Part(program=form.program.data, number=form.number.data, semester=userActiveSemester())
2021-07-30 00:03:44 +00:00
class StudentView(SecureAdminModelView):
can_view_details = True
column_list = ["student_number", "first_name", "last_name", "uni_email", "contact_email", "part_students"]
2021-04-26 22:26:11 +00:00
column_details_list = column_list + ["bachelor_thesis", "bachelor_thesis_work_group", "note"]
column_sortable_list = ["student_number", "first_name", "last_name"]
column_searchable_list = column_sortable_list + ["uni_email", "contact_email"]
form_excluded_columns = ["part_students"]
2021-04-26 22:26:11 +00:00
2021-04-27 21:28:47 +00:00
form_args = {
"uni_email": {"validators": [Email()]},
"contact_email": {"validators": [Email()]},
2021-04-27 21:28:47 +00:00
}
2021-07-01 14:38:37 +00:00
column_extra_row_actions = [
EndpointLinkRowAction(
"glyphicon glyphicon-time",
id_arg="flt1_0",
title="Experiments history",
endpoint="experimentmark.index_view",
)
]
2021-06-10 01:14:30 +00:00
2021-07-12 14:42:11 +00:00
def partQueryFactory():
2021-07-30 13:14:35 +00:00
return Part.query.filter(Part.semester == userActiveSemester())
2021-06-10 01:14:30 +00:00
2021-07-12 14:42:11 +00:00
def groupQueryFactory():
2021-07-14 02:27:17 +00:00
return Group.query.filter(Group.semester == userActiveSemester())
2021-07-01 11:12:43 +00:00
2021-07-13 01:37:00 +00:00
markChoices = [(-1, "-")] + list(zip(range(16)[::-1], range(16)[::-1]))
2021-07-30 00:03:44 +00:00
class PartStudentView(SecureAdminModelView):
class CreateForm(Form):
2021-07-12 14:42:11 +00:00
def studentQueryFactory():
return Student.query
2021-04-26 22:26:11 +00:00
student = QuerySelectField(
2021-07-13 01:37:00 +00:00
"Student",
query_factory=studentQueryFactory,
validators=[DataRequired()],
allow_blank=True,
blank_text="-",
)
part = QuerySelectField(
2021-07-13 01:37:00 +00:00
"Part",
query_factory=partQueryFactory,
validators=[DataRequired()],
allow_blank=True,
blank_text="-",
)
group = QuerySelectField(
"Group",
query_factory=groupQueryFactory,
allow_blank=True,
blank_text="-",
)
2021-04-26 22:26:11 +00:00
class EditForm(CreateForm):
student = None
part = None
2021-07-13 01:37:00 +00:00
final_part_mark = SelectField("Final Part Mark", choices=markChoices, coerce=int)
form = EditForm
column_filters = ["part", "student", "group"]
2021-07-12 14:42:11 +00:00
def queryFilter(self):
2021-07-30 13:14:35 +00:00
return PartStudent.part.has(Part.semester == userActiveSemester())
2021-07-12 14:42:11 +00:00
def create_form(self, obj=None):
form = self.CreateForm
return form(get_form_data(), obj=obj)
2021-04-27 21:28:47 +00:00
def on_model_change(self, form, model, is_created):
2021-07-29 19:13:49 +00:00
PartStudent.check(model.group, model.part)
2021-04-27 21:28:47 +00:00
2021-07-12 17:49:10 +00:00
def update_model(self, form, model):
if form.final_part_mark.data == -1:
form.final_part_mark.data = None
return super().update_model(form, model)
2021-07-29 21:18:24 +00:00
def partStudentQueryFactory():
2021-07-30 13:14:35 +00:00
return PartStudent.query.filter(PartStudent.part.has(Part.semester == userActiveSemester()))
2021-07-13 01:37:00 +00:00
2021-07-30 00:03:44 +00:00
class GroupView(SecureAdminModelView):
2021-07-29 21:18:24 +00:00
def formFactory(create, group):
if create:
2021-07-29 21:18:24 +00:00
def query_factory():
return partStudentQueryFactory().filter(PartStudent.group == None)
else:
def query_factory():
return partStudentQueryFactory().filter(or_(PartStudent.group == None, PartStudent.group == group))
class CustomForm(Form):
part_students = QuerySelectMultipleField(
"Part Students", query_factory=query_factory, validators=[DataRequired()]
)
return CustomForm
2021-07-14 02:27:17 +00:00
column_list = ["number", "semester", "program", "part_students", "group_experiments"]
column_filters = ["number", "semester", "program"]
2021-07-12 14:42:11 +00:00
def queryFilter(self):
2021-07-14 02:27:17 +00:00
return Group.semester == userActiveSemester()
2021-07-12 14:42:11 +00:00
2021-07-29 22:24:10 +00:00
def customCreateModel(self, form):
return Group.customInit(form.part_students.data)
def update_model(self, form, model):
2021-07-29 19:13:49 +00:00
try:
Group.check(form.part_students.data, model.program)
except Exception as ex:
flash(str(ex), "error")
else:
return super().update_model(form, model)
2021-07-29 21:18:24 +00:00
def create_form(self, obj=None):
form = GroupView.formFactory(create=True, group=None)
return form(get_form_data(), obj=obj)
def edit_form(self, obj=None):
form = GroupView.formFactory(create=False, group=obj)
return form(get_form_data(), obj=obj)
2021-06-07 15:15:10 +00:00
2021-07-30 00:03:44 +00:00
class ExperimentView(SecureAdminModelView):
2021-06-21 16:07:18 +00:00
can_view_details = True
2021-07-28 22:58:50 +00:00
column_filters = ["active"]
2021-07-14 02:27:17 +00:00
column_list = ["number", "program", "title", "active"]
column_details_list = column_list + [
2021-07-28 11:59:41 +00:00
"description",
"wiki_link",
"room",
"building",
"responsibility",
"duration_in_days",
"oral_weighting",
"protocol_weighting",
"final_weighting",
"semester_experiments",
]
column_editable_list = ["active"]
2021-06-21 16:26:38 +00:00
2021-07-28 11:59:41 +00:00
form_columns = column_details_list
2021-07-13 16:41:00 +00:00
form_args = {"wiki_link": {"validators": [URL()]}}
form_extra_fields = {
"program": QuerySelectField(
"Program", query_factory=programQueryFactory, validators=[DataRequired()], allow_blank=True, blank_text="-"
)
}
2021-07-13 16:41:00 +00:00
2021-06-21 16:26:38 +00:00
2021-07-28 22:31:43 +00:00
def assistantQueryFactory():
2021-07-30 13:14:35 +00:00
return Assistant.query.filter(Assistant.user.has(User.active == True))
2021-07-28 22:31:43 +00:00
2021-07-30 00:03:44 +00:00
class SemesterExperimentView(SecureAdminModelView):
2021-07-28 22:31:43 +00:00
class CreateForm(Form):
def experimentQueryFactory():
return Experiment.query.filter(Experiment.active == True)
experiment = QuerySelectField(
"Experiment",
query_factory=experimentQueryFactory,
validators=[DataRequired()],
allow_blank=True,
blank_text="-",
)
assistants = QuerySelectMultipleField("Assistants", query_factory=assistantQueryFactory)
form = CreateForm
can_view_details = True
column_list = ["experiment", "assistants", "semester"]
column_details_list = column_list + ["group_experiments"]
2021-07-28 22:58:50 +00:00
column_filters = ["experiment"]
2021-06-21 16:26:38 +00:00
2021-07-12 14:42:11 +00:00
def queryFilter(self):
return SemesterExperiment.semester == userActiveSemester()
2021-06-21 16:07:18 +00:00
2021-07-29 22:24:10 +00:00
def customCreateModel(self, form):
return SemesterExperiment(
semester=userActiveSemester(), experiment=form.experiment.data, assistants=form.assistants.data
)
2021-07-28 22:31:43 +00:00
2021-06-21 16:07:18 +00:00
2021-07-30 00:03:44 +00:00
class AssistantView(SecureAdminModelView):
def assistantUserQueryFactory():
return User.query.filter(User.roles.any(Role.name == "assistant"))
2021-06-24 17:24:14 +00:00
can_view_details = True
2021-07-28 22:58:50 +00:00
2021-08-15 23:15:19 +00:00
column_list = ["user.first_name", "user.last_name", "user", "semester_experiments"]
2021-07-01 11:12:43 +00:00
column_details_list = column_list + [
2021-08-15 23:15:19 +00:00
"user.phone_number",
"user.mobile_phone_number",
"user.room",
"user.building",
2021-07-01 11:12:43 +00:00
"appointments",
"experiment_marks",
]
2021-08-15 23:15:19 +00:00
column_searchable_list = ["user.first_name", "user.last_name", "user.email"]
2021-06-24 17:24:14 +00:00
column_filters = ["user.active"]
2021-08-15 23:15:19 +00:00
form_excluded_columns = ["experiment_marks", "appointments"]
form_extra_fields = {
"user": QuerySelectField(
"User",
query_factory=assistantUserQueryFactory,
validators=[DataRequired()],
allow_blank=True,
blank_text="-",
2021-08-15 23:15:19 +00:00
),
"semester_experiments": QuerySelectMultipleField(
"Semester Experiments",
query_factory=semesterExperimentQueryFactory,
allow_blank=True,
blank_text="-",
),
}
2021-06-24 17:24:14 +00:00
2021-07-12 14:42:11 +00:00
assistantBlankText = "Auto assign if experiment has only one assistant"
2021-07-30 00:03:44 +00:00
class GroupExperimentView(SecureAdminModelView):
2021-07-01 11:12:43 +00:00
class CreateForm(Form):
group = QuerySelectField(
2021-07-13 01:37:00 +00:00
"Group",
query_factory=groupQueryFactory,
validators=[DataRequired()],
allow_blank=True,
blank_text="-",
2021-07-01 11:12:43 +00:00
)
semester_experiment = QuerySelectField(
"Semester Experiment",
query_factory=semesterExperimentQueryFactory,
2021-07-01 11:12:43 +00:00
validators=[DataRequired()],
allow_blank=True,
blank_text="-",
)
2021-07-01 17:43:59 +00:00
appointment1_date = DateField("Appointment-1 Date", validators=[Optional()])
2021-07-12 14:42:11 +00:00
appointment1_special = BooleanField("Appointment-1 Special", default=False)
2021-07-01 11:12:43 +00:00
appointment1_assistant = QuerySelectField(
2021-07-01 14:38:37 +00:00
"Appointment-1 Assistant",
2021-07-01 11:12:43 +00:00
query_factory=assistantQueryFactory,
allow_blank=True,
blank_text=assistantBlankText,
)
2021-07-01 17:43:59 +00:00
appointment2_date = DateField("Appointment-2 Date", validators=[Optional()])
2021-07-12 14:42:11 +00:00
appointment2_special = BooleanField("Appointment-2 Special", default=False)
2021-07-01 11:12:43 +00:00
appointment2_assistant = QuerySelectField(
2021-07-01 14:38:37 +00:00
"Appointment-2 Assistant",
2021-07-01 11:12:43 +00:00
query_factory=assistantQueryFactory,
allow_blank=True,
blank_text=assistantBlankText,
)
form = CreateForm
2021-07-01 14:38:37 +00:00
can_edit = False
2021-07-28 22:31:43 +00:00
column_list = ["group", "semester_experiment", "appointments", "experiment_marks"]
column_filters = ["group", "semester_experiment.experiment", "appointments"]
2021-06-24 17:39:26 +00:00
2021-07-12 14:42:11 +00:00
def queryFilter(self):
2021-07-30 13:14:35 +00:00
return GroupExperiment.group.has(Group.semester == userActiveSemester())
2021-07-29 22:24:10 +00:00
def customCreateModel(self, form):
return GroupExperiment.customInit(semester_experiment=form.semester_experiment.data, group=form.group.data)
2021-07-01 17:43:59 +00:00
2021-07-29 22:24:10 +00:00
def on_model_change(self, form, model, is_created):
if is_created:
2021-07-12 12:48:29 +00:00
for date, special, assistant in zip(
2021-07-01 17:43:59 +00:00
[form.appointment1_date.data, form.appointment2_date.data],
[form.appointment1_special.data, form.appointment2_special.data],
[form.appointment1_assistant.data, form.appointment2_assistant.data],
):
2021-07-12 12:48:29 +00:00
if date:
appointment = Appointment.customInit(date, special, assistant, model)
2021-07-01 17:43:59 +00:00
self.session.add(appointment)
2021-07-29 22:24:10 +00:00
def after_model_change(self, form, model, is_created):
if is_created:
if model.appointments:
flash(f"Appointments {model.appointments} added.", "success")
2021-07-01 17:43:59 +00:00
2021-06-24 17:39:26 +00:00
2021-07-12 14:42:11 +00:00
def groupExperimentQueryFactory():
return GroupExperiment.query.filter(
2021-07-30 13:14:35 +00:00
GroupExperiment.semester_experiment.has(SemesterExperiment.semester == userActiveSemester())
2021-07-12 14:42:11 +00:00
)
2021-07-30 00:03:44 +00:00
class AppointmentView(SecureAdminModelView):
2021-07-12 14:42:11 +00:00
class CreateForm(Form):
group_experiment = QuerySelectField(
2021-07-13 01:37:00 +00:00
"Group Experiment",
query_factory=groupExperimentQueryFactory,
validators=[DataRequired()],
allow_blank=True,
blank_text="-",
2021-07-12 14:42:11 +00:00
)
date = DateField("Date", validators=[DataRequired()])
special = BooleanField("Special", default=False)
assistant = QuerySelectField(
"Assistant",
query_factory=assistantQueryFactory,
allow_blank=True,
blank_text=assistantBlankText,
)
form = CreateForm
column_filters = [
"date",
"special",
"group_experiment.group",
"group_experiment.semester_experiment.experiment",
2021-08-15 23:15:19 +00:00
"assistant.user",
]
2021-07-30 21:42:20 +00:00
column_editable_list = ["date", "special"]
2021-07-12 14:42:11 +00:00
def queryFilter(self):
2021-07-30 13:14:35 +00:00
return Appointment.group_experiment.has(
GroupExperiment.semester_experiment.has(SemesterExperiment.semester == userActiveSemester())
2021-07-12 14:42:11 +00:00
)
2021-07-29 22:24:10 +00:00
def customCreateModel(self, form):
return Appointment.customInit(
form.date.data, form.special.data, form.assistant.data, form.group_experiment.data
)
2021-07-12 14:42:11 +00:00
def update_model(self, form, model):
if not form.date or not form.special: # For editables
return super().update_model(form, model)
2021-07-12 14:42:11 +00:00
try:
model.customUpdate(form.date.data, form.special.data, form.assistant.data, form.group_experiment.data)
2021-07-12 14:42:11 +00:00
self.on_model_change(form, model, False)
self.session.commit()
except Exception as ex:
flash(str(ex), "error")
self.session.rollback()
return False
else:
self.after_model_change(form, model, False)
return True
2021-07-01 11:12:43 +00:00
2021-07-30 00:03:44 +00:00
class ExperimentMarkView(SecureAdminModelView):
2021-07-01 14:38:37 +00:00
class StudentIdFilter(BaseSQLAFilter):
def apply(self, query, value, alias=None):
return query.filter(self.column == value)
def operation(self):
return "equals"
def validate(self, value):
if Student.query.get(value):
return True
else:
return False
2021-07-13 00:50:15 +00:00
class CreateForm(Form):
part_student = QuerySelectField(
"Part Student",
2021-07-29 21:18:24 +00:00
query_factory=partStudentQueryFactory,
2021-07-13 00:50:15 +00:00
validators=[DataRequired()],
allow_blank=True,
blank_text="-",
)
group_experiment = QuerySelectField(
"Group Experiment",
query_factory=groupExperimentQueryFactory,
validators=[DataRequired()],
allow_blank=True,
blank_text="-",
)
oral_mark = SelectField("Oral Mark", choices=markChoices, coerce=int)
protocol_mark = SelectField("Protocol Mark", choices=markChoices, coerce=int)
form = CreateForm
2021-07-30 21:42:20 +00:00
column_editable_list = ["oral_mark", "protocol_mark"]
form_args = {
"oral_mark": {"validators": [NumberRange(0, 15)]},
"protocol_mark": {"validators": [NumberRange(0, 15)]},
}
2021-07-01 14:38:37 +00:00
column_filters = [
StudentIdFilter(PartStudent.id, "Student / ID"),
2021-07-31 23:12:36 +00:00
"part_student.part.program",
"part_student.part",
2021-07-01 14:38:37 +00:00
"part_student.student",
2021-07-30 21:42:20 +00:00
"group_experiment.group",
2021-07-01 14:38:37 +00:00
"group_experiment.semester_experiment.semester",
"group_experiment.semester_experiment.experiment",
2021-07-13 00:50:15 +00:00
"edited_by_admin",
2021-08-15 23:15:19 +00:00
"assistant.user",
2021-07-13 00:50:15 +00:00
]
2021-07-30 21:42:20 +00:00
column_default_sort = [("oral_mark", False), ("protocol_mark", False)]
2021-07-13 00:50:15 +00:00
"""
# Deactivated for the experiments hisotry of a student.
2021-07-30 21:42:20 +00:00
def queryFilter(self):
2021-07-30 13:14:35 +00:00
return ExperimentMark.group_experiment.has(
GroupExperiment.semester_experiment.has(SemesterExperiment.semester == userActiveSemester())
2021-07-13 00:50:15 +00:00
)
"""
2021-07-13 00:50:15 +00:00
def checkForm(form):
2021-07-30 21:42:20 +00:00
if form.oral_mark and form.oral_mark.data == -1:
2021-07-13 00:50:15 +00:00
form.oral_mark.data = None
2021-07-30 21:42:20 +00:00
if form.protocol_mark and form.protocol_mark.data == -1:
2021-07-13 00:50:15 +00:00
form.protocol_mark.data = None
return form
def create_model(self, form):
form = ExperimentMarkView.checkForm(form)
return super().create_model(form)
def update_model(self, form, model):
form = ExperimentMarkView.checkForm(form)
return super().update_model(form, model)
def after_model_change(self, form, model, is_created):
if model.oral_mark or model.protocol_mark:
try:
model.edited_by_admin = True
self.session.commit()
except Exception as ex:
flash(str(ex), "error")
self.session.rollback()
2021-07-01 14:38:37 +00:00
2021-07-30 00:03:44 +00:00
class ProgramView(SecureAdminModelView):
2021-07-28 23:04:58 +00:00
can_view_details = True
2021-07-14 02:27:17 +00:00
column_list = ["label"]
2021-07-28 23:04:58 +00:00
form_excluded_columns = ["parts", "experiments", "groups"]
column_details_list = column_list + form_excluded_columns
2021-07-14 02:27:17 +00:00
2021-07-30 00:03:44 +00:00
adminSpace.add_view(StudentView(Student, db.session))
adminSpace.add_view(PartStudentView(PartStudent, db.session))
adminSpace.add_view(GroupView(Group, db.session))
adminSpace.add_view(GroupExperimentView(GroupExperiment, db.session))
adminSpace.add_view(AppointmentView(Appointment, db.session))
adminSpace.add_view(ExperimentMarkView(ExperimentMark, db.session))
adminSpace.add_view(ExperimentView(Experiment, db.session))
adminSpace.add_view(SemesterExperimentView(SemesterExperiment, db.session))
adminSpace.add_view(SemesterView(Semester, db.session))
2021-08-15 23:15:19 +00:00
adminSpace.add_view(PartView(Part, db.session))
adminSpace.add_view(AssistantView(Assistant, db.session))
2021-07-30 00:03:44 +00:00
adminSpace.add_view(UserView(User, db.session))
adminSpace.add_view(RoleView(Role, db.session))
2021-08-15 23:15:19 +00:00
adminSpace.add_view(ProgramView(Program, db.session))
initActiveSemesterMenuLinks(adminSpace)