mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-11-08 21:21:06 +00:00
51 lines
1.1 KiB
Python
51 lines
1.1 KiB
Python
# Functions not dependent on advlabdb
|
|
|
|
from flask import flash
|
|
from markupsafe import Markup
|
|
|
|
|
|
def flashRandomPassword(password):
|
|
flash(f"Random password: {password}", category="warning")
|
|
|
|
|
|
def parse_bool(str):
|
|
str_lower = str.lower()
|
|
if str_lower == "false":
|
|
return False
|
|
elif str_lower == "true":
|
|
return True
|
|
else:
|
|
raise ValueError(f'Can not parse a bool from "{str}"')
|
|
|
|
|
|
def deep_getattr(object, composed_name):
|
|
names = composed_name.split(".")
|
|
attr = getattr(object, names[0])
|
|
for name in names[1:]:
|
|
attr = getattr(attr, name)
|
|
|
|
return attr
|
|
|
|
|
|
def missing_formatter(view, context, model, name):
|
|
attr = deep_getattr(model, name)
|
|
if attr is None:
|
|
return Markup("<span style='color:red'>MISSING</span>")
|
|
|
|
return attr
|
|
|
|
|
|
def str_without_semester_formatter(view, context, model, name):
|
|
attr = deep_getattr(model, name)
|
|
if attr is not None:
|
|
return attr.str_without_semester()
|
|
|
|
return attr
|
|
|
|
|
|
def str_formatter(view, context, model, name):
|
|
attr = deep_getattr(model, name)
|
|
if attr is not None:
|
|
return attr.str()
|
|
|
|
return attr
|