diff --git a/advlabdb/dependent_funs.py b/advlabdb/dependent_funs.py index d4504f4..61ec33c 100644 --- a/advlabdb/dependent_funs.py +++ b/advlabdb/dependent_funs.py @@ -10,12 +10,23 @@ def flashRandomPassword(password): flash(f"Random password: {password}", category="warning") -def sortedSemestersStartingWithNewest(): - return Semester.query.order_by(Semester.year.desc()).order_by(Semester.label.desc()) +def sortedSemestersStartingWithNewest(limit=0): + # Inserting an older semester is not allowed! + # Therefore, the id is enough. + stmt = select(Semester).order_by(Semester.id.desc()) + + if limit > 0: + stmt = stmt.limit(limit) + + return db.session.execute(stmt).scalars() + + +def lastSemester(): + return sortedSemestersStartingWithNewest(limit=1).first() def userActiveSemester(flashWarning=False): - lastSemesterId = sortedSemestersStartingWithNewest().first().id # "WS" > "SS" + lastSemesterId = lastSemester().id if current_user.active_semester_id is None: current_user.active_semester_id = lastSemesterId db.session.commit()