from random import choice from string import ascii_letters, digits from flask import flash from flask_security import current_user from advlabdb import db from advlabdb.models import Semester def randomPassword(): return "".join(choice(ascii_letters + digits) for i in range(12)) def userActiveSemester(flashWarning=False): lastSemesterId = ( Semester.query.order_by(Semester.year).order_by(Semester.id.desc()).first().id ) # TODO: Check orders if current_user.active_semester_id is None: current_user.active_semester_id = lastSemesterId db.session.commit() elif current_user.active_semester_id != lastSemesterId: activeSemester = Semester.query.get(current_user.active_semester_id) if activeSemester: if flashWarning: flash(f"You are in the old semester {activeSemester.repr()}!", "warning") else: current_user.active_semester_id = lastSemesterId db.session.commit() flash("Semester changed!", "warning") return Semester.query.get(current_user.active_semester_id) def setUserActiveSemester(semesterId): if current_user.active_semester_id != semesterId: semester = Semester.query.get(semesterId) if semester: try: current_user.active_semester_id = semesterId db.session.commit() except Exception as ex: flash(str(ex), "error") db.session.rollback() else: flash(f"Active semester changed to {semester.repr()}!", "warning") else: flash(f"No semester with ID {semesterId}!", "error")