1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-12-20 23:41:20 +00:00

Added create_model to GroupExperiment

This commit is contained in:
Mo 2021-07-01 19:43:59 +02:00
parent 515c389610
commit 5212eb8ee6
4 changed files with 57 additions and 8 deletions

View file

@ -52,6 +52,7 @@ This URL leads to the home page where you can login with this testing admin acco
# To-Do: # To-Do:
- Rest of admin model views - Rest of admin model views
- Assistants space - Assistants space
- Deactivate assistants account after a while if not assigned to experiments
- Homepage text changeable - Homepage text changeable
- Email integration? - Email integration?
- 2FA? - 2FA?

View file

@ -160,7 +160,7 @@ class SemesterView(SecureModelView):
setUserActiveSemester(model.id) setUserActiveSemester(model.id)
if form.transfer_parts.data: if form.transfer_parts.data:
model.transferParts(oldSemester) model.transferPartsFrom(oldSemester)
if form.transfer_semester_experiments.data: if form.transfer_semester_experiments.data:
try: try:
@ -403,7 +403,7 @@ class GroupExperimentView(SecureModelView):
assistantBlankText = "Auto assign if experiment has only one assistant" assistantBlankText = "Auto assign if experiment has only one assistant"
appointment1 = DateField("Appointment-1") appointment1_date = DateField("Appointment-1 Date", validators=[Optional()])
appointment1_special = BooleanField("Appointment-1 special", default=False) appointment1_special = BooleanField("Appointment-1 special", default=False)
appointment1_assistant = QuerySelectField( appointment1_assistant = QuerySelectField(
"Appointment-1 Assistant", "Appointment-1 Assistant",
@ -412,7 +412,7 @@ class GroupExperimentView(SecureModelView):
blank_text=assistantBlankText, blank_text=assistantBlankText,
) )
appointment2 = DateField("Appointment-2") appointment2_date = DateField("Appointment-2 Date", validators=[Optional()])
appointment2_special = BooleanField("Appointment-2 special", default=False) appointment2_special = BooleanField("Appointment-2 special", default=False)
appointment2_assistant = QuerySelectField( appointment2_assistant = QuerySelectField(
"Appointment-2 Assistant", "Appointment-2 Assistant",
@ -427,6 +427,50 @@ class GroupExperimentView(SecureModelView):
column_list = ["group", "semester_experiment", "appointments", "experiment_marks"] column_list = ["group", "semester_experiment", "appointments", "experiment_marks"]
column_filters = ["group", "semester_experiment.experiment", "appointments"] column_filters = ["group", "semester_experiment.experiment", "appointments"]
def create_model(self, form):
try:
model = GroupExperiment(semester_experiment=form.semester_experiment.data, group=form.group.data)
self.session.add(model)
for appointmentDate, special, assistant in zip(
[form.appointment1_date.data, form.appointment2_date.data],
[form.appointment1_special.data, form.appointment2_special.data],
[form.appointment1_assistant.data, form.appointment2_assistant.data],
):
if appointmentDate:
if assistant:
if assistant not in form.semester_experiment.data.assistants:
raise Exception(f"{assistant} not responsible for {form.semester_experiment.data}!")
else:
if len(form.semester_experiment.data.assistants) != 1:
raise Exception(
f"Experiment {form.semester_experiment.data} has more than one assistant. You have to assign one of these assistants: {form.semester_experiment.data.assistants}"
)
assistant = form.semester_experiment.data.assistants[0]
appointment = Appointment(
date=appointmentDate,
special=special,
group_experiment=model,
assistant=assistant,
)
self.session.add(appointment)
self.on_model_change(form, model, True)
self.session.commit()
except Exception as ex:
flash(ex, "error")
self.session.rollback()
else:
flash(f"Appointments {model.appointments} added.", "success")
self.after_model_change(form, model, True)
return model
def get_query(self): def get_query(self):
return ( return (
super() super()
@ -479,6 +523,7 @@ class ExperimentMarkView(SecureModelView):
"part_student.student", "part_student.student",
"group_experiment.semester_experiment.semester", "group_experiment.semester_experiment.semester",
"group_experiment.semester_experiment.experiment", "group_experiment.semester_experiment.experiment",
"assistant",
] ]

View file

@ -7,7 +7,7 @@ https://flask-sqlalchemy.palletsprojects.com/en/2.x/models/
""" """
# Imports # Imports
from flask import flash from flask import flash, has_request_context
from flask_security.models.fsqla_v2 import FsRoleMixin, FsUserMixin from flask_security.models.fsqla_v2 import FsRoleMixin, FsUserMixin
# Importing the database instance # Importing the database instance
@ -148,7 +148,7 @@ class Semester(db.Model):
def __repr__(self): def __repr__(self):
return f"<{self.label}>" return f"<{self.label}>"
def transferParts(self, oldSemester): def transferPartsFrom(self, oldSemester):
try: try:
if oldSemester: if oldSemester:
partLabels = [part.label for part in oldSemester.parts] partLabels = [part.label for part in oldSemester.parts]
@ -160,7 +160,10 @@ class Semester(db.Model):
db.session.commit() db.session.commit()
except Exception as ex: except Exception as ex:
flash(ex, "error") if has_request_context():
flash(ex, "error")
else:
print(ex)
db.session.rollback() db.session.rollback()

View file

@ -15,8 +15,8 @@ with app.app_context():
db.session.add(sem1) db.session.add(sem1)
db.session.add(sem2) db.session.add(sem2)
sem1.transferParts(None) sem1.transferPartsFrom(None)
sem2.transferParts(None) sem2.transferPartsFrom(None)
parta1 = sem2.parts[0] parta1 = sem2.parts[0]
partb2 = sem2.parts[1] partb2 = sem2.parts[1]