From 16bec2f5e1b5b69789435988c578d5c1f3651186 Mon Sep 17 00:00:00 2001 From: Samuel Van Campenhout Date: Sat, 27 Jul 2024 21:43:28 +0200 Subject: [PATCH 01/12] Resolving issues filtering with no organizations --- course.py | 14 ++++++++++++++ static/scripts/util.js | 14 +++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/course.py b/course.py index 36da38c..3da9bc3 100644 --- a/course.py +++ b/course.py @@ -65,6 +65,11 @@ def assign_teachers_to_course(course_id, course_year, assigned_teachers): raise e +def count_org(orgs): + if len(orgs) == 0: + return "Please select at least one organization", 400 + + @course_bp.route('/add_course', methods=['POST', 'GET']) @login_required @check_access_level(Role.ADMIN) @@ -87,6 +92,9 @@ def add_course(): language = request.form['language'] organization_ids = request.form.getlist('organization_code[]') + check_orgs = count_org(organization_ids) + if check_orgs: + return make_response(*check_orgs) try: is_course = db.session.query(Course).filter(Course.code == code, @@ -167,6 +175,9 @@ def update_course_info(): nbr_monitor_students = request.form['nbr_monitor_students'] assigned_teachers = request.form.getlist('assigned_teachers[]') organisation_code = request.form.getlist('organization_code[]') + check_orgs = count_org(organisation_code) + if check_orgs: + return make_response(*check_orgs) course = db.session.query(Course).filter(Course.id == course_id, Course.year == year).first() if not course: @@ -239,6 +250,9 @@ def add_duplicate_course(): course_id = request.form['course_id'] assigned_teachers = request.form.getlist('assigned_teachers[]') organisation_code = request.form.getlist('organization_code[]') + check_orgs = count_org(organisation_code) + if check_orgs: + return make_response(*check_orgs) try: duplicate_course = Course(id=course_id, code=code, title=title, quadri=quadri, year=year, diff --git a/static/scripts/util.js b/static/scripts/util.js index 6856b2e..e0b7cec 100644 --- a/static/scripts/util.js +++ b/static/scripts/util.js @@ -75,18 +75,10 @@ function filter(page) { if (activeOrganizations.length > 0) { items.each(function () { - let organizations; - let showItem; - - if (page === "course") { - organizations = $(this).data('organizations').toString().split(','); - showItem = activeOrganizations.some(org => organizations.includes(org.toString())); - } - else if (page === "user") { - organizations = $(this).data('organizations'); - showItem = activeOrganizations.includes(organizations); - } + let organizations = $(this).data('organizations').toString().split(','); + let showItem = activeOrganizations.some(org => organizations.includes(org.toString())); + // Show or hide the item based on whether it belongs to any active organization if (showItem) { $(this).show(); } else { From 585ace3daa0cfc7793086573913faff95df46685 Mon Sep 17 00:00:00 2001 From: Samuel Van Campenhout Date: Mon, 29 Jul 2024 20:22:54 +0200 Subject: [PATCH 02/12] use toast to display errors --- course.py | 21 ++++++++++----------- templates/add_course.html | 1 + templates/course_template.html | 1 + templates/toast_check_orgs.html | 29 +++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 templates/toast_check_orgs.html diff --git a/course.py b/course.py index 3da9bc3..fe0a969 100644 --- a/course.py +++ b/course.py @@ -66,8 +66,7 @@ def assign_teachers_to_course(course_id, course_year, assigned_teachers): def count_org(orgs): - if len(orgs) == 0: - return "Please select at least one organization", 400 + return len(orgs) == 0 @course_bp.route('/add_course', methods=['POST', 'GET']) @@ -92,9 +91,9 @@ def add_course(): language = request.form['language'] organization_ids = request.form.getlist('organization_code[]') - check_orgs = count_org(organization_ids) - if check_orgs: - return make_response(*check_orgs) + if count_org(organization_ids): + flash("Please select at least one organization", "danger") + return redirect(url_for('course.add_course')) try: is_course = db.session.query(Course).filter(Course.code == code, @@ -175,9 +174,9 @@ def update_course_info(): nbr_monitor_students = request.form['nbr_monitor_students'] assigned_teachers = request.form.getlist('assigned_teachers[]') organisation_code = request.form.getlist('organization_code[]') - check_orgs = count_org(organisation_code) - if check_orgs: - return make_response(*check_orgs) + if count_org(organisation_code): + flash("Please select at least one organization", "danger") + return redirect(url_for('course.course_info', course_id=course_id)) course = db.session.query(Course).filter(Course.id == course_id, Course.year == year).first() if not course: @@ -250,9 +249,9 @@ def add_duplicate_course(): course_id = request.form['course_id'] assigned_teachers = request.form.getlist('assigned_teachers[]') organisation_code = request.form.getlist('organization_code[]') - check_orgs = count_org(organisation_code) - if check_orgs: - return make_response(*check_orgs) + if count_org(organisation_code): + flash("Please select at least one organization", "danger") + return redirect(url_for('course.duplicate_course', course_id=course_id, year=year)) try: duplicate_course = Course(id=course_id, code=code, title=title, quadri=quadri, year=year, diff --git a/templates/add_course.html b/templates/add_course.html index 4b45b11..5e3cf8f 100644 --- a/templates/add_course.html +++ b/templates/add_course.html @@ -13,6 +13,7 @@ {% endblock %} {% block pagecontent %} + {% include "toast_check_orgs.html" %}

New Course

diff --git a/templates/course_template.html b/templates/course_template.html index e8fa3ac..dedc6ca 100644 --- a/templates/course_template.html +++ b/templates/course_template.html @@ -14,6 +14,7 @@ {% endblock %} {% block pagecontent %} + {% include "toast_check_orgs.html" %}

diff --git a/templates/toast_check_orgs.html b/templates/toast_check_orgs.html new file mode 100644 index 0000000..c0949cf --- /dev/null +++ b/templates/toast_check_orgs.html @@ -0,0 +1,29 @@ +
+ {% with messages = get_flashed_messages(with_categories=true) %} + {% if messages %} + + {% endif %} + {% endwith %} +
+{% block additionalfooter %} + +{% endblock %} \ No newline at end of file From eada3ca914b062e131dc6f40349d43bc0d8aed0d Mon Sep 17 00:00:00 2001 From: Samuel Van Campenhout Date: Tue, 30 Jul 2024 13:07:59 +0200 Subject: [PATCH 03/12] Fix small issues --- course.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/course.py b/course.py index fe0a969..ead2686 100644 --- a/course.py +++ b/course.py @@ -65,10 +65,6 @@ def assign_teachers_to_course(course_id, course_year, assigned_teachers): raise e -def count_org(orgs): - return len(orgs) == 0 - - @course_bp.route('/add_course', methods=['POST', 'GET']) @login_required @check_access_level(Role.ADMIN) @@ -91,7 +87,7 @@ def add_course(): language = request.form['language'] organization_ids = request.form.getlist('organization_code[]') - if count_org(organization_ids): + if not len(organization_ids): flash("Please select at least one organization", "danger") return redirect(url_for('course.add_course')) @@ -174,7 +170,7 @@ def update_course_info(): nbr_monitor_students = request.form['nbr_monitor_students'] assigned_teachers = request.form.getlist('assigned_teachers[]') organisation_code = request.form.getlist('organization_code[]') - if count_org(organisation_code): + if not len(organisation_code): flash("Please select at least one organization", "danger") return redirect(url_for('course.course_info', course_id=course_id)) @@ -249,7 +245,7 @@ def add_duplicate_course(): course_id = request.form['course_id'] assigned_teachers = request.form.getlist('assigned_teachers[]') organisation_code = request.form.getlist('organization_code[]') - if count_org(organisation_code): + if not len(organisation_code): flash("Please select at least one organization", "danger") return redirect(url_for('course.duplicate_course', course_id=course_id, year=year)) From 7cae21838595e6a277b0edc179ca2230cab9f456 Mon Sep 17 00:00:00 2001 From: Samuel Van Campenhout Date: Tue, 30 Jul 2024 15:22:27 +0200 Subject: [PATCH 04/12] create unique file for toast and use it --- templates/add_course.html | 2 +- templates/course_template.html | 2 +- templates/evaluations.html | 28 +--------------------------- templates/home.html | 24 +----------------------- templates/toast.html | 27 +++++++++++++++++++++++++++ templates/toast_check_orgs.html | 29 ----------------------------- 6 files changed, 31 insertions(+), 81 deletions(-) create mode 100644 templates/toast.html delete mode 100644 templates/toast_check_orgs.html diff --git a/templates/add_course.html b/templates/add_course.html index 5e3cf8f..481f3db 100644 --- a/templates/add_course.html +++ b/templates/add_course.html @@ -13,7 +13,7 @@ {% endblock %} {% block pagecontent %} - {% include "toast_check_orgs.html" %} + {% include "toast.html" %}

New Course

diff --git a/templates/course_template.html b/templates/course_template.html index dedc6ca..1438b82 100644 --- a/templates/course_template.html +++ b/templates/course_template.html @@ -14,7 +14,7 @@ {% endblock %} {% block pagecontent %} - {% include "toast_check_orgs.html" %} + {% include "toast.html" %}

diff --git a/templates/evaluations.html b/templates/evaluations.html index 5bd3202..099a115 100644 --- a/templates/evaluations.html +++ b/templates/evaluations.html @@ -1,26 +1,7 @@ {% extends "layout.html" %} {% block pagetitle %}Evaluation form{% endblock %} {% block pagecontent %} -
-
- {% with messages = get_flashed_messages(with_categories=true) %} - {% if messages %} - {% for category, message in messages %} - - {% endfor %} - {% endif %} - {% endwith %} -
-
+ {% include "toast.html" %}

Evaluation form

@@ -103,11 +84,4 @@

Focus on your (first) course of the semester

{% endblock %} -{% block additionalfooter %} - -{% endblock %} diff --git a/templates/home.html b/templates/home.html index 1997232..5229658 100644 --- a/templates/home.html +++ b/templates/home.html @@ -2,22 +2,7 @@ {% block pagetitle %}Home Page{% endblock %} {% block pagecontent %}
-
- {% with messages = get_flashed_messages(with_categories=true) %} - {% if messages %} - {% for category, message in messages %} -
-
- Notification -
-
- {{ message }} -
-
- {% endfor %} - {% endif %} - {% endwith %} -
+ {% include "toast.html" %}

{{ session.first_name }} {{ session.name }}


{% if user.is_researcher %} @@ -113,11 +98,4 @@

Course assignments for my researchers

{% endif %} -{% endblock %} -{% block additionalfooter %} - {% endblock %} \ No newline at end of file diff --git a/templates/toast.html b/templates/toast.html new file mode 100644 index 0000000..d745e2b --- /dev/null +++ b/templates/toast.html @@ -0,0 +1,27 @@ +
+
+ {% with messages = get_flashed_messages(with_categories=true) %} + {% if messages %} + {% for category, message in messages %} + + {% endfor %} + {% endif %} + {% endwith %} +
+
+{% block additionalfooter %} + +{% endblock %} diff --git a/templates/toast_check_orgs.html b/templates/toast_check_orgs.html deleted file mode 100644 index c0949cf..0000000 --- a/templates/toast_check_orgs.html +++ /dev/null @@ -1,29 +0,0 @@ -
- {% with messages = get_flashed_messages(with_categories=true) %} - {% if messages %} - - {% endif %} - {% endwith %} -
-{% block additionalfooter %} - -{% endblock %} \ No newline at end of file From 1177b1192418adf37c7b666da3963e710343a9b8 Mon Sep 17 00:00:00 2001 From: Samuel Van Campenhout Date: Wed, 9 Oct 2024 13:01:20 +0200 Subject: [PATCH 05/12] indicate the type of user to be displayed --- templates/users.html | 2 +- user.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/templates/users.html b/templates/users.html index 03878d1..6580e3f 100644 --- a/templates/users.html +++ b/templates/users.html @@ -10,7 +10,7 @@ {% endblock %} {% block pagecontent %}
-

List of Users

+

List of {{ list_name }}


{% include 'filter.html' %}
diff --git a/user.py b/user.py index 8c63d4b..7c6e1c5 100644 --- a/user.py +++ b/user.py @@ -80,18 +80,23 @@ def add_user(): @check_access_level(Role.ADMIN) def users(user_type): base_query = db.session.query(User).filter() + list_name = '' if user_type == 'teacher': base_query = base_query.filter(User.is_teacher == True, User.active == True) + list_name = 'Teachers' elif user_type == 'researcher': base_query = base_query.filter(User.is_researcher == True, User.active == True) + list_name = 'Researchers' elif user_type == 'archived': base_query = base_query.filter(User.active == False) + list_name = 'Archived Users' elif user_type == 'other': base_query = base_query.filter(User.active == True, User.is_teacher == False, User.is_researcher == False) + list_name = 'Other Users' all_users = base_query.all() - return render_template('users.html', users=all_users, user_type=user_type) + return render_template('users.html', users=all_users, user_type=user_type, list_name=list_name) def is_allowed_user(user_id): From 9f8b4cc244c992f0f61803c34a912609408febee Mon Sep 17 00:00:00 2001 From: Samuel Van Campenhout Date: Wed, 9 Oct 2024 13:06:15 +0200 Subject: [PATCH 06/12] Fix the bug that prevents you from changing organisation if none was selected --- templates/user_profile.html | 11 +++++------ user.py | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/templates/user_profile.html b/templates/user_profile.html index 366882b..b065e94 100644 --- a/templates/user_profile.html +++ b/templates/user_profile.html @@ -70,13 +70,12 @@

Your informations

disabled {% endif %}> {% if requested_user.organization == None and current_user %} - {% else %} - {% for code in organizations_code %} - - {% endfor %} {% endif %} + {% for code in organizations_code %} + + {% endfor %} diff --git a/user.py b/user.py index 7c6e1c5..a525c7f 100644 --- a/user.py +++ b/user.py @@ -79,7 +79,7 @@ def add_user(): @login_required @check_access_level(Role.ADMIN) def users(user_type): - base_query = db.session.query(User).filter() + base_query = db.session.query(User) list_name = '' if user_type == 'teacher': From 679391738eb6999277cd7a7c2c1d35ec56081e8f Mon Sep 17 00:00:00 2001 From: Samuel Van Campenhout Date: Thu, 3 Oct 2024 20:31:12 +0200 Subject: [PATCH 07/12] =?UTF-8?q?Allow=20researchers=20to=20post=20their?= =?UTF-8?q?=20past=20evaluations=20Display=20past=20evaluations=20on=20the?= =?UTF-8?q?=20courses=20page=20Show=20the=20=E2=80=98Evaluations=E2=80=99?= =?UTF-8?q?=20tab=20only=20if=20you=20are=20a=20researcher?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Conflicts: # app.py # course.py # Conflicts: # app.py --- app.py | 9 ++- course.py | 33 ++++++--- db.py | 12 +++- templates/course_info.html | 94 ++++++++++++++++---------- templates/course_info_template.html | 8 +++ templates/course_template.html | 5 -- templates/evaluations.html | 37 ++++++---- templates/home.html | 100 ++++++++++++++++++---------- templates/layout.html | 10 +-- 9 files changed, 204 insertions(+), 104 deletions(-) diff --git a/app.py b/app.py index b53d248..9f4ad73 100644 --- a/app.py +++ b/app.py @@ -7,7 +7,7 @@ from course import course_bp from config import config_bp from course_preference import course_preference_bp -from db import db, Configuration, Organization, User, Course, Teacher, Researcher +from db import db, Configuration, Organization, User, Course, Teacher, Researcher, Evaluation from decorators import * from flask import Flask, render_template, session, request from enums import * @@ -55,7 +55,12 @@ def index(): # put application's code here current_year = get_current_year() user = db.session.query(User).filter_by(email=session['email']).first() courses_teacher = db.session.query(Course).filter_by(year=current_year).join(Teacher).filter(Teacher.user_id == user.id).all() - return render_template("home.html", user=user, courses=courses_teacher) + current_year = get_current_year() + evaluations = db.session.query(Evaluation).filter( + Evaluation.user_id == user.id, + Evaluation.course_year < current_year + ).all() + return render_template("home.html", user=user, courses=courses_teacher, evaluations=evaluations) if __name__ == '__main__': diff --git a/course.py b/course.py index 3072d1f..6342552 100644 --- a/course.py +++ b/course.py @@ -144,7 +144,13 @@ def course_info(course_id, year): all_years = db.session.query(Course).filter_by(id=course.id).distinct(Course.year).order_by( Course.year.desc()).all() - return render_template('course_info.html', course=course, all_years=all_years, current_year=year) + evaluations = db.session.query(Evaluation).filter( + Evaluation.course_id == course_id, + Evaluation.course_year < course.year + ).all() + + return render_template('course_info.html', course=course, all_years=all_years, current_year=year, + evaluations=evaluations) @course_bp.route('/update_course_info', methods=['POST']) @@ -262,16 +268,27 @@ def add_duplicate_course(): return redirect(url_for('course.course_info', course_id=course_id, year=year)) -@course_bp.route('/evaluations//') +@course_bp.route('/evaluations//', defaults={'evaluation_id': None}) +@course_bp.route('/evaluation/', defaults={'user_id': None, 'current_year': None}) @login_required -def evaluations(user_id, current_year): - courses = db.session.query(Course).filter_by(year=current_year).all() +def evaluations(user_id=None, current_year=None, evaluation_id=None): - return render_template('evaluations.html', courses=courses, current_year=current_year, user_id=user_id) + evaluation = db.session.query(Evaluation).get(evaluation_id) \ + if evaluation_id else None + year = evaluation.course_year if evaluation else current_year + courses = db.session.query(Course).filter_by(year=year).all() + if evaluation: + return render_template('evaluations.html', evaluation=evaluation, current_year=evaluation.course_year, + courses=courses) + + if user_id and current_year: + return render_template('evaluations.html', courses=courses, current_year=current_year, user_id=user_id, + evaluation=evaluation) @course_bp.route('/create_evaluations//', methods=['POST']) @login_required +@check_access_level(Role.RESEARCHER) def create_evaluation(user_id, current_year): form = request.form if not form: @@ -287,9 +304,6 @@ def create_evaluation(user_id, current_year): if not all([course_id, evaluation_hour, workload, comment is not None]): return make_response("Missing required fields", 400) - if other_task: - tasks.append(other_task) - try: existing_evaluation = db.session.query(Evaluation).filter_by(course_id=course_id, course_year=current_year, user_id=user_id).first() @@ -301,7 +315,8 @@ def create_evaluation(user_id, current_year): flash('Evaluation created successfully!', 'success') new_evaluation = Evaluation(course_id=course_id, course_year=current_year, user_id=user_id, task=tasks, - nbr_hours=evaluation_hour, workload=workload, comment=comment) + other_task=other_task ,nbr_hours=evaluation_hour, workload=workload, + comment=comment) db.session.add(new_evaluation) db.session.commit() except Exception as e: diff --git a/db.py b/db.py index 1eedabf..3830182 100644 --- a/db.py +++ b/db.py @@ -175,17 +175,23 @@ class CourseOrganization(db.Model): class Evaluation(db.Model): __tablename__ = 'evaluation' - course_id = db.Column(db.Integer, primary_key=True) - course_year = db.Column(db.Integer, primary_key=True) - user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True) + id = db.Column(db.Integer, primary_key=True) + course_id = db.Column(db.Integer, nullable=False) + course_year = db.Column(db.Integer, nullable=False) + user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) task = db.Column(db.JSON, nullable=False) + other_task = db.Column(db.String(100)) nbr_hours = db.Column(db.String(10), nullable=False) workload = db.Column(db.String(10), nullable=False) comment = db.Column(db.String(500)) __table_args__ = ( + db.UniqueConstraint('course_id', 'course_year', 'user_id', name='unique_course_year_user'), db.ForeignKeyConstraint( ['course_id', 'course_year'], ['course.id', 'course.year'] ), ) + + course = db.relationship('Course', backref=db.backref('course_evaluation', lazy=True)) + user = db.relationship('User', backref=db.backref('user_evaluation', lazy=True)) diff --git a/templates/course_info.html b/templates/course_info.html index 27ea179..3e7eae2 100644 --- a/templates/course_info.html +++ b/templates/course_info.html @@ -43,42 +43,68 @@

Assistant(s) for this year ({{ course.year }} - {{ course
-

History

-
+
+
+

History

+
- - + + + + +
+ +
+ + + + + + + + + +
Last nameFirst name
+
+ +
+ + + + + + + + + + + {% for evaluation in evaluations %} + + + + + + {% endfor %} + +
Course nameYearUser
+ {{ evaluation.course.code }} - {{ evaluation.course.title }} + {{ evaluation.course.year }}{{ evaluation.user.name }} {{ evaluation.user.first_name }}
+
+
- -
- -
- - - - - - - - - -
Last nameFirst name
-
- -
- -
-

Content for evaluations will go here.

diff --git a/templates/course_info_template.html b/templates/course_info_template.html index 5689b78..1ac8ab7 100644 --- a/templates/course_info_template.html +++ b/templates/course_info_template.html @@ -1,3 +1,11 @@ +{% block additionalpageheader %} + +{% endblock %}
diff --git a/templates/course_template.html b/templates/course_template.html index 1438b82..3acf1be 100644 --- a/templates/course_template.html +++ b/templates/course_template.html @@ -2,11 +2,6 @@ {% block pagetitle %}Course informations{% endblock %} {% block additionalpageheader %} +{% endblock %} {% block pagecontent %}
{% include "toast.html" %} @@ -21,43 +29,65 @@

Course(s) assigned this year ({{ dynamic_year }} - {{ dyn
-

History

-
+
+
+

History

+
- - + + - -
- -
- - - - - - - - - - -
CodeTitleYear
-
- -
- -
-

Content for evaluations will go here.

+ +
+ +
+ + + + + + + + + + +
CodeTitleYear
+
+ +
+ + + + + + + + + + {% for evaluation in evaluations %} + + + + + {% endfor %} + +
Course nameYear
+ {{ evaluation.course.code }} - {{ evaluation.course.title }} + {{ evaluation.course.year }}
+
+
diff --git a/templates/layout.html b/templates/layout.html index 0e4afbf..56832bb 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -39,10 +39,12 @@ href="{{ url_for('user.user_profile', user_id=session.user_id, current_year=dynamic_year) }}">My Profile -
  • - Evaluations -
  • + {% if is_researcher %} +
  • + Evaluations +
  • + {% endif %} {% if session.is_admin %}
    From 7c85c50a4234ff392a99fbad2f5a274cafecb28f Mon Sep 17 00:00:00 2001 From: Samuel Van Campenhout Date: Tue, 15 Oct 2024 17:57:54 +0200 Subject: [PATCH 10/12] fix small bug --- course.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/course.py b/course.py index 2ed9977..69ae3fe 100644 --- a/course.py +++ b/course.py @@ -104,7 +104,7 @@ def add_course(): db.session.add(new_course) db.session.commit() - return redirect(url_for("course.courses", current_year=year)) + return redirect(url_for("course.courses", year=year)) except Exception as e: db.session.rollback() raise e From 33b063f4069d70cfa758b4942fa7bb156d4c8460 Mon Sep 17 00:00:00 2001 From: Samuel Van Campenhout Date: Thu, 17 Oct 2024 17:00:18 +0200 Subject: [PATCH 11/12] apply requested changes --- app.py | 7 +------ course.py | 31 ++++++++++++++++--------------- db.py | 7 ++++--- templates/course_info.html | 16 +++++++--------- templates/home.html | 2 +- templates/layout.html | 2 +- 6 files changed, 30 insertions(+), 35 deletions(-) diff --git a/app.py b/app.py index 9f4ad73..2702a64 100644 --- a/app.py +++ b/app.py @@ -55,12 +55,7 @@ def index(): # put application's code here current_year = get_current_year() user = db.session.query(User).filter_by(email=session['email']).first() courses_teacher = db.session.query(Course).filter_by(year=current_year).join(Teacher).filter(Teacher.user_id == user.id).all() - current_year = get_current_year() - evaluations = db.session.query(Evaluation).filter( - Evaluation.user_id == user.id, - Evaluation.course_year < current_year - ).all() - return render_template("home.html", user=user, courses=courses_teacher, evaluations=evaluations) + return render_template("home.html", user=user, courses=courses_teacher, evaluations=user.evaluations) if __name__ == '__main__': diff --git a/course.py b/course.py index 69ae3fe..3756e83 100644 --- a/course.py +++ b/course.py @@ -144,8 +144,7 @@ def course_info(course_id, year): all_years = db.session.query(Course).filter_by(id=course.id).distinct(Course.year).order_by( Course.year.desc()).all() - evaluations = db.session.query(Evaluation).filter(Evaluation.course_id == course_id).order_by( - Evaluation.course_year.desc()).all() + evaluations = db.session.query(Evaluation).filter(Evaluation.course_id == course_id) return render_template('course_info.html', course=course, all_years=all_years, current_year=year, evaluations=evaluations) @@ -266,21 +265,23 @@ def add_duplicate_course(): return redirect(url_for('course.course_info', course_id=course_id, year=year)) -@course_bp.route('/evaluations//', defaults={'evaluation_id': None}) -@course_bp.route('/evaluation/', defaults={'user_id': None, 'current_year': None}) +@course_bp.route('/evaluation/') @login_required -def evaluations(user_id=None, current_year=None, evaluation_id=None): - evaluation = db.session.query(Evaluation).get(evaluation_id) \ - if evaluation_id else None - year = evaluation.course_year if evaluation else current_year - courses = db.session.query(Course).filter_by(year=year).all() - if evaluation: - return render_template('evaluations.html', evaluation=evaluation, current_year=evaluation.course_year, - courses=courses) +def course_evaluation(evaluation_id): + evaluation = db.session.query(Evaluation).get(evaluation_id) + courses = db.session.query(Course).filter_by(year=evaluation.course_year).all() + + return render_template('evaluations.html', evaluation=evaluation, current_year=evaluation.course_year, + courses=courses) + + +@course_bp.route('/evaluations//') +@login_required +def user_evaluation(user_id, current_year): + courses = db.session.query(Course).filter_by(year=current_year).all() - if user_id and current_year: - return render_template('evaluations.html', courses=courses, current_year=current_year, user_id=user_id, - evaluation=evaluation) + return render_template('evaluations.html', courses=courses, current_year=current_year, user_id=user_id, + evaluation=None) @course_bp.route('/create_evaluations//', methods=['POST']) diff --git a/db.py b/db.py index 3830182..a0d718d 100644 --- a/db.py +++ b/db.py @@ -180,7 +180,7 @@ class Evaluation(db.Model): course_year = db.Column(db.Integer, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) task = db.Column(db.JSON, nullable=False) - other_task = db.Column(db.String(100)) + other_task = db.Column(db.String(200)) nbr_hours = db.Column(db.String(10), nullable=False) workload = db.Column(db.String(10), nullable=False) comment = db.Column(db.String(500)) @@ -193,5 +193,6 @@ class Evaluation(db.Model): ), ) - course = db.relationship('Course', backref=db.backref('course_evaluation', lazy=True)) - user = db.relationship('User', backref=db.backref('user_evaluation', lazy=True)) + course = db.relationship('Course', backref=db.backref('evaluations', lazy=True)) + user = db.relationship('User', + backref=db.backref('evaluations', lazy=True, order_by='desc(Evaluation.course_year)')) diff --git a/templates/course_info.html b/templates/course_info.html index 05a5151..6046bf5 100644 --- a/templates/course_info.html +++ b/templates/course_info.html @@ -97,15 +97,13 @@

    History

    {% endif %} {% for evaluation in evaluations %} - {% if evaluation.course_year < course.year %} - - - {{ evaluation.course.code }} - {{ evaluation.course.title }} - - {{ evaluation.course.year }} - {{ evaluation.user.name }} {{ evaluation.user.first_name }} - - {% endif %} + + + {{ evaluation.course.code }} - {{ evaluation.course.title }} + + {{ evaluation.course.year }} + {{ evaluation.user.name }} {{ evaluation.user.first_name }} + {% endfor %} diff --git a/templates/home.html b/templates/home.html index 735a72a..b4b4055 100644 --- a/templates/home.html +++ b/templates/home.html @@ -78,7 +78,7 @@

    History

    {% for evaluation in evaluations %} - + {{ evaluation.course.code }} - {{ evaluation.course.title }} {{ evaluation.course.year }} diff --git a/templates/layout.html b/templates/layout.html index 95e111a..3d70d1c 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -42,7 +42,7 @@ {% if is_researcher %}
  • Evaluations + href="{{ url_for('course.user_evaluation', user_id=session.user_id, current_year=dynamic_year) }}">Evaluations
  • {% endif %} {% if session.is_admin %} From c912e97a31c810930ea55c183ef97f92fb29631b Mon Sep 17 00:00:00 2001 From: Samuel Van Campenhout Date: Tue, 22 Oct 2024 14:45:34 +0200 Subject: [PATCH 12/12] fix requested changes --- course.py | 17 ++++++++++++++--- templates/course_info.html | 2 -- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/course.py b/course.py index 3756e83..597016c 100644 --- a/course.py +++ b/course.py @@ -125,7 +125,8 @@ def search_teachers(): if not validate_string_pattern(search_term): return make_response("Invalid search term", 400) - teachers = db.session.query(User).join(Teacher).filter( + teachers = db.session.query(User).filter( + User.is_teacher, User.active == True, User.name.ilike(f'%{search_term}%') ).all() @@ -144,7 +145,7 @@ def course_info(course_id, year): all_years = db.session.query(Course).filter_by(id=course.id).distinct(Course.year).order_by( Course.year.desc()).all() - evaluations = db.session.query(Evaluation).filter(Evaluation.course_id == course_id) + evaluations = course.evaluations return render_template('course_info.html', course=course, all_years=all_years, current_year=year, evaluations=evaluations) @@ -271,12 +272,22 @@ def course_evaluation(evaluation_id): evaluation = db.session.query(Evaluation).get(evaluation_id) courses = db.session.query(Course).filter_by(year=evaluation.course_year).all() + course = evaluation.course + user = db.session.query(User).filter_by(id=session['user_id']).first() + teachers = course.course_teacher + + # Accessible only to the admin, the evaluation creator and the course teachers + if (not user.is_admin) and (user.id != evaluation.user_id) and (user.id not in [teacher.user_id for teacher in teachers]): + flash("You are not allowed to access this page", "danger") + return redirect(url_for('course.courses', course_id=course.id, year=course.year)) + return render_template('evaluations.html', evaluation=evaluation, current_year=evaluation.course_year, courses=courses) @course_bp.route('/evaluations//') @login_required +@check_access_level(Role.RESEARCHER) def user_evaluation(user_id, current_year): courses = db.session.query(Course).filter_by(year=current_year).all() @@ -321,4 +332,4 @@ def create_evaluation(user_id, current_year): db.session.rollback() flash(f'An error occurred: {str(e)}', 'danger') - return redirect(url_for('course.evaluations', user_id=user_id, current_year=current_year)) + return redirect(url_for('course.user_evaluation', user_id=user_id, current_year=current_year)) diff --git a/templates/course_info.html b/templates/course_info.html index 6046bf5..84d4d30 100644 --- a/templates/course_info.html +++ b/templates/course_info.html @@ -87,7 +87,6 @@

    History

    Course name Year - User @@ -102,7 +101,6 @@

    History

    {{ evaluation.course.code }} - {{ evaluation.course.title }} {{ evaluation.course.year }} - {{ evaluation.user.name }} {{ evaluation.user.first_name }} {% endfor %}