2021-07-30 22:12:37 +00:00
from flask import flash , url_for
from flask_admin . menu import MenuLink
2021-06-02 21:43:41 +00:00
from flask_security import current_user
2021-04-26 19:31:15 +00:00
2022-05-08 19:26:25 +00:00
from . import app , db
from . models import Semester
2021-03-19 12:41:52 +00:00
2021-05-17 20:36:24 +00:00
2022-02-23 19:36:29 +00:00
def flashRandomPassword ( password ) :
flash ( f " Random password: { password } " , category = " warning " )
2022-05-16 16:35:42 +00:00
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 ( )
2022-04-12 13:00:12 +00:00
2021-05-17 14:55:04 +00:00
def userActiveSemester ( flashWarning = False ) :
2022-05-16 16:35:42 +00:00
lastSemesterId = lastSemester ( ) . id
2021-04-26 19:31:15 +00:00
if current_user . active_semester_id is None :
current_user . active_semester_id = lastSemesterId
2021-04-27 13:50:03 +00:00
db . session . commit ( )
2021-04-26 19:31:15 +00:00
elif current_user . active_semester_id != lastSemesterId :
2022-05-16 14:56:24 +00:00
activeSemester = db . session . get ( Semester , current_user . active_semester_id )
if activeSemester is not None :
2021-05-17 14:55:04 +00:00
if flashWarning :
2021-07-14 02:25:34 +00:00
flash ( f " You are in the old semester { activeSemester . repr ( ) } ! " , " warning " )
2021-04-26 19:31:15 +00:00
else :
current_user . active_semester_id = lastSemesterId
2021-04-27 13:50:03 +00:00
db . session . commit ( )
flash ( " Semester changed! " , " warning " )
2022-05-16 14:56:24 +00:00
return db . session . get ( Semester , current_user . active_semester_id )
2021-05-18 12:54:30 +00:00
2021-06-25 23:59:21 +00:00
def setUserActiveSemester ( semesterId ) :
2021-07-11 11:48:13 +00:00
if current_user . active_semester_id != semesterId :
2022-05-16 14:56:24 +00:00
semester = db . session . get ( Semester , semesterId )
if semester is not None :
2021-07-11 11:48:13 +00:00
try :
current_user . active_semester_id = semesterId
db . session . commit ( )
except Exception as ex :
flash ( str ( ex ) , " error " )
db . session . rollback ( )
else :
2021-07-14 02:25:34 +00:00
flash ( f " Active semester changed to { semester . repr ( ) } ! " , " warning " )
2021-07-11 11:48:13 +00:00
else :
flash ( f " No semester with ID { semesterId } ! " , " error " )
2021-07-30 22:12:37 +00:00
def initActiveSemesterMenuLinks ( space ) :
with app . app_context ( ) :
2022-05-17 11:24:16 +00:00
try :
2022-05-16 16:37:57 +00:00
semesters = sortedSemestersStartingWithNewest ( )
2022-05-17 11:24:16 +00:00
for semester in semesters :
space . add_link (
MenuLink (
name = semester . repr ( ) ,
url = url_for ( " set_semester " ) + " ?semester_id= " + str ( semester . id ) ,
category = " Active semester " ,
)
2021-07-30 22:12:37 +00:00
)
2022-05-17 11:24:16 +00:00
except Exception as ex :
print (
" ERROR: The Semester table does not exist yet! Therefore, menu links could not be generated. You can ignore this error if you are just initializing the database. "
2021-07-30 22:12:37 +00:00
)
2022-05-17 11:24:16 +00:00
else :
space . add_link ( MenuLink ( name = " Logout " , url = url_for ( " security.logout " ) ) )
2021-11-30 00:36:19 +00:00
def reportBadAttempt ( message ) :
print ( " BAD ATTEMPT: " , message ) # TODO: Log