2022-07-03 16:26:15 +00:00
|
|
|
"""
|
|
|
|
Functions not dependent on advlabdb.
|
|
|
|
"""
|
2022-05-16 20:20:36 +00:00
|
|
|
|
2022-06-30 02:01:42 +00:00
|
|
|
from flask import flash, url_for
|
|
|
|
from markupsafe import Markup, escape
|
2022-05-16 20:20:36 +00:00
|
|
|
|
|
|
|
|
2022-06-28 15:01:42 +00:00
|
|
|
def flashRandomPassword(email: str, password: str):
|
|
|
|
flash(f"New random password for email {email}: {password}", category="warning")
|
2022-05-16 20:20:36 +00:00
|
|
|
|
2022-04-18 16:04:17 +00:00
|
|
|
|
2022-08-15 20:22:36 +00:00
|
|
|
def deep_getattr(object, composed_name: str):
|
2022-06-01 21:56:55 +00:00
|
|
|
names = composed_name.split(".")
|
|
|
|
attr = getattr(object, names[0])
|
|
|
|
for name in names[1:]:
|
|
|
|
attr = getattr(attr, name)
|
|
|
|
|
|
|
|
return attr
|
|
|
|
|
|
|
|
|
2022-08-15 20:22:36 +00:00
|
|
|
def str_without_semester_formatter(view, context, model, name: str):
|
2022-06-01 21:56:55 +00:00
|
|
|
attr = deep_getattr(model, name)
|
2022-06-01 21:15:33 +00:00
|
|
|
|
2022-07-01 16:49:29 +00:00
|
|
|
if attr is None:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
if hasattr(attr, "__iter__") and not isinstance(attr, str):
|
|
|
|
# List of attributes
|
2022-07-02 14:46:02 +00:00
|
|
|
return ", ".join(a.str_without_semester() for a in attr)
|
2022-07-01 16:49:29 +00:00
|
|
|
|
|
|
|
return attr.str_without_semester()
|
2022-06-01 21:56:55 +00:00
|
|
|
|
|
|
|
|
2022-08-15 20:22:36 +00:00
|
|
|
def str_formatter(view, context, model, name: str):
|
2022-06-01 21:56:55 +00:00
|
|
|
attr = deep_getattr(model, name)
|
|
|
|
|
2022-07-01 16:49:29 +00:00
|
|
|
if attr is None:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
if hasattr(attr, "__iter__") and not isinstance(attr, str):
|
|
|
|
# List of attributes
|
2022-07-02 14:46:02 +00:00
|
|
|
return ", ".join(a.str() for a in attr)
|
2022-07-01 16:49:29 +00:00
|
|
|
|
|
|
|
return attr.str()
|
2022-06-30 02:01:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _details_link(url, attr, attr_formatter):
|
|
|
|
return f"<a href='{url}?id={attr.id}'>{escape(attr_formatter(attr))}</a>"
|
|
|
|
|
|
|
|
|
|
|
|
def _default_attr_formatter(attr):
|
|
|
|
return attr
|
|
|
|
|
|
|
|
|
2022-08-15 20:22:36 +00:00
|
|
|
def link_formatter(model, name: str, endpoint, attr_formatter=_default_attr_formatter):
|
2022-06-30 02:01:42 +00:00
|
|
|
attr = deep_getattr(model, name)
|
|
|
|
|
|
|
|
if attr is None:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
url = url_for(f"{endpoint}.details_view")
|
|
|
|
|
|
|
|
if hasattr(attr, "__iter__") and not isinstance(attr, str):
|
|
|
|
# List of attributes
|
|
|
|
links = (_details_link(url, a, attr_formatter) for a in attr)
|
|
|
|
return Markup(", ".join(links))
|
|
|
|
|
|
|
|
# Single attribute
|
|
|
|
return Markup(_details_link(url, attr, attr_formatter))
|
|
|
|
|
|
|
|
|
|
|
|
def link_formatter_factory(endpoint, attr_formatter=_default_attr_formatter):
|
|
|
|
def formatter(view, context, model, name):
|
|
|
|
return link_formatter(model, name, endpoint, attr_formatter)
|
|
|
|
|
|
|
|
return formatter
|
|
|
|
|
|
|
|
|
2022-08-15 20:22:36 +00:00
|
|
|
def email_formatter(view, context, model, name: str):
|
2022-06-30 02:01:42 +00:00
|
|
|
attr = deep_getattr(model, name)
|
|
|
|
|
|
|
|
if attr is None:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
return Markup(f"<a href='mailto:{escape(attr)}'>{escape(attr)}</a>")
|
2022-06-30 17:44:35 +00:00
|
|
|
|
|
|
|
|
2022-08-15 20:22:36 +00:00
|
|
|
def experiment_marks_missing_formatter(view, context, model, name: str):
|
2022-06-30 17:44:35 +00:00
|
|
|
experiment_marks_missing = getattr(model, name)
|
|
|
|
if experiment_marks_missing is True:
|
|
|
|
return Markup("<span style='color: red;'>Yes</span>")
|
|
|
|
|
|
|
|
return "No"
|