2022-07-03 16:26:15 +00:00
"""
Functions dependent on advlabdb . models .
"""
2022-05-16 20:20:36 +00:00
2022-06-26 22:19:59 +00:00
from functools import cache
2022-09-24 14:18:12 +00:00
from flask import flash
2022-08-15 20:22:36 +00:00
from flask_login import current_user
2022-09-11 18:05:09 +00:00
from markupsafe import Markup
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-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
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-09-11 18:05:09 +00:00
flash (
Markup (
2022-09-21 14:52:04 +00:00
f " You are in the old semester { active_semester_str } ! You should change your active semester in <a href= ' /user-settings ' >user settings</a>. "
2022-09-11 18:05:09 +00:00
) ,
" 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 ( )
2022-07-03 15:11:33 +00:00
if default is None :
2022-06-26 22:19:59 +00:00
default = - 1
return SelectField (
mark_type + " Mark " ,
default = default ,
choices = choices ,
validators = [ DataRequired ( ) ] ,
)
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 ,
)