mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-11-08 21:21:06 +00:00
Add Admin model
This commit is contained in:
parent
de4ef9d6f4
commit
205d90f88a
4 changed files with 47 additions and 23 deletions
|
@ -3,7 +3,7 @@ from pathlib import Path
|
||||||
from flask import flash, redirect, request, url_for
|
from flask import flash, redirect, request, url_for
|
||||||
from flask_admin import expose
|
from flask_admin import expose
|
||||||
from flask_admin.contrib.sqla.fields import QuerySelectField, QuerySelectMultipleField
|
from flask_admin.contrib.sqla.fields import QuerySelectField, QuerySelectMultipleField
|
||||||
from flask_admin.contrib.sqla.filters import BaseSQLAFilter
|
from flask_admin.contrib.sqla.filters import FilterEqual
|
||||||
from flask_admin.helpers import get_form_data
|
from flask_admin.helpers import get_form_data
|
||||||
from flask_admin.menu import MenuLink
|
from flask_admin.menu import MenuLink
|
||||||
from flask_admin.model.template import EndpointLinkRowAction
|
from flask_admin.model.template import EndpointLinkRowAction
|
||||||
|
@ -211,6 +211,10 @@ class UserView(SecureAdminModelView):
|
||||||
assistant = Assistant(user=model, semester_experiments=semester_experiments)
|
assistant = Assistant(user=model, semester_experiments=semester_experiments)
|
||||||
self.session.add(assistant)
|
self.session.add(assistant)
|
||||||
|
|
||||||
|
if model.has_role("admin") and not model.admin:
|
||||||
|
admin = Admin(user=model)
|
||||||
|
self.session.add(admin)
|
||||||
|
|
||||||
|
|
||||||
class RoleView(SecureAdminModelView):
|
class RoleView(SecureAdminModelView):
|
||||||
can_create = False
|
can_create = False
|
||||||
|
@ -835,19 +839,24 @@ class AppointmentView(SecureAdminModelView):
|
||||||
|
|
||||||
|
|
||||||
class ExperimentMarkView(SecureAdminModelView):
|
class ExperimentMarkView(SecureAdminModelView):
|
||||||
class StudentIdFilter(BaseSQLAFilter):
|
@expose("/")
|
||||||
def apply(self, query, value, alias=None):
|
def index_view(self):
|
||||||
return query.filter(self.column == value)
|
# To update filter options
|
||||||
|
self._refresh_filters_cache()
|
||||||
def operation(self):
|
return super().index_view()
|
||||||
return "equals"
|
|
||||||
|
|
||||||
|
class StudentFilter(FilterEqual):
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
if Student.query.get(value):
|
if Student.query.get(value):
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
class AssistantFilter(FilterEqual):
|
||||||
|
def get_options(self, view):
|
||||||
|
assistants = Assistant.query.filter(Assistant.user.has(User.active == True)).all()
|
||||||
|
return ((assistant.id, assistant.repr()) for assistant in assistants)
|
||||||
|
|
||||||
class CreateForm(Form):
|
class CreateForm(Form):
|
||||||
part_student = QuerySelectField(
|
part_student = QuerySelectField(
|
||||||
"Part Student",
|
"Part Student",
|
||||||
|
@ -882,20 +891,19 @@ class ExperimentMarkView(SecureAdminModelView):
|
||||||
"oral_mark": "Between 0 and 15",
|
"oral_mark": "Between 0 and 15",
|
||||||
"protocol_mark": "Between 0 and 15",
|
"protocol_mark": "Between 0 and 15",
|
||||||
"final_experiment_mark": "Calculated automatically with oral and protocol marks and weightings",
|
"final_experiment_mark": "Calculated automatically with oral and protocol marks and weightings",
|
||||||
"assistant": "The assistant who assigned the mark",
|
"assistant": "The last assistant who edited the mark",
|
||||||
"edited_by_admin": "If the mark was edited by an admin",
|
"admin": "The last admin who edited the mark",
|
||||||
}
|
}
|
||||||
|
|
||||||
column_filters = [
|
column_filters = [
|
||||||
StudentIdFilter(PartStudent.id, "Student / ID"),
|
StudentFilter(PartStudent.id, "Student / ID"),
|
||||||
"part_student.part.program",
|
"part_student.part.program",
|
||||||
"part_student.part",
|
"part_student.part",
|
||||||
"part_student.student",
|
"part_student.student",
|
||||||
"group_experiment.group",
|
"group_experiment.group",
|
||||||
"group_experiment.semester_experiment.semester",
|
"group_experiment.semester_experiment.semester",
|
||||||
"group_experiment.semester_experiment.experiment",
|
"group_experiment.semester_experiment.experiment",
|
||||||
"edited_by_admin",
|
AssistantFilter(Assistant.id, "Assistant"),
|
||||||
"assistant.user",
|
|
||||||
"oral_mark",
|
"oral_mark",
|
||||||
"protocol_mark",
|
"protocol_mark",
|
||||||
"final_experiment_mark",
|
"final_experiment_mark",
|
||||||
|
@ -923,7 +931,7 @@ class ExperimentMarkView(SecureAdminModelView):
|
||||||
if (form.oral_mark and form.oral_mark.data != model.oral_mark) or (
|
if (form.oral_mark and form.oral_mark.data != model.oral_mark) or (
|
||||||
form.protocol_mark and form.protocol_mark.data != model.protocol_mark
|
form.protocol_mark and form.protocol_mark.data != model.protocol_mark
|
||||||
):
|
):
|
||||||
model.edited_by_admin = True
|
model.admin = current_user.admin
|
||||||
|
|
||||||
ret = super().update_model(form, model)
|
ret = super().update_model(form, model)
|
||||||
|
|
||||||
|
|
|
@ -79,7 +79,7 @@ class AssistantExperimentMarkView(SecureAssistantModelView):
|
||||||
"part_student.student.contact_email",
|
"part_student.student.contact_email",
|
||||||
"part_student.part",
|
"part_student.part",
|
||||||
"assistant",
|
"assistant",
|
||||||
"edited_by_admin",
|
"admin",
|
||||||
]
|
]
|
||||||
column_labels = {
|
column_labels = {
|
||||||
"group_experiment.semester_experiment.experiment": "Experiment",
|
"group_experiment.semester_experiment.experiment": "Experiment",
|
||||||
|
@ -95,8 +95,8 @@ class AssistantExperimentMarkView(SecureAssistantModelView):
|
||||||
"protocol_mark": "Between 0 and 15",
|
"protocol_mark": "Between 0 and 15",
|
||||||
"final_experiment_mark": "Calculated automatically with oral and protocol marks and weightings",
|
"final_experiment_mark": "Calculated automatically with oral and protocol marks and weightings",
|
||||||
"part_student.student.contact_email": "The preferred contact email address if entered by the student",
|
"part_student.student.contact_email": "The preferred contact email address if entered by the student",
|
||||||
"assistant": "The assistant who assigned the mark",
|
"assistant": "The last assistant who edited the mark",
|
||||||
"edited_by_admin": "If the mark was edited by an admin",
|
"admin": "The last admin who edited the mark",
|
||||||
}
|
}
|
||||||
|
|
||||||
column_editable_list = [
|
column_editable_list = [
|
||||||
|
|
|
@ -326,6 +326,20 @@ class Assistant(db.Model):
|
||||||
return f"<ASST: {self.repr()}>"
|
return f"<ASST: {self.repr()}>"
|
||||||
|
|
||||||
|
|
||||||
|
class Admin(db.Model):
|
||||||
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
|
||||||
|
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False, unique=True)
|
||||||
|
|
||||||
|
experiment_marks = db.relationship("ExperimentMark", backref="admin", lazy=True)
|
||||||
|
|
||||||
|
def repr(self):
|
||||||
|
return f"{self.user.first_name} {self.user.last_name}"
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<ADMIN: {self.repr()}>"
|
||||||
|
|
||||||
|
|
||||||
class Appointment(db.Model):
|
class Appointment(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
date = db.Column(db.Date, nullable=False) # To be specified with the python package "datetime"
|
date = db.Column(db.Date, nullable=False) # To be specified with the python package "datetime"
|
||||||
|
@ -493,13 +507,12 @@ class ExperimentMark(db.Model):
|
||||||
nullable=True,
|
nullable=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
edited_by_admin = db.Column(db.Boolean, default=False, nullable=False) # TODO: Admin id
|
|
||||||
|
|
||||||
part_student_id = db.Column(db.Integer, db.ForeignKey("part_student.id"), nullable=False)
|
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)
|
group_experiment_id = db.Column(db.Integer, db.ForeignKey("group_experiment.id"), nullable=False)
|
||||||
assistant_id = db.Column(
|
assistant_id = db.Column(
|
||||||
db.Integer, db.ForeignKey("assistant.id"), nullable=True
|
db.Integer, db.ForeignKey("assistant.id"), nullable=True
|
||||||
) # The assistant who gave the mark
|
) # The last assistant who edited the mark
|
||||||
|
admin_id = db.Column(db.Integer, db.ForeignKey("admin.id"), nullable=True) # The last admin who edited the mark
|
||||||
|
|
||||||
__table_args__ = (db.UniqueConstraint(part_student_id, group_experiment_id),)
|
__table_args__ = (db.UniqueConstraint(part_student_id, group_experiment_id),)
|
||||||
|
|
||||||
|
@ -529,6 +542,7 @@ class User(db.Model, FsUserMixin):
|
||||||
|
|
||||||
active_semester_id = db.Column(db.Integer, db.ForeignKey("semester.id"), nullable=True)
|
active_semester_id = db.Column(db.Integer, db.ForeignKey("semester.id"), nullable=True)
|
||||||
|
|
||||||
|
admin = db.relationship("Admin", backref="user", lazy=False, uselist=False)
|
||||||
assistant = db.relationship("Assistant", backref="user", lazy=True, uselist=False)
|
assistant = db.relationship("Assistant", backref="user", lazy=True, uselist=False)
|
||||||
|
|
||||||
def repr(self):
|
def repr(self):
|
||||||
|
|
10
testDB.py
10
testDB.py
|
@ -98,7 +98,7 @@ with app.app_context():
|
||||||
user_datastore.create_role(name="admin")
|
user_datastore.create_role(name="admin")
|
||||||
user_datastore.create_role(name="assistant")
|
user_datastore.create_role(name="assistant")
|
||||||
|
|
||||||
user_datastore.create_user(
|
admin_user = user_datastore.create_user(
|
||||||
email="admin@advlabdb.de",
|
email="admin@advlabdb.de",
|
||||||
password=hash_password("admin"),
|
password=hash_password("admin"),
|
||||||
roles=["admin"],
|
roles=["admin"],
|
||||||
|
@ -106,6 +106,10 @@ with app.app_context():
|
||||||
last_name="Blümler",
|
last_name="Blümler",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
admin = Admin(user=admin_user)
|
||||||
|
|
||||||
|
db.session.add(admin)
|
||||||
|
|
||||||
us1 = user_datastore.create_user(
|
us1 = user_datastore.create_user(
|
||||||
email="test@protonmail.com",
|
email="test@protonmail.com",
|
||||||
password=hash_password("h1"),
|
password=hash_password("h1"),
|
||||||
|
@ -123,9 +127,7 @@ with app.app_context():
|
||||||
last_name="l",
|
last_name="l",
|
||||||
)
|
)
|
||||||
|
|
||||||
as1 = Assistant(
|
as1 = Assistant(user=us1)
|
||||||
user=us1,
|
|
||||||
)
|
|
||||||
as2 = Assistant(user=us2)
|
as2 = Assistant(user=us2)
|
||||||
|
|
||||||
as1.semester_experiments.append(sx1)
|
as1.semester_experiments.append(sx1)
|
||||||
|
|
Loading…
Reference in a new issue