mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-11-08 21:21:06 +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_security import current_user, hash_password
|
||||
from sqlalchemy import func
|
||||
from wtforms import BooleanField, SelectField, TextField
|
||||
from wtforms import BooleanField, SelectField, TextField, RadioField
|
||||
from wtforms.validators import DataRequired, Email
|
||||
|
||||
from advlabdb import admin, app, db, user_datastore
|
||||
|
@ -27,7 +27,7 @@ from advlabdb.models import (
|
|||
from advlabdb.utils import (
|
||||
partFromLabelInUserActiveSemester,
|
||||
randomPassword,
|
||||
setActiveSemester,
|
||||
setUserActiveSemester,
|
||||
userActiveSemester,
|
||||
)
|
||||
|
||||
|
@ -100,15 +100,21 @@ class SemesterModelView(SecureModelView):
|
|||
can_edit = False
|
||||
|
||||
column_list = ["label", "parts"]
|
||||
form_columns = ["semester_label", "year", "create_parts"]
|
||||
form_columns = ["semester_label", "year", "create_parts", "transfer_assistants"]
|
||||
|
||||
semesterLabels = ["WS", "SS"]
|
||||
form_extra_fields = {
|
||||
"semester_label": SelectField(
|
||||
"semester_label": RadioField(
|
||||
"Semester", choices=list(zip(semesterLabels, semesterLabels)), 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):
|
||||
|
@ -125,18 +131,35 @@ class SemesterModelView(SecureModelView):
|
|||
return model
|
||||
|
||||
def after_model_change(self, form, model, is_created):
|
||||
admin.add_link(
|
||||
MenuLink(
|
||||
name=model.label,
|
||||
url=url_for("set_semester") + "?semester_id=" + str(model.id),
|
||||
category="Active semester",
|
||||
)
|
||||
)
|
||||
|
||||
oldSemesterParts = userActiveSemester().parts
|
||||
|
||||
setUserActiveSemester(model.id)
|
||||
|
||||
if form.create_parts.data:
|
||||
model.createParts()
|
||||
|
||||
if is_created:
|
||||
admin.add_link(
|
||||
MenuLink(
|
||||
name=model.label,
|
||||
url=url_for("set_semester") + "?semester_id=" + str(model.id),
|
||||
category="Active semester",
|
||||
)
|
||||
)
|
||||
setActiveSemester(model.id)
|
||||
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):
|
||||
|
|
|
@ -3,7 +3,7 @@ from flask_security import auth_required, current_user
|
|||
|
||||
from advlabdb import app
|
||||
from advlabdb.models import Semester
|
||||
from advlabdb.utils import setActiveSemester, titleToTemplate, userActiveSemester
|
||||
from advlabdb.utils import setUserActiveSemester, titleToTemplate, userActiveSemester
|
||||
|
||||
|
||||
@app.context_processor
|
||||
|
@ -73,7 +73,7 @@ def index():
|
|||
@app.route("/set_semester")
|
||||
@auth_required()
|
||||
def set_semester():
|
||||
setActiveSemester(request.args.get("semester_id"))
|
||||
setUserActiveSemester(request.args.get("semester_id"))
|
||||
ref = request.referrer
|
||||
red = ref if ref else url_for("index")
|
||||
return redirect(red)
|
||||
|
|
|
@ -101,7 +101,7 @@ def partFromLabelInUserActiveSemester(partLabel):
|
|||
return part
|
||||
|
||||
|
||||
def setActiveSemester(semesterId):
|
||||
def setUserActiveSemester(semesterId):
|
||||
try:
|
||||
current_user.active_semester_id = Semester.query.get(int(semesterId)).id
|
||||
except Exception as err:
|
||||
|
|
Loading…
Reference in a new issue