mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-11-08 21:21:06 +00:00
85 lines
2.6 KiB
Python
85 lines
2.6 KiB
Python
# Functions dependent on advlabdb.models
|
|
|
|
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 . import app, db
|
|
from .models import MAX_MARK, MIN_MARK, Semester
|
|
|
|
|
|
def initActiveSemesterMenuLinks(space):
|
|
with app.app_context():
|
|
try:
|
|
semesters = Semester.sortedSemestersStartingWithNewest()
|
|
for semester in semesters:
|
|
space.add_link(
|
|
MenuLink(
|
|
name=str(semester),
|
|
url=url_for("set_semester") + "?semester_id=" + str(semester.id),
|
|
category="Active semester",
|
|
)
|
|
)
|
|
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."
|
|
)
|
|
else:
|
|
space.add_link(MenuLink(name="Logout", url=url_for("security.logout")))
|
|
|
|
|
|
def reportBadAttempt(message):
|
|
print("BAD ATTEMPT:", message) # TODO: Log
|
|
|
|
|
|
def get_count(table):
|
|
return db.session.scalar(select(func.count()).select_from(table))
|
|
|
|
|
|
def active_semester_str():
|
|
active_semester = current_user.active_semester
|
|
active_semester_str = str(active_semester)
|
|
if active_semester != Semester.lastSemester():
|
|
flash(f"You are in the old semester {active_semester_str}!", "warning")
|
|
|
|
return active_semester_str
|
|
|
|
|
|
def mark_field(mark_type: str):
|
|
return IntegerField(
|
|
mark_type + " Mark",
|
|
validators=[Optional(), NumberRange(MIN_MARK, MAX_MARK)],
|
|
description=f"Between {MIN_MARK} and {MAX_MARK}.",
|
|
)
|
|
|
|
|
|
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,
|
|
)
|