diff --git a/advlabdb/model_dependent_funs.py b/advlabdb/model_dependent_funs.py index 7574928..0e87188 100644 --- a/advlabdb/model_dependent_funs.py +++ b/advlabdb/model_dependent_funs.py @@ -1,11 +1,13 @@ # Functions dependent on advlabdb.models +from functools import cache + from flask import flash, url_for from flask_admin.menu import MenuLink from flask_security import current_user from sqlalchemy import select -from wtforms.fields import BooleanField, IntegerField, StringField -from wtforms.validators import NumberRange, Optional +from wtforms.fields import BooleanField, IntegerField, SelectField, StringField +from wtforms.validators import DataRequired, NumberRange, Optional from . import app, db from .models import MAX_MARK, MIN_MARK, Semester @@ -44,15 +46,45 @@ def active_semester_str(): return active_semester_str -def mark_field(mark_type: str, default=None): +def mark_field(mark_type: str): return IntegerField( mark_type + " Mark", - default=default, validators=[Optional(), NumberRange(MIN_MARK, MAX_MARK)], description=f"Between {MIN_MARK} and {MAX_MARK}.", ) +@cache +def selection_mark_field_choices(): + choices = [(mark, str(mark)) for mark in range(MAX_MARK, MIN_MARK - 1, -1)] + choices.insert(0, (-1, "Not assigned yet")) + + 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()], + render_kw={"class": "form-control"}, + ) + + +def parse_selection_mark_field(field): + data = int(field.data) + if data == -1: + return None + + return data + + def user_info_fields(): phone_number = StringField( "Phone Number",