mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-12-20 23:41:20 +00:00
Transfer part experiments and assistants for new semesters
This commit is contained in:
parent
27c8d3d952
commit
f840bd4b20
3 changed files with 40 additions and 17 deletions
|
@ -3,7 +3,7 @@ from flask_admin.contrib.sqla.filters import BaseSQLAFilter
|
||||||
from flask_admin.menu import MenuLink
|
from flask_admin.menu import MenuLink
|
||||||
from flask_security import current_user, hash_password
|
from flask_security import current_user, hash_password
|
||||||
from sqlalchemy import func
|
from sqlalchemy import func
|
||||||
from wtforms import BooleanField, SelectField, TextField
|
from wtforms import BooleanField, SelectField, TextField, RadioField
|
||||||
from wtforms.validators import DataRequired, Email
|
from wtforms.validators import DataRequired, Email
|
||||||
|
|
||||||
from advlabdb import admin, app, db, user_datastore
|
from advlabdb import admin, app, db, user_datastore
|
||||||
|
@ -27,7 +27,7 @@ from advlabdb.models import (
|
||||||
from advlabdb.utils import (
|
from advlabdb.utils import (
|
||||||
partFromLabelInUserActiveSemester,
|
partFromLabelInUserActiveSemester,
|
||||||
randomPassword,
|
randomPassword,
|
||||||
setActiveSemester,
|
setUserActiveSemester,
|
||||||
userActiveSemester,
|
userActiveSemester,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -100,15 +100,21 @@ class SemesterModelView(SecureModelView):
|
||||||
can_edit = False
|
can_edit = False
|
||||||
|
|
||||||
column_list = ["label", "parts"]
|
column_list = ["label", "parts"]
|
||||||
form_columns = ["semester_label", "year", "create_parts"]
|
form_columns = ["semester_label", "year", "create_parts", "transfer_assistants"]
|
||||||
|
|
||||||
semesterLabels = ["WS", "SS"]
|
semesterLabels = ["WS", "SS"]
|
||||||
form_extra_fields = {
|
form_extra_fields = {
|
||||||
"semester_label": SelectField(
|
"semester_label": RadioField(
|
||||||
"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("Create parts:" + ", ".join(getConfig("partLabels")) + ".", default=True),
|
"create_parts": BooleanField(
|
||||||
|
"Create parts:"
|
||||||
|
+ ", ".join(getConfig("partLabels"))
|
||||||
|
+ " and transfer part experiments from your current active semester:",
|
||||||
|
default=True,
|
||||||
|
),
|
||||||
|
"transfer_assistants": BooleanField("Transfer Assistants from your current active semester:", default=False),
|
||||||
}
|
}
|
||||||
|
|
||||||
def create_model(self, form):
|
def create_model(self, form):
|
||||||
|
@ -125,10 +131,6 @@ class SemesterModelView(SecureModelView):
|
||||||
return model
|
return model
|
||||||
|
|
||||||
def after_model_change(self, form, model, is_created):
|
def after_model_change(self, form, model, is_created):
|
||||||
if form.create_parts.data:
|
|
||||||
model.createParts()
|
|
||||||
|
|
||||||
if is_created:
|
|
||||||
admin.add_link(
|
admin.add_link(
|
||||||
MenuLink(
|
MenuLink(
|
||||||
name=model.label,
|
name=model.label,
|
||||||
|
@ -136,7 +138,28 @@ class SemesterModelView(SecureModelView):
|
||||||
category="Active semester",
|
category="Active semester",
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
setActiveSemester(model.id)
|
|
||||||
|
oldSemesterParts = userActiveSemester().parts
|
||||||
|
|
||||||
|
setUserActiveSemester(model.id)
|
||||||
|
|
||||||
|
if form.create_parts.data:
|
||||||
|
model.createParts()
|
||||||
|
|
||||||
|
try:
|
||||||
|
for part in oldSemesterParts:
|
||||||
|
for partExperiment in part.part_experiments:
|
||||||
|
newPartExperiment = PartExperiment(
|
||||||
|
experiment=partExperiment.experiment, part=partFromLabelInUserActiveSemester(part.label)
|
||||||
|
)
|
||||||
|
if form.transfer_assistants.data:
|
||||||
|
newPartExperiment.assistants = partExperiment.assistants
|
||||||
|
self.session.add(newPartExperiment)
|
||||||
|
self.session.commit()
|
||||||
|
except Exception as ex:
|
||||||
|
flash(ex, "error")
|
||||||
|
|
||||||
|
self.session.rollback()
|
||||||
|
|
||||||
|
|
||||||
class PartModelView(SecureModelView):
|
class PartModelView(SecureModelView):
|
||||||
|
|
|
@ -3,7 +3,7 @@ from flask_security import auth_required, current_user
|
||||||
|
|
||||||
from advlabdb import app
|
from advlabdb import app
|
||||||
from advlabdb.models import Semester
|
from advlabdb.models import Semester
|
||||||
from advlabdb.utils import setActiveSemester, titleToTemplate, userActiveSemester
|
from advlabdb.utils import setUserActiveSemester, titleToTemplate, userActiveSemester
|
||||||
|
|
||||||
|
|
||||||
@app.context_processor
|
@app.context_processor
|
||||||
|
@ -73,7 +73,7 @@ def index():
|
||||||
@app.route("/set_semester")
|
@app.route("/set_semester")
|
||||||
@auth_required()
|
@auth_required()
|
||||||
def set_semester():
|
def set_semester():
|
||||||
setActiveSemester(request.args.get("semester_id"))
|
setUserActiveSemester(request.args.get("semester_id"))
|
||||||
ref = request.referrer
|
ref = request.referrer
|
||||||
red = ref if ref else url_for("index")
|
red = ref if ref else url_for("index")
|
||||||
return redirect(red)
|
return redirect(red)
|
||||||
|
|
|
@ -101,7 +101,7 @@ def partFromLabelInUserActiveSemester(partLabel):
|
||||||
return part
|
return part
|
||||||
|
|
||||||
|
|
||||||
def setActiveSemester(semesterId):
|
def setUserActiveSemester(semesterId):
|
||||||
try:
|
try:
|
||||||
current_user.active_semester_id = Semester.query.get(int(semesterId)).id
|
current_user.active_semester_id = Semester.query.get(int(semesterId)).id
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
|
|
Loading…
Reference in a new issue