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

Done AppointmentView

This commit is contained in:
Mo 2021-07-12 16:42:11 +02:00
parent b67ba46768
commit 5b62f9e78e
2 changed files with 116 additions and 28 deletions

View file

@ -188,7 +188,8 @@ class PartView(SecureModelView):
column_details_list = ["label", "semester", "part_students", "groups"] column_details_list = ["label", "semester", "part_students", "groups"]
form_columns = ["label", "semester"] form_columns = ["label", "semester"]
queryFilter = lambda self: Part.id.in_([part.id for part in userActiveSemester().parts]) def queryFilter(self):
return Part.id.in_([part.id for part in userActiveSemester().parts])
class StudentView(SecureModelView): class StudentView(SecureModelView):
@ -217,14 +218,18 @@ class StudentView(SecureModelView):
] ]
partQueryFactory = lambda: Part.query.filter(Part.id.in_([part.id for part in userActiveSemester().parts])) def partQueryFactory():
return Part.query.filter(Part.id.in_([part.id for part in userActiveSemester().parts]))
groupQueryFactory = lambda: Group.query.filter(Group.part_id.in_([part.id for part in userActiveSemester().parts]))
def groupQueryFactory():
return Group.query.filter(Group.part_id.in_([part.id for part in userActiveSemester().parts]))
class PartStudentView(SecureModelView): class PartStudentView(SecureModelView):
class CreateForm(Form): class CreateForm(Form):
studentQueryFactory = lambda: Student.query def studentQueryFactory():
return Student.query
student = QuerySelectField( student = QuerySelectField(
"Student", query_factory=studentQueryFactory, validators=[DataRequired()], allow_blank=True, blank_text="-" "Student", query_factory=studentQueryFactory, validators=[DataRequired()], allow_blank=True, blank_text="-"
@ -243,10 +248,11 @@ class PartStudentView(SecureModelView):
column_filters = ["part", "student", "group"] column_filters = ["part", "student", "group"]
queryFilter = lambda self: PartStudent.part_id.in_([part.id for part in userActiveSemester().parts])
partGroupPartMismatchException = "Student's part and group's part do not match!" partGroupPartMismatchException = "Student's part and group's part do not match!"
def queryFilter(self):
return PartStudent.part_id.in_([part.id for part in userActiveSemester().parts])
def create_form(self, obj=None): def create_form(self, obj=None):
form = self.CreateForm form = self.CreateForm
return form(get_form_data(), obj=obj) return form(get_form_data(), obj=obj)
@ -264,9 +270,8 @@ class PartStudentView(SecureModelView):
class GroupView(SecureModelView): class GroupView(SecureModelView):
class CreateForm(Form): class CreateForm(Form):
partStudentsQueryFactory = lambda: PartStudent.query.filter( def partStudentsQueryFactory():
PartStudent.part_id.in_([part.id for part in userActiveSemester().parts]) return PartStudent.query.filter(PartStudent.part_id.in_([part.id for part in userActiveSemester().parts]))
)
part = QuerySelectField( part = QuerySelectField(
"Part", query_factory=partQueryFactory, validators=[DataRequired()], allow_blank=True, blank_text="-" "Part", query_factory=partQueryFactory, validators=[DataRequired()], allow_blank=True, blank_text="-"
@ -281,10 +286,11 @@ class GroupView(SecureModelView):
column_list = ["number", "part", "part_students", "group_experiments"] column_list = ["number", "part", "part_students", "group_experiments"]
column_filters = ["number", "part"] column_filters = ["number", "part"]
queryFilter = lambda self: Group.part_id.in_([part.id for part in userActiveSemester().parts])
partStudentPartPartMismatchException = "Group's part and student's part do not match!" partStudentPartPartMismatchException = "Group's part and student's part do not match!"
def queryFilter(self):
return Group.part_id.in_([part.id for part in userActiveSemester().parts])
def create_model(self, form): def create_model(self, form):
try: try:
model = Group.customInit(form.part.data, form.part_students.data) model = Group.customInit(form.part.data, form.part_students.data)
@ -338,7 +344,8 @@ class ExperimentView(SecureModelView):
class SemesterExperimentView(SecureModelView): class SemesterExperimentView(SecureModelView):
column_list = ["experiment", "semester", "assistants"] column_list = ["experiment", "semester", "assistants"]
queryFilter = lambda self: SemesterExperiment.semester == userActiveSemester() def queryFilter(self):
return SemesterExperiment.semester == userActiveSemester()
class AssistantView(SecureModelView): class AssistantView(SecureModelView):
@ -356,15 +363,17 @@ class AssistantView(SecureModelView):
form_excluded_columns = ["experiment_marks"] form_excluded_columns = ["experiment_marks"]
def assistantQueryFactory():
return Assistant.query.filter(Assistant.user_id.in_([user.id for user in User.query.filter(User.active == True)]))
assistantBlankText = "Auto assign if experiment has only one assistant"
class GroupExperimentView(SecureModelView): class GroupExperimentView(SecureModelView):
class CreateForm(Form): class CreateForm(Form):
semesterExperimentQueryFactory = lambda: SemesterExperiment.query.filter( def semesterExperimentQueryFactory():
SemesterExperiment.semester == userActiveSemester() return SemesterExperiment.query.filter(SemesterExperiment.semester == userActiveSemester())
)
assistantQueryFactory = lambda: Assistant.query.filter(
Assistant.user_id.in_([user.id for user in User.query.filter(User.active == True)])
)
group = QuerySelectField( group = QuerySelectField(
"Group", query_factory=groupQueryFactory, validators=[DataRequired()], allow_blank=True, blank_text="-" "Group", query_factory=groupQueryFactory, validators=[DataRequired()], allow_blank=True, blank_text="-"
@ -377,10 +386,8 @@ class GroupExperimentView(SecureModelView):
blank_text="-", blank_text="-",
) )
assistantBlankText = "Auto assign if experiment has only one assistant"
appointment1_date = DateField("Appointment-1 Date", validators=[Optional()]) 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",
query_factory=assistantQueryFactory, query_factory=assistantQueryFactory,
@ -389,7 +396,7 @@ class GroupExperimentView(SecureModelView):
) )
appointment2_date = DateField("Appointment-2 Date", validators=[Optional()]) 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",
query_factory=assistantQueryFactory, query_factory=assistantQueryFactory,
@ -403,9 +410,10 @@ 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"]
queryFilter = lambda self: GroupExperiment.group_id.in_( def queryFilter(self):
[g.id for g in Group.query.filter(Group.part_id.in_([part.id for part in userActiveSemester().parts]))] return GroupExperiment.group_id.in_(
) [g.id for g in Group.query.filter(Group.part_id.in_([part.id for part in userActiveSemester().parts]))]
)
def create_model(self, form): def create_model(self, form):
try: try:
@ -437,8 +445,75 @@ class GroupExperimentView(SecureModelView):
return model return model
def groupExperimentQueryFactory():
return GroupExperiment.query.filter(
GroupExperiment.semester_experiment_id.in_(
[
semesterExperiment.id
for semesterExperiment in SemesterExperiment.query.filter(
SemesterExperiment.semester == userActiveSemester()
)
]
)
)
class AppointmentView(SecureModelView): class AppointmentView(SecureModelView):
column_list = ["date", "special", "group_experiment", "assistant"] class CreateForm(Form):
group_experiment = QuerySelectField(
"Group Experiment", query_factory=groupExperimentQueryFactory, validators=[DataRequired()]
)
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.group.part", "assistant"]
def queryFilter(self):
return Appointment.group_experiment_id.in_(
groupExperiment.id for groupExperiment in groupExperimentQueryFactory()
)
def create_model(self, form):
try:
model = Appointment.customInit(
form.date.data, form.special.data, form.assistant.data, form.group_experiment.data
)
self.session.add(model)
self.on_model_change(form, model, True)
self.session.commit()
except Exception as ex:
flash(str(ex), "error")
self.session.rollback()
else:
self.after_model_change(form, model, True)
return model
def update_model(self, form, model):
try:
model.customUpdate(form.date.data, form.special.data, form.assistant.data, form.group_experiment.data)
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
class ExperimentMarkView(SecureModelView): class ExperimentMarkView(SecureModelView):

View file

@ -191,7 +191,7 @@ class Appointment(db.Model):
group_experiment_id = db.Column(db.Integer, db.ForeignKey("group_experiment.id"), nullable=False) group_experiment_id = db.Column(db.Integer, db.ForeignKey("group_experiment.id"), nullable=False)
assistant_id = db.Column(db.Integer, db.ForeignKey("assistant.id"), nullable=False) assistant_id = db.Column(db.Integer, db.ForeignKey("assistant.id"), nullable=False)
def customInit(date, special, assistant, groupExperiment): def checkAndGetAssistant(assistant, groupExperiment):
semesterExperiment = groupExperiment.semester_experiment semesterExperiment = groupExperiment.semester_experiment
semesterExperimentAssistants = semesterExperiment.assistants semesterExperimentAssistants = semesterExperiment.assistants
@ -209,6 +209,11 @@ class Appointment(db.Model):
f"Experiment {semesterExperiment} has more than one assistant. You have to assign one of these assistants: {semesterExperimentAssistants}" f"Experiment {semesterExperiment} has more than one assistant. You have to assign one of these assistants: {semesterExperimentAssistants}"
) )
return assistant
def customInit(date, special, assistant, groupExperiment):
assistant = Appointment.checkAndGetAssistant(assistant, groupExperiment)
return Appointment( return Appointment(
date=date, date=date,
special=special, special=special,
@ -216,6 +221,14 @@ class Appointment(db.Model):
assistant=assistant, assistant=assistant,
) )
def customUpdate(self, date, special, assistant, groupExperiment):
assistant = Appointment.checkAndGetAssistant(assistant, groupExperiment)
self.date = date
self.special = special
self.assistant = assistant
self.group_experiment = groupExperiment
def repr(self): def repr(self):
return f"{self.date} {self.group_experiment.repr()}" return f"{self.date} {self.group_experiment.repr()}"