mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-11-08 21:21:06 +00:00
Addded exceptions
This commit is contained in:
parent
3024d2661e
commit
91547a7487
4 changed files with 29 additions and 33 deletions
|
@ -1,7 +1,9 @@
|
||||||
from flask_admin import AdminIndexView
|
from flask_admin import AdminIndexView
|
||||||
from flask_admin.contrib.sqla import ModelView
|
from flask_admin.contrib.sqla import ModelView
|
||||||
from flask_security import current_user
|
from flask_security import current_user
|
||||||
from flask import redirect, request, url_for
|
from flask import redirect, request, url_for, flash
|
||||||
|
|
||||||
|
from advlabdb.exceptions import ModelViewValidatorException
|
||||||
|
|
||||||
|
|
||||||
def adminViewIsAccessible():
|
def adminViewIsAccessible():
|
||||||
|
@ -49,3 +51,10 @@ class SecureModelView(ModelView):
|
||||||
return super().get_count_query().filter(self.queryFilter())
|
return super().get_count_query().filter(self.queryFilter())
|
||||||
else:
|
else:
|
||||||
return super().get_count_query()
|
return super().get_count_query()
|
||||||
|
|
||||||
|
def handle_view_exception(self, exc):
|
||||||
|
if type(exc) == ModelViewValidatorException:
|
||||||
|
flash(str(exc), "error")
|
||||||
|
return True
|
||||||
|
|
||||||
|
return super().handle_view_exception(exc)
|
||||||
|
|
2
advlabdb/exceptions.py
Normal file
2
advlabdb/exceptions.py
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
class ModelViewValidatorException(Exception):
|
||||||
|
pass
|
|
@ -34,6 +34,7 @@ from advlabdb.utils import (
|
||||||
setUserActiveSemester,
|
setUserActiveSemester,
|
||||||
userActiveSemester,
|
userActiveSemester,
|
||||||
)
|
)
|
||||||
|
from advlabdb.exceptions import ModelViewValidatorException
|
||||||
|
|
||||||
|
|
||||||
class UserView(SecureModelView):
|
class UserView(SecureModelView):
|
||||||
|
@ -49,9 +50,6 @@ class UserView(SecureModelView):
|
||||||
"roles": {"validators": [DataRequired(message="A role is required!")]},
|
"roles": {"validators": [DataRequired(message="A role is required!")]},
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteSelfException = "Tried to delete yourself as user!"
|
|
||||||
deactivateSelfException = "Tried to deactiavte yourself as user!"
|
|
||||||
|
|
||||||
def create_model(self, form):
|
def create_model(self, form):
|
||||||
password = randomPassword()
|
password = randomPassword()
|
||||||
passwordHash = hash_password(password)
|
passwordHash = hash_password(password)
|
||||||
|
@ -81,17 +79,11 @@ class UserView(SecureModelView):
|
||||||
|
|
||||||
def on_model_delete(self, model):
|
def on_model_delete(self, model):
|
||||||
if model == current_user:
|
if model == current_user:
|
||||||
raise Exception(self.deleteSelfException)
|
raise ModelViewValidatorException("Tried to delete yourself as user!")
|
||||||
|
|
||||||
def on_model_change(self, form, model, is_created):
|
def on_model_change(self, form, model, is_created):
|
||||||
if model == current_user and not form.active.data:
|
if model == current_user and not form.active.data:
|
||||||
raise Exception(self.deactivateSelfException)
|
raise ModelViewValidatorException("Tried to deactiavte yourself as user!")
|
||||||
|
|
||||||
def handle_view_exception(self, exc):
|
|
||||||
if exc.args[0] in (self.deleteSelfException, self.deactivateSelfException):
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
return super().handle_view_exception(exc)
|
|
||||||
|
|
||||||
|
|
||||||
class RoleView(SecureModelView):
|
class RoleView(SecureModelView):
|
||||||
|
@ -261,8 +253,6 @@ class PartStudentView(SecureModelView):
|
||||||
|
|
||||||
column_filters = ["part", "student", "group"]
|
column_filters = ["part", "student", "group"]
|
||||||
|
|
||||||
partGroupPartMismatchException = "Student's part and group's part do not match!"
|
|
||||||
|
|
||||||
def queryFilter(self):
|
def queryFilter(self):
|
||||||
return PartStudent.part_id.in_([part.id for part in userActiveSemester().parts])
|
return PartStudent.part_id.in_([part.id for part in userActiveSemester().parts])
|
||||||
|
|
||||||
|
@ -272,13 +262,7 @@ class PartStudentView(SecureModelView):
|
||||||
|
|
||||||
def on_model_change(self, form, model, is_created):
|
def on_model_change(self, form, model, is_created):
|
||||||
if model.group and model.part != model.group.part:
|
if model.group and model.part != model.group.part:
|
||||||
raise Exception(self.partGroupPartMismatchException)
|
raise ModelViewValidatorException("Student's part and group's part do not match!")
|
||||||
|
|
||||||
def handle_view_exception(self, exc):
|
|
||||||
if exc.args[0] in (self.partGroupPartMismatchException):
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
return super().handle_view_exception(exc)
|
|
||||||
|
|
||||||
def update_model(self, form, model):
|
def update_model(self, form, model):
|
||||||
if form.final_part_mark.data == -1:
|
if form.final_part_mark.data == -1:
|
||||||
|
@ -310,8 +294,6 @@ 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"]
|
||||||
|
|
||||||
partStudentPartPartMismatchException = "Group's part and student's part do not match!"
|
|
||||||
|
|
||||||
def queryFilter(self):
|
def queryFilter(self):
|
||||||
return Group.part_id.in_([part.id for part in userActiveSemester().parts])
|
return Group.part_id.in_([part.id for part in userActiveSemester().parts])
|
||||||
|
|
||||||
|
@ -333,13 +315,7 @@ class GroupView(SecureModelView):
|
||||||
def on_model_change(self, form, model, is_created):
|
def on_model_change(self, form, model, is_created):
|
||||||
for partStudent in model.part_students:
|
for partStudent in model.part_students:
|
||||||
if model.part != partStudent.part:
|
if model.part != partStudent.part:
|
||||||
raise Exception(self.partStudentPartPartMismatchException)
|
raise ModelViewValidatorException("Group's part and student's part do not match!")
|
||||||
|
|
||||||
def handle_view_exception(self, exc):
|
|
||||||
if exc.args[0] in (self.partStudentPartPartMismatchException):
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
return super().handle_view_exception(exc)
|
|
||||||
|
|
||||||
def create_form(self, obj=None):
|
def create_form(self, obj=None):
|
||||||
form = self.CreateForm
|
form = self.CreateForm
|
||||||
|
|
|
@ -131,9 +131,18 @@ class Experiment(db.Model):
|
||||||
responsibility = db.Column(db.String(200), nullable=True)
|
responsibility = db.Column(db.String(200), nullable=True)
|
||||||
duration_in_days = db.Column(db.Integer, db.CheckConstraint("duration_in_days > -1"), nullable=False)
|
duration_in_days = db.Column(db.Integer, db.CheckConstraint("duration_in_days > -1"), nullable=False)
|
||||||
active = db.Column(db.Boolean, default=True, nullable=False)
|
active = db.Column(db.Boolean, default=True, nullable=False)
|
||||||
oral_weighting = db.Column(db.Float, nullable=False)
|
oral_weighting = db.Column(
|
||||||
protocol_weighting = db.Column(db.Float, nullable=False)
|
db.Float, db.CheckConstraint("oral_weighting >= 0"), db.CheckConstraint("oral_weighting <= 1"), nullable=False
|
||||||
final_weighting = db.Column(db.Float, nullable=False)
|
)
|
||||||
|
protocol_weighting = db.Column(
|
||||||
|
db.Float,
|
||||||
|
db.CheckConstraint("protocol_weighting >= 0"),
|
||||||
|
db.CheckConstraint("protocol_weighting <= 1"),
|
||||||
|
nullable=False,
|
||||||
|
)
|
||||||
|
final_weighting = db.Column(
|
||||||
|
db.Float, db.CheckConstraint("final_weighting >= 0"), db.CheckConstraint("final_weightin <= 1"), nullable=False
|
||||||
|
)
|
||||||
semester_experiments = db.relationship("SemesterExperiment", backref="experiment", lazy=True)
|
semester_experiments = db.relationship("SemesterExperiment", backref="experiment", lazy=True)
|
||||||
|
|
||||||
def repr(self):
|
def repr(self):
|
||||||
|
|
Loading…
Reference in a new issue