OpenSlides/openslides/agenda/projector.py

116 lines
3.6 KiB
Python
Raw Normal View History

2018-12-23 11:05:38 +01:00
from collections import defaultdict
from typing import Any, Dict, List, Tuple
from ..utils.projector import (
AllData,
ProjectorElementException,
register_projector_slide,
)
2015-06-24 23:36:36 +02: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!
2018-12-23 11:05:38 +01:00
def get_tree(
all_data: AllData, parent_id: int = 0
) -> List[Tuple[int, List[Tuple[int, List[Any]]]]]:
"""
Build the item tree from all_data.
2018-12-23 11:05:38 +01:00
Only build the tree from elements unterneath parent_id.
2018-12-23 11:05:38 +01:00
Returns a list of two element tuples where the first element is the item title
and the second a List with children as two element tuples.
2015-06-24 23:36:36 +02:00
"""
2019-01-06 16:22:33 +01:00
2018-12-23 11:05:38 +01:00
# Build a dict from an item_id to all its children
children: Dict[int, List[int]] = defaultdict(list)
for item in sorted(
all_data["agenda/item"].values(), key=lambda item: item["weight"]
):
if item["type"] == 1: # only normal items
children[item["parent_id"] or 0].append(item["id"])
2018-12-23 11:05:38 +01:00
def get_children(
item_ids: List[int]
) -> List[Tuple[int, List[Tuple[int, List[Any]]]]]:
return [
(all_data["agenda/item"][item_id]["title"], get_children(children[item_id]))
for item_id in item_ids
]
2015-06-24 23:36:36 +02:00
2018-12-23 11:05:38 +01:00
return get_children(children[parent_id])
2015-06-24 23:36:36 +02:00
2018-12-23 11:05:38 +01:00
def items_slide(all_data: AllData, element: Dict[str, Any]) -> Dict[str, Any]:
2015-06-24 23:36:36 +02:00
"""
2018-12-23 11:05:38 +01:00
Item list slide.
2019-01-06 16:22:33 +01:00
2018-12-23 11:05:38 +01:00
Returns all root items or all children of an item.
"""
root_item_id = element.get("id") or None
show_tree = element.get("tree") or False
2015-06-24 23:36:36 +02:00
2018-12-23 11:05:38 +01:00
if show_tree:
2019-01-12 23:01:42 +01:00
agenda_items = get_tree(all_data, root_item_id or 0)
2018-12-23 11:05:38 +01:00
else:
2019-01-12 23:01:42 +01:00
agenda_items = []
2018-12-23 11:05:38 +01:00
for item in sorted(
all_data["agenda/item"].values(), key=lambda item: item["weight"]
):
if item["parent_id"] == root_item_id and item["type"] == 1:
2019-01-12 23:01:42 +01:00
agenda_items.append(item["title"])
2015-06-24 23:36:36 +02:00
2019-01-12 23:01:42 +01:00
return {"items": agenda_items}
2016-10-05 18:25:50 +02:00
2016-09-12 11:05:34 +02:00
def list_of_speakers_slide(
all_data: AllData, element: Dict[str, Any]
) -> Dict[str, Any]:
2016-09-12 11:05:34 +02:00
"""
2018-12-23 11:05:38 +01:00
List of speakers slide.
Returns all usernames, that are on the list of speaker of a slide.
2016-10-05 18:25:50 +02:00
"""
item_id = element.get("id") or 0 # item_id 0 means current_list_of_speakers
2018-12-23 11:05:38 +01:00
# TODO: handle item_id == 0
try:
item = all_data["agenda/item"][item_id]
except KeyError:
raise ProjectorElementException(f"Item {item_id} does not exist")
2019-01-06 16:22:33 +01:00
2018-12-23 11:05:38 +01:00
user_ids = []
for speaker in item["speakers"]:
user_ids.append(speaker["user"])
return {"user_ids": user_ids}
def current_list_of_speakers_slide(
all_data: AllData, element: Dict[str, Any]
) -> Dict[str, Any]:
"""
TODO
Note: This data is for all projectors showing this slide, so we cannot give projector-
specific data. The work-around is to make a dict with projector-ids as keys and the
data-per-projector as values. This is not a security concern, because if a person can
see one projector, he is able to see all others, too. Maybe a bit more data..
"""
return {"error": "TODO"}
def register_projector_slides() -> None:
register_projector_slide("agenda/item-list", items_slide)
register_projector_slide("agenda/list-of-speakers", list_of_speakers_slide)
register_projector_slide(
"agenda/current-list-of-speakers", current_list_of_speakers_slide
)
register_projector_slide(
"agenda/current-list-of-speakers-overlay", current_list_of_speakers_slide
)