mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2025-04-10 20:48:36 +00:00
Updated SemesterModelView and added session exceptions
This commit is contained in:
parent
699192201f
commit
c5a8ef790b
6 changed files with 86 additions and 36 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
advLab.db
|
||||
*.pyc
|
||||
.vscode
|
||||
|
|
|
@ -53,13 +53,20 @@ class UserModelView(SecureModelView):
|
|||
if "admin" in roles:
|
||||
flash("You have registered a new admin!", "danger")
|
||||
|
||||
model = user_datastore.create_user(email=email, password=passwordHash, roles=roles)
|
||||
db.session.commit()
|
||||
flash(
|
||||
f"{email} registered with roles: {', '.join([role.name for role in form.roles.data])}.", category="success"
|
||||
)
|
||||
flash(f"Random password: {password}", category="warning")
|
||||
return model
|
||||
try:
|
||||
model = user_datastore.create_user(email=email, password=passwordHash, roles=roles)
|
||||
self.session.commit()
|
||||
except Exception as ex:
|
||||
flash(ex, "error")
|
||||
|
||||
self.session.rollback()
|
||||
else:
|
||||
flash(
|
||||
f"{email} registered with roles: {', '.join([role.name for role in form.roles.data])}.",
|
||||
category="success",
|
||||
)
|
||||
flash(f"Random password: {password}", category="warning")
|
||||
return model
|
||||
|
||||
|
||||
class RoleModelView(SecureModelView):
|
||||
|
@ -67,19 +74,35 @@ class RoleModelView(SecureModelView):
|
|||
|
||||
|
||||
class SemesterModelView(SecureModelView):
|
||||
column_list = ["label", "parts"]
|
||||
form_columns = ["label", "create_parts"]
|
||||
can_edit = False
|
||||
|
||||
column_list = ["label", "parts"]
|
||||
form_columns = ["semester_label", "year", "create_parts"]
|
||||
|
||||
semesterLabels = ["WS", "SS"]
|
||||
form_extra_fields = {
|
||||
"create_parts": BooleanField("Create parts:" + ", ".join(getConfig("partsLabels")) + ".", default=True)
|
||||
"semester_label": SelectField(
|
||||
"Semester", choices=list(zip(semesterLabels, semesterLabels)), validators=[DataRequired()]
|
||||
),
|
||||
"year": TextField("Year", validators=[DataRequired()]),
|
||||
"create_parts": BooleanField("Create parts:" + ", ".join(getConfig("partLabels")) + ".", default=True),
|
||||
}
|
||||
|
||||
def create_model(self, form):
|
||||
try:
|
||||
model = Semester(label=form.semester_label.data + form.year.data)
|
||||
self.session.add(model)
|
||||
self.session.commit()
|
||||
except Exception as ex:
|
||||
flash(ex, "error")
|
||||
|
||||
self.session.rollback()
|
||||
else:
|
||||
self.after_model_change(form, model, True)
|
||||
return model
|
||||
|
||||
def after_model_change(self, form, model, is_created):
|
||||
if form.create_parts.data:
|
||||
if is_created == False and model.parts != []:
|
||||
flash("This semester already has parts!", "danger")
|
||||
return
|
||||
|
||||
model.createParts()
|
||||
|
||||
if is_created:
|
||||
|
@ -98,8 +121,8 @@ class PartModelView(SecureModelView):
|
|||
column_details_list = ["label", "semester", "part_experiments", "part_students", "groups"]
|
||||
form_columns = ["label", "semester"]
|
||||
|
||||
partsLabels = getConfig("partsLabels")
|
||||
form_choices = {"label": list(zip(partsLabels, partsLabels))}
|
||||
partLabels = getConfig("partLabels")
|
||||
form_choices = {"label": list(zip(partLabels, partLabels))}
|
||||
|
||||
|
||||
class StudentModelView(SecureModelView):
|
||||
|
@ -114,7 +137,7 @@ class StudentModelView(SecureModelView):
|
|||
"contact_email": {"validators": [Email()]},
|
||||
}
|
||||
|
||||
partChoices = ["-"] + getConfig("partsLabels")
|
||||
partChoices = ["-"] + getConfig("partLabels")
|
||||
form_extra_fields = {
|
||||
"new_part_student_part": SelectField(
|
||||
"Part", choices=list(zip(partChoices, partChoices)), default=partChoices[0]
|
||||
|
@ -148,7 +171,7 @@ class StudentModelView(SecureModelView):
|
|||
|
||||
def after_model_change(self, form, model, is_created):
|
||||
partLabel = form.new_part_student_part.data
|
||||
print("\nLL\n")
|
||||
|
||||
if partLabel != self.partChoices[0]:
|
||||
groupNumber = int(form.new_part_student_group_number.data)
|
||||
|
||||
|
@ -156,14 +179,28 @@ class StudentModelView(SecureModelView):
|
|||
group = Group.query.filter(Group.number == groupNumber, Group.part == part).first()
|
||||
|
||||
if group is None:
|
||||
group = Group(number=groupNumber, part=part)
|
||||
db.session.add(group)
|
||||
flash(f"Added the new group with number {str(groupNumber)} in part {str(part)}.", "success")
|
||||
try:
|
||||
group = Group(number=groupNumber, part=part)
|
||||
self.session.add(group)
|
||||
self.session.commit()
|
||||
except Exception as ex:
|
||||
flash(ex, "error")
|
||||
|
||||
partStudent = PartStudent(student=model, part=part, group=group)
|
||||
db.session.add(partStudent)
|
||||
db.session.commit()
|
||||
flash("Added part student.", "success")
|
||||
self.session.rollback()
|
||||
return
|
||||
else:
|
||||
flash(f"Added the new group with number {str(groupNumber)} in part {str(part)}.", "success")
|
||||
|
||||
try:
|
||||
partStudent = PartStudent(student=model, part=part, group=group)
|
||||
self.session.add(partStudent)
|
||||
self.session.commit()
|
||||
except Exception as ex:
|
||||
flash(ex, "error")
|
||||
|
||||
self.session.rollback()
|
||||
else:
|
||||
flash("Added part student.", "success")
|
||||
|
||||
|
||||
class PartFilter(BaseSQLAFilter):
|
||||
|
@ -182,14 +219,15 @@ class PartFilter(BaseSQLAFilter):
|
|||
|
||||
|
||||
class PartStudentModelView(SecureModelView):
|
||||
partsLabels = getConfig("partsLabels")
|
||||
column_filters = [PartFilter(PartStudent.part_id, "Part", options=list(zip(partsLabels, partsLabels)))]
|
||||
partLabels = getConfig("partLabels")
|
||||
column_filters = [PartFilter(PartStudent.part_id, "Part", options=list(zip(partLabels, partLabels)))]
|
||||
|
||||
form_excluded_columns = ["experiment_marks"]
|
||||
|
||||
|
||||
class GroupModelView(SecureModelView):
|
||||
partsLabels = getConfig("partsLabels")
|
||||
column_filters = [PartFilter(Group.part_id, "Part", options=list(zip(partsLabels, partsLabels)))]
|
||||
partLabels = getConfig("partLabels")
|
||||
column_filters = [PartFilter(Group.part_id, "Part", options=list(zip(partLabels, partLabels)))]
|
||||
|
||||
def validate_form(self, form):
|
||||
if request.method == "POST":
|
||||
|
|
|
@ -144,9 +144,14 @@ class Semester(db.Model):
|
|||
return f"<{self.label}>"
|
||||
|
||||
def createParts(self):
|
||||
for partLabel in getConfig("partsLabels"):
|
||||
db.session.add(Part(label=partLabel, semester=self))
|
||||
db.session.commit()
|
||||
try:
|
||||
for partLabel in getConfig("partLabels"):
|
||||
db.session.add(Part(label=partLabel, semester=self))
|
||||
db.session.commit()
|
||||
except Exception as ex:
|
||||
flash(ex, "error")
|
||||
|
||||
db.session.rollback()
|
||||
|
||||
|
||||
class ExperimentMark(db.Model):
|
||||
|
|
10
config.json
10
config.json
|
@ -1 +1,9 @@
|
|||
{"partsLabels": ["A/1", "A/2", "A/m", "B/1", "B/2"]}
|
||||
{
|
||||
"partLabels": [
|
||||
"A/1",
|
||||
"A/2",
|
||||
"A/m",
|
||||
"B/1",
|
||||
"B/2"
|
||||
]
|
||||
}
|
2
poetry.lock
generated
2
poetry.lock
generated
|
@ -391,7 +391,7 @@ locale = ["Babel (>=1.3)"]
|
|||
[metadata]
|
||||
lock-version = "1.1"
|
||||
python-versions = "^3.9"
|
||||
content-hash = "db7a8f38d58cc2d8c8a1f7ca99dc8d3ab7a544b1b1899022c2bae7ffe5fa3aa2"
|
||||
content-hash = "6a893e3ebd4bf4ad943714d6187c0b34a988665318819b5bda514dc0eb0ad6da"
|
||||
|
||||
[metadata.files]
|
||||
appdirs = [
|
||||
|
|
|
@ -9,9 +9,7 @@ readme = "README.md"
|
|||
python = "^3.9"
|
||||
Flask = ">=2.0.1"
|
||||
Flask-SQLAlchemy = ">=2.5.1"
|
||||
SQLAlchemy = ">=1.4.17"
|
||||
Flask-WTF = ">=0.15.1"
|
||||
email-validator = ">=1.1.2"
|
||||
Flask-Security-Too = ">=4.0.1"
|
||||
Flask-Admin = ">=1.5.8"
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue