diff --git a/app/constants.py b/app/constants.py index 511428b5cbb78b60e66178122675b66750ab3c84..0ddad8d6bfc0597316bbb80afdae777f28369c27 100644 --- a/app/constants.py +++ b/app/constants.py @@ -1,2 +1,6 @@ +from datetime import datetime + STUDENT = "student" STAFF = "staff" + +DEADLINE_FOR_STUDENT_CHANGES = datetime(2022, 9, 17, 7, 0) \ No newline at end of file diff --git a/app/messages/__init__.py b/app/messages/__init__.py index ee50d4924c11dd7a8c8f7c4801793fd25a657b66..d5652d86b1468618d1f4c23d2cf42cfa61fdb5f4 100644 --- a/app/messages/__init__.py +++ b/app/messages/__init__.py @@ -2,7 +2,7 @@ from .message_builders import ( login_manager_message, login_unsuccessful_error, project_not_found, - shortlist_not_found, + shortlist_not_found, deadline_expired_for_students, ) ######################################################################### @@ -14,3 +14,5 @@ LOGIN_MANAGER_MESSAGE = login_manager_message() PROJECT_NOT_FOUND = project_not_found() SHORTLIST_NOT_FOUND = shortlist_not_found() + +DEADLINE_EXPIRED_FOR_STUDENTS = deadline_expired_for_students() diff --git a/app/messages/message_builders.py b/app/messages/message_builders.py index c279131f699652ac10961edbc87bdfeffc568897..8d49a458c76538eb70ec5300fec83f67eb6d9f81 100644 --- a/app/messages/message_builders.py +++ b/app/messages/message_builders.py @@ -64,3 +64,13 @@ def shortlist_not_found(): style=ERROR, ) ) + + +def deadline_expired_for_students(): + return MessageEncoder.encode( + Message( + header="<h3><strong>Deadline expired</strong></h3>", + body="<p>The deadline to make changes has now expired, and you cannot make changes to your shortlisting and ranking. If you have questions, please contact Lorenzo Picinali.</p>", + style=ERROR, + ) + ) \ No newline at end of file diff --git a/app/utils/decorators.py b/app/utils/decorators.py index 787852c65eed3f671184b1fc13370391131a838d..a46fe5ca4b891f224c8ca0523cf0c7e8cb5ec397 100644 --- a/app/utils/decorators.py +++ b/app/utils/decorators.py @@ -1,10 +1,13 @@ from functools import wraps -from flask import url_for +from flask import url_for, flash from flask_login import current_user from werkzeug.utils import redirect -from app.constants import STAFF, STUDENT +from app import messages +from app.constants import STAFF, STUDENT, DEADLINE_FOR_STUDENT_CHANGES + +from datetime import datetime def staff_only(func): @@ -25,3 +28,14 @@ def students_only(func): return redirect(url_for(f"{current_user.role}.projects")) return inner + + +def only_before_deadline_for_students(func): + @wraps(func) + def inner(*args, **kwargs): + if datetime.utcnow() <= DEADLINE_FOR_STUDENT_CHANGES: + return func(*args, **kwargs) + flash(messages.DEADLINE_EXPIRED_FOR_STUDENTS) + return redirect(url_for(f"{current_user.role}.projects")) + + return inner \ No newline at end of file diff --git a/app/views/student.py b/app/views/student.py index 2c725c87d2f9874d1e18637c1e79c2dd5169f24f..b54972972b3351f7cbcd20108c19fe2287317b89 100644 --- a/app/views/student.py +++ b/app/views/student.py @@ -5,7 +5,7 @@ from app import messages from app.database import db from app.models.project import Project from app.models.shortlist import Shortlisting -from app.utils.decorators import students_only +from app.utils.decorators import students_only, only_before_deadline_for_students bp = Blueprint("student", __name__, url_prefix="/student") @@ -44,6 +44,7 @@ def projects_ranking(): @bp.route("/projects/rankings", methods=["PUT"]) @login_required @students_only +@only_before_deadline_for_students def update_ranking(): project_ids = request.json current_shortlist: list[Shortlisting] = Shortlisting.query.filter( @@ -68,6 +69,7 @@ def view_project(project_id): @bp.route("/projects/<project_id>/shortlist") @login_required @students_only +@only_before_deadline_for_students def shortlist_project(project_id): if Project.query.get(project_id): shortlisting = Shortlisting( @@ -83,6 +85,7 @@ def shortlist_project(project_id): @bp.route("/projects/<project_id>/unshortlist") @login_required @students_only +@only_before_deadline_for_students def unshortlist_project(project_id): if Project.query.get(project_id): shortlist: Shortlisting = (