diff --git a/app.py b/app.py index b53d248..2702a64 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,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() - return render_template("home.html", user=user, courses=courses_teacher) + 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 3072d1f..597016c 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 @@ -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,10 @@ 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 = course.evaluations + + 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 +266,38 @@ def add_duplicate_course(): return redirect(url_for('course.course_info', course_id=course_id, year=year)) +@course_bp.route('/evaluation/') +@login_required +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 -def evaluations(user_id, current_year): +@check_access_level(Role.RESEARCHER) +def user_evaluation(user_id, current_year): courses = db.session.query(Course).filter_by(year=current_year).all() - return render_template('evaluations.html', courses=courses, current_year=current_year, user_id=user_id) + return render_template('evaluations.html', courses=courses, current_year=current_year, user_id=user_id, + evaluation=None) @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 +313,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,11 +324,12 @@ 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: 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/db.py b/db.py index 1eedabf..a0d718d 100644 --- a/db.py +++ b/db.py @@ -175,17 +175,24 @@ 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(200)) 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('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 27ea179..84d4d30 100644 --- a/templates/course_info.html +++ b/templates/course_info.html @@ -43,42 +43,70 @@

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

History

-
+
+
+

History

+
- - + + - -
- -
- - - - - - - - - -
Last nameFirst name
-
- -
- -
-

Content for evaluations will go here.

+ +
+ +
+ + + + + + + + + +
Last nameFirst name
+
+ +
+ + + + + + + + + + {% if evaluations is none %} + + + + {% endif %} + {% for evaluation in evaluations %} + + + + + {% endfor %} + +
Course nameYear
No evaluations for this year.
+ {{ evaluation.course.code }} - {{ evaluation.course.title }} + {{ evaluation.course.year }}
+
+
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..3d70d1c 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 %}
    -{% block additionalfooter %} - -{% endblock %} +{% block additionalfooter %}{% endblock %}