2022-05-16 20:20:36 +00:00
# Functions dependent on advlabdb.models
2022-06-26 22:19:59 +00:00
from functools import cache
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
2022-05-16 20:20:36 +00:00
from sqlalchemy import select
2022-06-26 22:19:59 +00:00
from wtforms . fields import BooleanField , IntegerField , SelectField , StringField
from wtforms . validators import DataRequired , NumberRange , Optional
2021-04-26 19:31:15 +00:00
2022-05-08 19:26:25 +00:00
from . import app , db
2022-06-17 18:56:14 +00:00
from . models import MAX_MARK , MIN_MARK , Semester
2021-03-19 12:41:52 +00:00
2021-05-17 20:36:24 +00:00
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-29 19:47:23 +00:00
semesters = Semester . sortedSemestersStartingWithNewest ( )
2022-05-17 11:24:16 +00:00
for semester in semesters :
space . add_link (
MenuLink (
2022-06-01 21:02:17 +00:00
name = str ( semester ) ,
2022-05-17 11:24:16 +00:00
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
2022-05-16 19:48:04 +00:00
def get_count ( table ) :
return db . session . scalar ( select ( func . count ( ) ) . select_from ( table ) )
2022-05-16 23:28:50 +00:00
2022-06-01 20:53:23 +00:00
def active_semester_str ( ) :
2022-05-16 23:28:50 +00:00
active_semester = current_user . active_semester
2022-06-01 20:53:23 +00:00
active_semester_str = str ( active_semester )
2022-05-29 19:47:23 +00:00
if active_semester != Semester . lastSemester ( ) :
2022-06-01 20:53:23 +00:00
flash ( f " You are in the old semester { active_semester_str } ! " , " warning " )
2022-05-16 23:28:50 +00:00
2022-06-01 20:53:23 +00:00
return active_semester_str
2022-06-17 18:56:14 +00:00
2022-06-26 22:19:59 +00:00
def mark_field ( mark_type : str ) :
2022-06-17 18:56:14 +00:00
return IntegerField (
mark_type + " Mark " ,
validators = [ Optional ( ) , NumberRange ( MIN_MARK , MAX_MARK ) ] ,
description = f " Between { MIN_MARK } and { MAX_MARK } . " ,
)
2022-06-26 22:19:59 +00:00
@cache
def selection_mark_field_choices ( ) :
choices = [ ( mark , str ( mark ) ) for mark in range ( MAX_MARK , MIN_MARK - 1 , - 1 ) ]
2022-06-27 20:09:17 +00:00
choices . insert ( 0 , ( - 1 , " Not set yet " ) )
2022-06-26 22:19:59 +00:00
return choices
def selection_mark_field ( mark_type : str , default ) :
choices = selection_mark_field_choices ( )
if default == None :
default = - 1
return SelectField (
mark_type + " Mark " ,
default = default ,
choices = choices ,
validators = [ DataRequired ( ) ] ,
2022-06-27 20:09:17 +00:00
render_kw = { " class " : " form-control " , " style " : " width:auto; " } ,
2022-06-26 22:19:59 +00:00
)
def parse_selection_mark_field ( field ) :
data = int ( field . data )
if data == - 1 :
return None
return data
2022-06-17 18:56:14 +00:00
def user_info_fields ( ) :
phone_number = StringField (
" Phone Number " ,
validators = [ Optional ( ) ] ,
)
mobile_phone_number = StringField (
" Mobile Phone Number " ,
validators = [ Optional ( ) ] ,
)
building = StringField (
" Building " ,
validators = [ Optional ( ) ] ,
)
room = StringField (
" Room " ,
validators = [ Optional ( ) ] ,
)
return phone_number , mobile_phone_number , building , room
def generate_new_password_field ( ) :
return BooleanField (
" Generate new random password. For security reasons, it is not possible to manually enter a password. Please use a password manager like Bitwarden or KeepassXC to save the randomly generated password. " ,
default = False ,
)