2019-01-27 13:17:17 +01:00
|
|
|
from typing import Any, Dict, List
|
2017-08-30 00:07:54 +02:00
|
|
|
|
2019-01-26 20:37:49 +01:00
|
|
|
from ..utils.projector import (
|
|
|
|
AllData,
|
|
|
|
ProjectorElementException,
|
|
|
|
register_projector_slide,
|
|
|
|
)
|
2015-02-18 01:45:39 +01:00
|
|
|
|
|
|
|
|
2018-12-23 11:05:38 +01:00
|
|
|
# Important: All functions have to be prune. This means, that thay can only
|
|
|
|
# access the data, that they get as argument and do not have any
|
|
|
|
# side effects. They are called from an async context. So they have
|
|
|
|
# to be fast!
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2015-02-18 01:45:39 +01:00
|
|
|
|
2019-01-27 13:17:17 +01:00
|
|
|
def user_slide(all_data: AllData, element: Dict[str, Any]) -> Dict[str, Any]:
|
2018-12-23 11:05:38 +01:00
|
|
|
"""
|
|
|
|
User slide.
|
2019-01-26 20:37:49 +01:00
|
|
|
|
|
|
|
The returned dict can contain the following fields:
|
|
|
|
* user
|
2018-12-23 11:05:38 +01:00
|
|
|
"""
|
2019-01-26 20:37:49 +01:00
|
|
|
user_id = element.get("id")
|
|
|
|
|
|
|
|
if user_id is None:
|
2019-01-31 10:32:32 +01:00
|
|
|
raise ProjectorElementException("id is required for user slide")
|
2019-01-26 20:37:49 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
user = all_data["users/user"][user_id]
|
|
|
|
except KeyError:
|
|
|
|
raise ProjectorElementException(f"user with id {user_id} does not exist")
|
|
|
|
|
2019-01-31 10:32:32 +01:00
|
|
|
return {"user": get_user_name(all_data, user["id"])}
|
2015-06-12 21:08:57 +02:00
|
|
|
|
2017-08-30 00:07:54 +02:00
|
|
|
|
2019-01-27 13:17:17 +01:00
|
|
|
def get_user_name(all_data: AllData, user_id: int) -> str:
|
|
|
|
"""
|
|
|
|
Returns the short name for an user_id.
|
|
|
|
"""
|
|
|
|
user = all_data["users/user"][user_id]
|
|
|
|
name_parts: List[str] = []
|
|
|
|
for name_part in ("title", "first_name", "last_name"):
|
|
|
|
if user[name_part]:
|
|
|
|
name_parts.append(user[name_part])
|
|
|
|
if not name_part:
|
|
|
|
name_parts.append(user["username"])
|
|
|
|
if user["structure_level"]:
|
|
|
|
name_parts.append(f"({user['structure_level']})")
|
|
|
|
return " ".join(name_parts)
|
|
|
|
|
|
|
|
|
|
|
|
def register_projector_slides() -> None:
|
|
|
|
register_projector_slide("users/user", user_slide)
|