From 9430654ea13b5fa36b1cec1f7cca1c66f0422a7e Mon Sep 17 00:00:00 2001 From: Mo8it Date: Sat, 21 Aug 2021 22:58:23 +0200 Subject: [PATCH] Improved feedback after changing experiment mark --- advlabdb/adminModelViews.py | 26 +++++++++++++++++--------- advlabdb/models.py | 15 +++++++++++++-- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/advlabdb/adminModelViews.py b/advlabdb/adminModelViews.py index 6af09f6..0d7373c 100644 --- a/advlabdb/adminModelViews.py +++ b/advlabdb/adminModelViews.py @@ -7,7 +7,7 @@ from flask_admin.model.template import EndpointLinkRowAction from flask_security import admin_change_password, current_user, hash_password from sqlalchemy import func, or_, and_ from wtforms import BooleanField, Form, RadioField, SelectField, TextField -from wtforms.fields.html5 import DateField +from wtforms.fields.html5 import DateField, IntegerField from wtforms.validators import URL, DataRequired, Email, NumberRange, Optional from advlabdb import adminSpace, app, assistantSpace, db, user_datastore @@ -689,9 +689,19 @@ class ExperimentMarkView(SecureAdminModelView): blank_text="-", ) - form = CreateForm + class EditForm(Form): + oral_mark = IntegerField( + "Oral Mark", + validators=[NumberRange(min=0, max=15), Optional()], + description="Between 0 and 15", + ) + protocol_mark = IntegerField( + "Protocol Mark", + validators=[NumberRange(min=0, max=15), Optional()], + description="Between 0 and 15", + ) - can_edit = False + form = EditForm column_descriptions = { "oral_mark": "Between 0 and 15", @@ -700,12 +710,6 @@ class ExperimentMarkView(SecureAdminModelView): "edited_by_admin": "If the mark was edited by an admin", } - column_editable_list = ["oral_mark", "protocol_mark"] - form_args = { - "oral_mark": {"validators": [NumberRange(0, 15)]}, - "protocol_mark": {"validators": [NumberRange(0, 15)]}, - } - column_filters = [ StudentIdFilter(PartStudent.id, "Student / ID"), "part_student.part.program", @@ -727,6 +731,10 @@ class ExperimentMarkView(SecureAdminModelView): ) """ + def create_form(self, obj=None): + form = self.CreateForm + return form(get_form_data(), obj=obj) + def customCreateModel(self, form): return ExperimentMark.customInit( part_student=form.part_student.data, group_experiment=form.group_experiment.data diff --git a/advlabdb/models.py b/advlabdb/models.py index d75ee89..d2648d5 100644 --- a/advlabdb/models.py +++ b/advlabdb/models.py @@ -8,6 +8,7 @@ https://flask-sqlalchemy.palletsprojects.com/en/2.x/models/ # Imports from flask import flash, has_request_context +from flask_security import current_user from flask_security.models.fsqla_v2 import FsRoleMixin, FsUserMixin from decimal import Decimal, ROUND_HALF_UP @@ -114,8 +115,18 @@ class PartStudent(db.Model): self.session.rollback() else: - if oldFinalPartMark != self.final_part_mark: - flash(f"Final part mark changed for {self} from {oldFinalPartMark} to {self.final_part_mark}.") + if current_user.has_role("admin"): + if oldFinalPartMark != self.final_part_mark: + if oldFinalPartMark > self.final_part_mark: + category = "danger" + else: + category = "warning" + flash( + f"Final part mark changed for {self} from {oldFinalPartMark} to {self.final_part_mark}.", + category, + ) + else: + flash(f"Final part mark did not change for {self} from {oldFinalPartMark}.") def repr(self): return f"{self.student.repr()} {self.part.repr()}"