1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-12-20 23:41:20 +00:00

Added CheckConstraints

This commit is contained in:
Mo 2021-07-11 14:27:00 +02:00
parent c9143410d4
commit 72b05beefc
3 changed files with 28 additions and 17 deletions

File diff suppressed because one or more lines are too long

View file

@ -4,7 +4,7 @@ from flask_admin.menu import MenuLink
from flask_admin.model.template import EndpointLinkRowAction
from flask_security import current_user, hash_password
from sqlalchemy import func
from wtforms import Form, BooleanField, SelectField, TextField, RadioField, FloatField
from wtforms import Form, BooleanField, SelectField, TextField, RadioField, IntegerField
from wtforms.validators import DataRequired, Email, Optional
from flask_admin.contrib.sqla.fields import QuerySelectMultipleField, QuerySelectField
from flask_admin.helpers import get_form_data
@ -37,7 +37,7 @@ from advlabdb.utils import (
class UserView(SecureModelView):
column_list = ["email", "active", "roles", "assistant"]
column_list = ["email", "active", "roles", "assistant", "active_semester"]
column_searchable_list = ["email"]
column_filters = ["active"]
form_columns = ["email", "active", "roles"]
@ -68,7 +68,7 @@ class UserView(SecureModelView):
self.on_model_change(form, model, True)
self.session.commit()
except Exception as ex:
flash(ex, "error")
flash(str(ex), "error")
self.session.rollback()
else:
@ -106,9 +106,10 @@ class RoleView(SecureModelView):
class SemesterView(SecureModelView):
can_edit = False
can_delete = False
column_display_actions = False
can_view_details = True
column_list = ["label", "parts"]
column_details_list = column_list + ["active_users"]
form_columns = ["semester_label", "year", "transfer_parts", "transfer_assistants"]
semesterLabels = ["WS", "SS"]
@ -137,7 +138,7 @@ class SemesterView(SecureModelView):
self.on_model_change(form, model, True)
self.session.commit()
except Exception as ex:
flash(ex, "error")
flash(str(ex), "error")
self.session.rollback()
else:
@ -177,7 +178,7 @@ class SemesterView(SecureModelView):
self.session.commit()
except Exception as ex:
flash(ex, "error")
flash(str(ex), "error")
self.session.rollback()
@ -248,7 +249,7 @@ class PartStudentView(SecureModelView):
class EditForm(CreateForm):
student = None
part = None
final_part_mark = FloatField("Final Part Mark", validators=[Optional()])
final_part_mark = IntegerField("Final Part Mark", validators=[Optional()])
form = EditForm
@ -316,7 +317,7 @@ class GroupView(SecureModelView):
self.on_model_change(form, model, True)
self.session.commit()
except Exception as ex:
flash(ex, "error")
flash(str(ex), "error")
self.session.rollback()
else:
@ -481,7 +482,7 @@ class GroupExperimentView(SecureModelView):
self.on_model_change(form, model, True)
self.session.commit()
except Exception as ex:
flash(ex, "error")
flash(str(ex), "error")
self.session.rollback()
else:

View file

@ -17,7 +17,7 @@ from advlabdb.configUtils import getConfig
class Student(db.Model):
id = db.Column(db.Integer, primary_key=True)
student_number = db.Column(db.Integer, nullable=False, unique=True)
student_number = db.Column(db.Integer, db.CheckConstraint("student_number > -1"), nullable=False, unique=True)
first_name = db.Column(db.String(100), nullable=False)
last_name = db.Column(db.String(100), nullable=False)
uni_email = db.Column(db.String(200), nullable=False, unique=True)
@ -37,7 +37,12 @@ class Student(db.Model):
class PartStudent(db.Model):
# A student doing a specific part
id = db.Column(db.Integer, primary_key=True)
final_part_mark = db.Column(db.Integer, nullable=True)
final_part_mark = db.Column(
db.Integer,
db.CheckConstraint("final_part_mark > -1"),
db.CheckConstraint("final_part_mark < 16"),
nullable=True,
)
student_id = db.Column(db.Integer, db.ForeignKey("student.id"), nullable=False)
part_id = db.Column(db.Integer, db.ForeignKey("part.id"), nullable=False)
group_id = db.Column(db.Integer, db.ForeignKey("group.id"), nullable=True)
@ -57,7 +62,7 @@ class PartStudent(db.Model):
class Group(db.Model):
id = db.Column(db.Integer, primary_key=True)
number = db.Column(db.Integer, nullable=False)
number = db.Column(db.Integer, db.CheckConstraint("number > 0"), nullable=False)
part_id = db.Column(db.Integer, db.ForeignKey("part.id"), nullable=False)
part_students = db.relationship("PartStudent", backref="group", lazy=True)
group_experiments = db.relationship("GroupExperiment", backref="group", lazy=True)
@ -109,7 +114,7 @@ class Experiment(db.Model):
room = db.Column(db.String(100), nullable=False)
building = db.Column(db.String(100), nullable=False)
responsibility = db.Column(db.String(200), nullable=True)
duration_in_days = db.Column(db.Integer, nullable=False)
duration_in_days = db.Column(db.Integer, db.CheckConstraint("duration_in_days > -1"), nullable=False)
active = db.Column(db.Boolean, nullable=False, default=True)
oral_weighting = db.Column(db.Float, nullable=False)
protocol_weighting = db.Column(db.Float, nullable=False)
@ -204,6 +209,7 @@ class Semester(db.Model):
label = db.Column(db.String(100), nullable=False, unique=True) # WS2122 for example
parts = db.relationship("Part", backref="semester", lazy=True)
semester_experiments = db.relationship("SemesterExperiment", backref="semester", lazy=True)
active_users = db.relationship("User", backref="active_semester", lazy=True)
def repr(self):
return f"{self.label}"
@ -224,8 +230,12 @@ class Semester(db.Model):
class ExperimentMark(db.Model):
# A mark for a student after a specific experiment
id = db.Column(db.Integer, primary_key=True)
oral_mark = db.Column(db.Integer, nullable=True)
protocol_mark = db.Column(db.Integer, nullable=True)
oral_mark = db.Column(
db.Integer, db.CheckConstraint("oral_mark > -1"), db.CheckConstraint("oral_mark < 16"), nullable=True
)
protocol_mark = db.Column(
db.Integer, db.CheckConstraint("protocol_mark > -1"), db.CheckConstraint("protocol_mark < 16"), nullable=True
)
part_student_id = db.Column(db.Integer, db.ForeignKey("part_student.id"), nullable=False)
group_experiment_id = db.Column(db.Integer, db.ForeignKey("group_experiment.id"), nullable=False)
assistant_id = db.Column(
@ -243,7 +253,7 @@ class ExperimentMark(db.Model):
class User(db.Model, FsUserMixin):
assistant = db.relationship("Assistant", backref="user", lazy=True, uselist=False)
active_semester_id = db.Column(db.Integer, nullable=True)
active_semester_id = db.Column(db.Integer, db.ForeignKey("semester.id"), nullable=True)
def repr(self):
return f"{self.email}"