-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.py
61 lines (47 loc) · 2.12 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from decorators import login_required, check_access_level
from db import db, Year, Role
from flask import Blueprint, render_template, flash, current_app, url_for, request, make_response, redirect, session, \
Flask
import logging
config_bp = Blueprint('config', __name__)
@config_bp.route('/manage_years', methods=['GET'])
@login_required
@check_access_level(Role.ADMIN)
def manage_years():
configurations = Year.query.order_by(Year.year.desc()).all()
return render_template('config.html', configurations=configurations)
# This method allows the admin to create a new academic year.
@config_bp.route('/new_year', methods=['POST'])
@login_required
@check_access_level(Role.ADMIN)
def new_year():
last_year = Year.query.order_by(Year.year.desc()).first()
new_year = last_year.year + 1
try:
# Check whether the following year already exists in the database
existing_config = Year.query.filter_by(year=new_year).first()
if existing_config is not None:
flash("The year already exists", "error")
return redirect(url_for("index"))
config = Year(year=new_year)
db.session.add(config)
db.session.commit()
flash("Year created successfully", "success")
except Exception as e:
db.session.rollback()
logging.error(f'An error occurred: {str(e)}', exc_einfo=True)
flash(f"An error occurred while creating the year {new_year} - {new_year + 1}.", "error")
return redirect(url_for("config.manage_years"))
# This method allows the admin to move on to the next academic year for all users.
@config_bp.route('/change_year/<int:year>', methods=['POST'])
@login_required
@check_access_level(Role.ADMIN)
def change_year(year):
try:
new_current_year = Year.query.filter_by(year=year).first()
Year.update_current_year(new_current_year.id)
except Exception as e:
db.session.rollback()
logging.error(f'An error occurred: {str(e)}', exc_einfo=True)
flash(f"An error occurred while trying to change the current year to {year} - {year + 1}.", "error")
return redirect(url_for("config.manage_years"))