diff --git a/config.py b/config.py index 08c3742..0d0f47c 100644 --- a/config.py +++ b/config.py @@ -5,6 +5,7 @@ config_bp = Blueprint('config', __name__) + # This method allows the admin to create a new academic year. @config_bp.route('/new_year', methods=['POST']) @login_required @@ -17,7 +18,8 @@ def new_year(): # Check whether the following year already exists in the database existing_config = Configuration.query.filter_by(year=new_year).first() if existing_config is not None: - return make_response("The year already exists", 500) + flash("The year already exists", "error") + return redirect(url_for("index")) config = Configuration(year=new_year) db.session.add(config) @@ -25,10 +27,12 @@ def new_year(): except Exception as e: db.session.rollback() - raise e + flash("An error occurred while processing your request. Please try again.", "error") + return redirect(url_for("index")) return redirect(url_for("index")) + # This method allows the admin to move on to the next academic year for all users. @config_bp.route('/next_year', methods=['POST']) @login_required @@ -37,16 +41,24 @@ def next_year(): current_year = Configuration.query.filter_by(is_current_year=True).first() try: is_new_year = Configuration.query.filter_by(year=current_year.year + 1).first() + # Check if the following year already exists if is_new_year is None: - return make_response("Please create a new year before moving on to the next one", 500) + # Create the new year entry automatically + is_new_year = Configuration(year=current_year.year + 1, is_current_year=True) + db.session.add(is_new_year) + flash("New year created automatically", "success") + else: + # If the new year exists, just update the current year status + is_new_year.is_current_year = True current_year.is_current_year = False - is_new_year.is_current_year = True db.session.commit() except Exception as e: db.session.rollback() - raise e + flash("An error occurred while processing your request. Please try again.", "error") + return redirect(url_for("index")) + + return redirect(url_for("index")) - return redirect(url_for("index")) \ No newline at end of file diff --git a/course.py b/course.py index 721973d..7cdf432 100644 --- a/course.py +++ b/course.py @@ -110,12 +110,12 @@ def add_course(): raise e -@course_bp.route('/courses/') +@course_bp.route('/courses/') @login_required @check_access_level(Role.ADMIN) -def courses(current_year=None): - courses = db.session.query(Course).filter_by(year=current_year).all() - return render_template('courses.html', courses=courses, current_year=current_year) +def courses(year): + courses = db.session.query(Course).filter_by(year=year).all() + return render_template('courses.html', courses=courses, current_year=year) @course_bp.route('/search_teachers') @@ -131,12 +131,11 @@ def search_teachers(): return jsonify(results) -@course_bp.route('') +@course_bp.route('/') @login_required @check_access_level(Role.ADMIN) -def course_info(course_id): - current_year = int(request.args.get('current_year')) - course = db.session.query(Course).filter(Course.id == course_id, Course.year == current_year).first() +def course_info(course_id, year): + course = db.session.query(Course).filter(Course.id == course_id, Course.year == year).first() if not course: return make_response("Course not found", 404) @@ -148,7 +147,7 @@ def course_info(course_id): Evaluation.course_year < course.year ).all() - return render_template('course_info.html', course=course, all_years=all_years, current_year=current_year, + return render_template('course_info.html', course=course, all_years=all_years, current_year=year, evaluations=evaluations) @@ -213,17 +212,14 @@ def update_course_info(): db.session.commit() except Exception as e: db.session.rollback() - return redirect(url_for("course.course_info", course_id=course_id, current_year=year)) + return redirect(url_for("course.course_info", course_id=course_id, year=year)) -@course_bp.route('/duplicate_course') +@course_bp.route('/duplicate_course//') @login_required @check_access_level(Role.ADMIN) -def duplicate_course(): - course_id = request.args.get('course_id') - course_year = request.args.get('year') - course = db.session.query(Course).filter(Course.id == course_id, Course.year == course_year).first() - +def duplicate_course(course_id, year): + course = db.session.query(Course).filter(Course.id == course_id, Course.year == year).first() return render_template('duplicate_course.html', course=course) @@ -269,7 +265,7 @@ def add_duplicate_course(): except Exception as e: db.session.rollback() - return redirect(url_for('course.course_info', course_id=course_id, current_year=year)) + return redirect(url_for('course.course_info', course_id=course_id, year=year)) ''' @course_bp.route('/evaluations//') diff --git a/templates/courses.html b/templates/courses.html index 386cb99..f574819 100644 --- a/templates/courses.html +++ b/templates/courses.html @@ -39,7 +39,7 @@

Courses

{% if session.is_admin == False %} disabled {% endif %}> {% for config in configurations %} {% endfor %} @@ -52,7 +52,7 @@

Courses

- + {{ course.code }} - {{ course.title }} diff --git a/templates/layout.html b/templates/layout.html index 69ec945..b07b9b7 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -83,7 +83,7 @@ - diff --git a/util.py b/util.py index 32a38f7..a2283f2 100644 --- a/util.py +++ b/util.py @@ -3,11 +3,8 @@ def get_current_year(): - try: - current_year = Configuration.query.filter_by(is_current_year=True).first() - return current_year.year - except Exception as e: - raise e + current_year = Configuration.query.filter_by(is_current_year=True).first() + return current_year.year