2022-07-03 18:26:15 +02:00
"""
Functions dependent on advlabdb . models .
"""
2022-05-16 22:20:36 +02:00
2022-06-27 00:19:59 +02:00
from functools import cache
2022-09-24 16:18:12 +02:00
from flask import flash
2022-08-15 22:22:36 +02:00
from flask_login import current_user
2022-09-11 20:05:09 +02:00
from markupsafe import Markup
2022-06-27 00:19:59 +02:00
from wtforms . fields import BooleanField , IntegerField , SelectField , StringField
from wtforms . validators import DataRequired , NumberRange , Optional
2021-04-26 21:31:15 +02:00
2022-06-17 20:56:14 +02:00
from . models import MAX_MARK , MIN_MARK , Semester
2021-03-19 13:41:52 +01:00
2021-05-17 22:36:24 +02:00
2022-06-01 22:53:23 +02:00
def active_semester_str ( ) :
2022-05-17 01:28:50 +02:00
active_semester = current_user . active_semester
2022-06-01 22:53:23 +02:00
active_semester_str = str ( active_semester )
2022-05-29 21:47:23 +02:00
if active_semester != Semester . lastSemester ( ) :
2022-09-11 20:05:09 +02:00
flash (
Markup (
2022-09-21 16:52:04 +02: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 20:05:09 +02:00
) ,
" warning " ,
)
2022-05-17 01:28:50 +02:00
2022-06-01 22:53:23 +02:00
return active_semester_str
2022-06-17 20:56:14 +02:00
2022-06-27 00:19:59 +02:00
def mark_field ( mark_type : str ) :
2022-06-17 20:56:14 +02:00
return IntegerField (
mark_type + " Mark " ,
validators = [ Optional ( ) , NumberRange ( MIN_MARK , MAX_MARK ) ] ,
description = f " Between { MIN_MARK } and { MAX_MARK } . " ,
)
2022-06-27 00:19:59 +02:00
@cache
def selection_mark_field_choices ( ) :
choices = [ ( mark , str ( mark ) ) for mark in range ( MAX_MARK , MIN_MARK - 1 , - 1 ) ]
2022-06-27 22:09:17 +02:00
choices . insert ( 0 , ( - 1 , " Not set yet " ) )
2022-06-27 00:19:59 +02:00
return choices
def selection_mark_field ( mark_type : str , default ) :
choices = selection_mark_field_choices ( )
2022-07-03 17:11:33 +02:00
if default is None :
2022-06-27 00:19:59 +02: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 20:56:14 +02: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 ,
)