Skip to content

Commit

Permalink
Addition of requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelVch98 committed Oct 4, 2024
1 parent c4087db commit ca74ecd
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 34 deletions.
24 changes: 18 additions & 6 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,18 +18,21 @@ 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)
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"))


# 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
Expand All @@ -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"))
30 changes: 13 additions & 17 deletions course.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ def add_course():
raise e


@course_bp.route('/courses/<int:current_year>')
@course_bp.route('/courses/<int:year>')
@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')
Expand All @@ -131,12 +131,11 @@ def search_teachers():
return jsonify(results)


@course_bp.route('<int:course_id>')
@course_bp.route('<int:course_id>/<int:year>')
@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)

Expand All @@ -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)


Expand Down Expand Up @@ -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/<int:course_id>/<int:year>')
@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)


Expand Down Expand Up @@ -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/<int:user_id>/<int:current_year>')
Expand Down
4 changes: 2 additions & 2 deletions templates/courses.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ <h4>Courses</h4>
{% if session.is_admin == False %} disabled {% endif %}>
{% for config in configurations %}
<option value="{{ config.year }}" {% if config.year == current_year %}
selected {% endif %} data-redirect="{{ url_for('course.courses', current_year=config.year) }}">
selected {% endif %} data-redirect="{{ url_for('course.courses', year=config.year) }}">
{{ config.year }}-{{ config.year + 1 }}
</option>
{% endfor %}
Expand All @@ -52,7 +52,7 @@ <h4>Courses</h4>
<tr class="course-item"
data-organizations="{{ course.organizations | map(attribute='id') | join(',') }}">
<td>
<a href="{{ url_for("course.course_info", course_id=course.id, current_year=current_year) }}">
<a href="{{ url_for("course.course_info", course_id=course.id, year=current_year) }}">
{{ course.code }} - {{ course.title }}
</a>
</td>
Expand Down
17 changes: 13 additions & 4 deletions templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<ul class="dropdown-menu" aria-labelledby="coursesDropdown">
<li>
<a class="dropdown-item" id="courseList"
href="{{ url_for('course.courses', current_year=dynamic_year) }}">Course
href="{{ url_for('course.courses', year=dynamic_year) }}">Course
list</a>
</li>
<li role="separator" class="dropdown-divider"></li>
Expand Down Expand Up @@ -116,9 +116,18 @@
{% endif %}
{% block additionalnav %}{% endblock %}
</ul>
<div class="navbar-text ms-auto me-3">
Logged as <strong>{{ session.first_name }} {{ session.name }}</strong>
<a href="{{ url_for('auth.logout') }}" class="btn btn-outline-danger ms-2">Logout</a>
<div class="d-flex align-items-center ms-auto">
<div class="me-0">
<div class="navbar-text me-2">
<input type="text" class="form-control form-control-sm" id="dynamicYearInput"
value="{{ dynamic_year }} - {{ dynamic_year + 1 }}" style="width: 100px;" disabled>
</div>
</div>

<div class="navbar-text ms-0"> <!-- Ajustez ici -->
Logged as <strong>{{ session.first_name }} {{ session.name }}</strong>
<a href="{{ url_for('auth.logout') }}" class="btn btn-outline-danger ms-2">Logout</a>
</div>
</div>
</div>
</div>
Expand Down
7 changes: 2 additions & 5 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand Down

0 comments on commit ca74ecd

Please sign in to comment.