1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-12-20 23:41:20 +00:00

Add selection_mark_field

This commit is contained in:
Mo 2022-06-27 00:19:59 +02:00
parent 35560622dc
commit 80e7ff731f

View file

@ -1,11 +1,13 @@
# Functions dependent on advlabdb.models # Functions dependent on advlabdb.models
from functools import cache
from flask import flash, url_for from flask import flash, url_for
from flask_admin.menu import MenuLink from flask_admin.menu import MenuLink
from flask_security import current_user from flask_security import current_user
from sqlalchemy import select from sqlalchemy import select
from wtforms.fields import BooleanField, IntegerField, StringField from wtforms.fields import BooleanField, IntegerField, SelectField, StringField
from wtforms.validators import NumberRange, Optional from wtforms.validators import DataRequired, NumberRange, Optional
from . import app, db from . import app, db
from .models import MAX_MARK, MIN_MARK, Semester from .models import MAX_MARK, MIN_MARK, Semester
@ -44,15 +46,45 @@ def active_semester_str():
return active_semester_str return active_semester_str
def mark_field(mark_type: str, default=None): def mark_field(mark_type: str):
return IntegerField( return IntegerField(
mark_type + " Mark", mark_type + " Mark",
default=default,
validators=[Optional(), NumberRange(MIN_MARK, MAX_MARK)], validators=[Optional(), NumberRange(MIN_MARK, MAX_MARK)],
description=f"Between {MIN_MARK} and {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(): def user_info_fields():
phone_number = StringField( phone_number = StringField(
"Phone Number", "Phone Number",