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

Changed PartExperiment to SemesterExperiment

This commit is contained in:
Mo 2021-07-01 14:41:20 +02:00
parent 6e153d9c09
commit d30945461b
5 changed files with 63 additions and 64 deletions

File diff suppressed because one or more lines are too long

View file

@ -20,7 +20,7 @@ from advlabdb.models import (
Group, Group,
GroupExperiment, GroupExperiment,
Part, Part,
PartExperiment, SemesterExperiment,
PartStudent, PartStudent,
Role, Role,
Semester, Semester,
@ -106,7 +106,7 @@ class SemesterView(SecureModelView):
can_edit = False can_edit = False
column_list = ["label", "parts"] column_list = ["label", "parts"]
form_columns = ["semester_label", "year", "create_parts", "transfer_assistants"] form_columns = ["semester_label", "year", "transfer_parts", "transfer_semester_experiments", "transfer_assistants"]
semesterLabels = ["WS", "SS"] semesterLabels = ["WS", "SS"]
form_extra_fields = { form_extra_fields = {
@ -114,13 +114,14 @@ class SemesterView(SecureModelView):
"Semester", choices=list(zip(semesterLabels, semesterLabels)), validators=[DataRequired()] "Semester", choices=list(zip(semesterLabels, semesterLabels)), validators=[DataRequired()]
), ),
"year": TextField("Year", validators=[DataRequired()]), "year": TextField("Year", validators=[DataRequired()]),
"create_parts": BooleanField( "transfer_parts": BooleanField(
"Create parts:" "Transfer parts from your current semester",
+ ", ".join(getConfig("partLabels"))
+ " and transfer part experiments from your current active semester:",
default=True, default=True,
), ),
"transfer_assistants": BooleanField("Transfer Assistants from your current active semester:", default=False), "transfer_semester_experiments": BooleanField(
"Transfer Semester Experiemnts from your current semester", default=True
),
"transfer_assistants": BooleanField("Transfer Assistants from your current active semester", default=True),
} }
def create_model(self, form): def create_model(self, form):
@ -147,22 +148,22 @@ class SemesterView(SecureModelView):
) )
) )
oldSemesterParts = userActiveSemester().parts oldSemester = userActiveSemester()
setUserActiveSemester(model.id) setUserActiveSemester(model.id)
if form.create_parts.data: if form.transfer_parts.data:
model.createParts() model.transferParts(oldSemester)
if form.transfer_semester_experiments.data:
try: try:
for part in oldSemesterParts: for semesterExperiment in oldSemester.semester_experiments:
for partExperiment in part.part_experiments: newSemesterExperiment = SemesterExperiment(
newPartExperiment = PartExperiment( experiment=semesterExperiment.experiment, semester=userActiveSemester()
experiment=partExperiment.experiment, part=partFromLabelInUserActiveSemester(part.label)
) )
if form.transfer_assistants.data: if form.transfer_assistants.data:
newPartExperiment.assistants = partExperiment.assistants newSemesterExperiment.assistants = semesterExperiment.assistants
self.session.add(newPartExperiment) self.session.add(newSemesterExperiment)
self.session.commit() self.session.commit()
except Exception as ex: except Exception as ex:
flash(ex, "error") flash(ex, "error")
@ -172,12 +173,9 @@ class SemesterView(SecureModelView):
class PartView(SecureModelView): class PartView(SecureModelView):
can_view_details = True can_view_details = True
column_details_list = ["label", "semester", "part_experiments", "part_students", "groups"] column_details_list = ["label", "semester", "part_students", "groups"]
form_columns = ["label", "semester"] form_columns = ["label", "semester"]
partLabels = getConfig("partLabels")
form_choices = {"label": list(zip(partLabels, partLabels))}
def get_query(self): def get_query(self):
return super().get_query().filter(Part.id.in_([part.id for part in userActiveSemester().parts])) return super().get_query().filter(Part.id.in_([part.id for part in userActiveSemester().parts]))
@ -337,26 +335,23 @@ class ExperimentView(SecureModelView):
column_list = ["number", "name", "deprecated"] column_list = ["number", "name", "deprecated"]
class PartExperimentView(SecureModelView): class SemesterExperimentView(SecureModelView):
column_list = ["experiment", "part", "assistants"] column_list = ["experiment", "semester", "assistants"]
partLabels = getConfig("partLabels")
column_filters = ["part"]
def get_query(self): def get_query(self):
return super().get_query().filter(PartExperiment.part_id.in_([part.id for part in userActiveSemester().parts])) return super().get_query().filter(SemesterExperiment.semester == userActiveSemester())
def get_count_query(self): def get_count_query(self):
return ( return (
self.session.query(func.count("*")) self.session.query(func.count("*"))
.select_from(self.model) .select_from(self.model)
.filter(PartExperiment.part_id.in_([part.id for part in userActiveSemester().parts])) .filter(SemesterExperiment.semester == userActiveSemester())
) )
class AssistantView(SecureModelView): class AssistantView(SecureModelView):
can_view_details = True can_view_details = True
column_list = ["first_name", "last_name", "email", "user", "part_experiments"] column_list = ["first_name", "last_name", "email", "user", "semester_experiments"]
column_details_list = column_list + [ column_details_list = column_list + [
"phone_number", "phone_number",
"mobile_phone_number", "mobile_phone_number",
@ -371,10 +366,8 @@ class AssistantView(SecureModelView):
class GroupExperimentView(SecureModelView): class GroupExperimentView(SecureModelView):
class CreateForm(Form): class CreateForm(Form):
def partExperimentQueryFactory(): def semesterExperimentQueryFactory():
return PartExperiment.query.filter( return SemesterExperiment.query.filter(SemesterExperiment.semester == userActiveSemester())
PartExperiment.part_id.in_([part.id for part in userActiveSemester().parts])
)
def assistantQueryFactory(): def assistantQueryFactory():
return Assistant.query.filter( return Assistant.query.filter(
@ -384,9 +377,9 @@ class GroupExperimentView(SecureModelView):
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="-"
) )
part_experiment = QuerySelectField( semester_experiment = QuerySelectField(
"Part Experiment", "Semester Experiment",
query_factory=partExperimentQueryFactory, query_factory=semesterExperimentQueryFactory,
validators=[DataRequired()], validators=[DataRequired()],
allow_blank=True, allow_blank=True,
blank_text="-", blank_text="-",
@ -414,8 +407,8 @@ class GroupExperimentView(SecureModelView):
form = CreateForm form = CreateForm
column_list = ["group", "part_experiment", "appointments", "experiment_marks"] column_list = ["group", "semester_experiment", "appointments", "experiment_marks"]
column_filters = ["group", "part_experiment.experiment", "appointments"] column_filters = ["group", "semester_experiment.experiment", "appointments"]
def get_query(self): def get_query(self):
return ( return (
@ -455,7 +448,7 @@ admin.add_view(PartStudentView(PartStudent, db.session))
admin.add_view(GroupView(Group, db.session)) admin.add_view(GroupView(Group, db.session))
admin.add_view(GroupExperimentView(GroupExperiment, db.session)) admin.add_view(GroupExperimentView(GroupExperiment, db.session))
admin.add_view(ExperimentView(Experiment, db.session)) admin.add_view(ExperimentView(Experiment, db.session))
admin.add_view(PartExperimentView(PartExperiment, db.session)) admin.add_view(SemesterExperimentView(SemesterExperiment, db.session))
admin.add_view(AssistantView(Assistant, db.session)) admin.add_view(AssistantView(Assistant, db.session))
admin.add_view(AppointmentView(Appointment, db.session)) admin.add_view(AppointmentView(Appointment, db.session))
admin.add_view(PartView(Part, db.session)) admin.add_view(PartView(Part, db.session))
@ -465,7 +458,7 @@ admin.add_view(UserView(User, db.session))
admin.add_view(RoleView(Role, db.session)) admin.add_view(RoleView(Role, db.session))
with app.app_context(): with app.app_context():
semesters = Semester.query.all()[::-1] semesters = Semester.query.order_by(Semester.id)
for semester in semesters: for semester in semesters:
admin.add_link( admin.add_link(
MenuLink( MenuLink(

View file

@ -58,7 +58,7 @@ class Group(db.Model):
class GroupExperiment(db.Model): class GroupExperiment(db.Model):
# An experiment specified to a group # An experiment specified to a group
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
part_experiment_id = db.Column(db.Integer, db.ForeignKey("part_experiment.id"), nullable=False) semester_experiment_id = db.Column(db.Integer, db.ForeignKey("semester_experiment.id"), nullable=False)
group_id = db.Column(db.Integer, db.ForeignKey("group.id"), nullable=False) group_id = db.Column(db.Integer, db.ForeignKey("group.id"), nullable=False)
appointments = db.relationship("Appointment", backref="group_experiment", lazy=True) appointments = db.relationship("Appointment", backref="group_experiment", lazy=True)
experiment_marks = db.relationship("ExperimentMark", backref="group_experiment", lazy=True) experiment_marks = db.relationship("ExperimentMark", backref="group_experiment", lazy=True)
@ -78,29 +78,29 @@ class Experiment(db.Model):
oral_weighting = db.Column(db.Float, nullable=False) oral_weighting = db.Column(db.Float, nullable=False)
protocol_weighting = db.Column(db.Float, nullable=False) protocol_weighting = db.Column(db.Float, nullable=False)
final_weighting = db.Column(db.Float, nullable=False) final_weighting = db.Column(db.Float, nullable=False)
part_experiments = db.relationship("PartExperiment", backref="experiment", lazy=True) semester_experiments = db.relationship("SemesterExperiment", backref="experiment", lazy=True)
def __repr__(self): def __repr__(self):
return f"<Exp {self.number}>" return f"<Exp {self.number}>"
# Helper table for the many to many relationship between Assistant and PartExperiment # Helper table for the many to many relationship between Assistant and SemesterExperiment
experiment_assistant = db.Table( experiment_assistant = db.Table(
"experiment_assistant", "experiment_assistant",
db.Column("part_experiment_id", db.Integer, db.ForeignKey("part_experiment.id"), primary_key=True), db.Column("semester_experiment_id", db.Integer, db.ForeignKey("semester_experiment.id"), primary_key=True),
db.Column("assistant_id", db.Integer, db.ForeignKey("assistant.id"), primary_key=True), db.Column("assistant_id", db.Integer, db.ForeignKey("assistant.id"), primary_key=True),
) )
class PartExperiment(db.Model): class SemesterExperiment(db.Model):
# An experiment in a specific part # An experiment in a specific part
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
experiment_id = db.Column(db.Integer, db.ForeignKey("experiment.id"), nullable=False) experiment_id = db.Column(db.Integer, db.ForeignKey("experiment.id"), nullable=False)
part_id = db.Column(db.Integer, db.ForeignKey("part.id"), nullable=False) semester_id = db.Column(db.Integer, db.ForeignKey("semester.id"), nullable=False)
assistants = db.relationship( assistants = db.relationship(
"Assistant", secondary=experiment_assistant, lazy=True, backref=db.backref("part_experiments", lazy=True) "Assistant", secondary=experiment_assistant, lazy=True, backref=db.backref("semester_experiments", lazy=True)
) )
group_experiments = db.relationship("GroupExperiment", backref="part_experiment", lazy=True) group_experiments = db.relationship("GroupExperiment", backref="semester_experiment", lazy=True)
class Assistant(db.Model): class Assistant(db.Model):
@ -132,7 +132,6 @@ class Part(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
label = db.Column(db.String(100), nullable=False) # A/1, A/2, B/1, B/2 label = db.Column(db.String(100), nullable=False) # A/1, A/2, B/1, B/2
semester_id = db.Column(db.Integer, db.ForeignKey("semester.id"), nullable=False) semester_id = db.Column(db.Integer, db.ForeignKey("semester.id"), nullable=False)
part_experiments = db.relationship("PartExperiment", backref="part", lazy=True)
part_students = db.relationship("PartStudent", backref="part", lazy=True) part_students = db.relationship("PartStudent", backref="part", lazy=True)
groups = db.relationship("Group", backref="part", lazy=True) groups = db.relationship("Group", backref="part", lazy=True)
@ -144,14 +143,21 @@ class Semester(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
label = db.Column(db.String(100), nullable=False, unique=True) # WS2122 for example label = db.Column(db.String(100), nullable=False, unique=True) # WS2122 for example
parts = db.relationship("Part", backref="semester", lazy=True) parts = db.relationship("Part", backref="semester", lazy=True)
semester_experiments = db.relationship("SemesterExperiment", backref="semester", lazy=True)
def __repr__(self): def __repr__(self):
return f"<{self.label}>" return f"<{self.label}>"
def createParts(self): def transferParts(self, oldSemester):
try: try:
for partLabel in getConfig("partLabels"): if oldSemester:
partLabels = [part.label for part in oldSemester.parts]
else:
partLabels = getConfig("defaultPartLabels")
for partLabel in partLabels:
db.session.add(Part(label=partLabel, semester=self)) db.session.add(Part(label=partLabel, semester=self))
db.session.commit() db.session.commit()
except Exception as ex: except Exception as ex:
flash(ex, "error") flash(ex, "error")

View file

@ -1,5 +1,5 @@
{ {
"partLabels": [ "defaultPartLabels": [
"A/1", "A/1",
"A/2", "A/2",
"A/m", "A/m",

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.createParts() sem1.transferParts(None)
sem2.createParts() sem2.transferParts(None)
parta1 = sem2.parts[0] parta1 = sem2.parts[0]
partb2 = sem2.parts[1] partb2 = sem2.parts[1]
@ -70,14 +70,14 @@ with app.app_context():
db.session.add(ex1) db.session.add(ex1)
db.session.add(ex2) db.session.add(ex2)
px1 = PartExperiment(experiment=ex1, part=parta1) sx1 = SemesterExperiment(experiment=ex1, semester=sem2)
px2 = PartExperiment(experiment=ex2, part=partb2) sx2 = SemesterExperiment(experiment=ex2, semester=sem2)
db.session.add(px1) db.session.add(sx1)
db.session.add(px2) db.session.add(sx2)
gx1 = GroupExperiment(part_experiment=px1, group=g1) gx1 = GroupExperiment(semester_experiment=sx1, group=g1)
gx2 = GroupExperiment(part_experiment=px2, group=g2) gx2 = GroupExperiment(semester_experiment=sx2, group=g2)
db.session.add(gx1) db.session.add(gx1)
db.session.add(gx2) db.session.add(gx2)
@ -100,8 +100,8 @@ with app.app_context():
) )
as2 = Assistant(first_name="As2", last_name="l", email="test2@test.com", user=us2) as2 = Assistant(first_name="As2", last_name="l", email="test2@test.com", user=us2)
as1.part_experiments.append(px1) as1.semester_experiments.append(sx1)
as2.part_experiments.append(px2) as2.semester_experiments.append(sx2)
db.session.add(as1) db.session.add(as1)
db.session.add(as2) db.session.add(as2)