2019-02-26 14:43:51 +01:00
|
|
|
import re
|
2019-06-26 16:16:53 +02:00
|
|
|
from typing import Any, Dict, List, Optional
|
2017-08-30 00:07:54 +02:00
|
|
|
|
2019-01-27 13:17:17 +01:00
|
|
|
from ..users.projector import get_user_name
|
|
|
|
from ..utils.projector import (
|
|
|
|
AllData,
|
|
|
|
ProjectorElementException,
|
|
|
|
get_config,
|
|
|
|
register_projector_slide,
|
|
|
|
)
|
2015-06-24 22:11:54 +02:00
|
|
|
|
|
|
|
|
2019-03-06 16:59:18 +01:00
|
|
|
motion_placeholder_regex = re.compile(r"\[motion:(\d+)\]")
|
|
|
|
|
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
|
2019-03-23 12:06:57 +01:00
|
|
|
# side effects.
|
2015-06-24 22:11:54 +02:00
|
|
|
|
2016-10-01 20:42:44 +02:00
|
|
|
|
2019-03-23 12:06:57 +01:00
|
|
|
async def get_state(
|
2019-05-10 07:07:59 +02:00
|
|
|
all_data: AllData, motion: Dict[str, Any], state_id_key: str
|
2018-12-23 11:05:38 +01:00
|
|
|
) -> Dict[str, Any]:
|
2019-01-19 21:36:30 +01:00
|
|
|
"""
|
2019-07-17 16:13:49 +02:00
|
|
|
Returns a state element from one motion. Raises an error if the state does not exist.
|
2019-01-19 21:36:30 +01:00
|
|
|
"""
|
2019-07-17 16:13:49 +02:00
|
|
|
state = all_data["motions/state"].get(motion[state_id_key])
|
|
|
|
if not state:
|
|
|
|
raise ProjectorElementException(
|
|
|
|
f"motion {motion['id']} can not be on the state with id {motion[state_id_key]}"
|
|
|
|
)
|
|
|
|
return state
|
2019-01-19 21:36:30 +01:00
|
|
|
|
2019-02-14 16:35:25 +01:00
|
|
|
|
2019-05-10 07:07:59 +02:00
|
|
|
async def get_amendment_merge_into_motion_diff(all_data, amendment):
|
2019-02-03 19:30:07 +01:00
|
|
|
"""
|
2019-03-09 15:00:03 +01:00
|
|
|
HINT: This implementation should be consistent to showInDiffView() in ViewMotionAmendedParagraph.ts
|
2019-02-03 19:30:07 +01:00
|
|
|
"""
|
|
|
|
if amendment["state_id"] is None:
|
|
|
|
return 0
|
|
|
|
|
2019-05-10 07:07:59 +02:00
|
|
|
state = await get_state(all_data, amendment, "state_id")
|
2019-03-09 15:00:03 +01:00
|
|
|
if state["merge_amendment_into_final"] == -1:
|
|
|
|
return 0
|
|
|
|
if state["merge_amendment_into_final"] == 1:
|
|
|
|
return 1
|
2019-02-03 19:30:07 +01:00
|
|
|
|
|
|
|
if amendment["recommendation_id"] is None:
|
|
|
|
return 0
|
2019-05-10 07:07:59 +02:00
|
|
|
recommendation = await get_state(all_data, amendment, "recommendation_id")
|
2019-03-09 15:00:03 +01:00
|
|
|
if recommendation["merge_amendment_into_final"] == 1:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2019-05-10 07:07:59 +02:00
|
|
|
async def get_amendment_merge_into_motion_final(all_data, amendment):
|
2019-03-09 15:00:03 +01:00
|
|
|
"""
|
|
|
|
HINT: This implementation should be consistent to showInFinalView() in ViewMotionAmendedParagraph.ts
|
|
|
|
"""
|
|
|
|
if amendment["state_id"] is None:
|
|
|
|
return 0
|
|
|
|
|
2019-05-10 07:07:59 +02:00
|
|
|
state = await get_state(all_data, amendment, "state_id")
|
2019-03-09 15:00:03 +01:00
|
|
|
if state["merge_amendment_into_final"] == 1:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
return 0
|
2019-02-03 19:30:07 +01:00
|
|
|
|
2019-02-14 16:35:25 +01:00
|
|
|
|
2019-03-23 12:06:57 +01:00
|
|
|
async def get_amendments_for_motion(motion, all_data):
|
2019-02-03 19:30:07 +01:00
|
|
|
amendment_data = []
|
|
|
|
for amendment_id, amendment in all_data["motions/motion"].items():
|
|
|
|
if amendment["parent_id"] == motion["id"]:
|
2019-03-23 12:06:57 +01:00
|
|
|
merge_amendment_into_final = await get_amendment_merge_into_motion_final(
|
2019-05-10 07:07:59 +02:00
|
|
|
all_data, amendment
|
2019-03-09 15:00:03 +01:00
|
|
|
)
|
2019-03-23 12:06:57 +01:00
|
|
|
merge_amendment_into_diff = await get_amendment_merge_into_motion_diff(
|
2019-05-10 07:07:59 +02:00
|
|
|
all_data, amendment
|
2019-02-14 16:35:25 +01:00
|
|
|
)
|
|
|
|
amendment_data.append(
|
|
|
|
{
|
|
|
|
"id": amendment["id"],
|
|
|
|
"identifier": amendment["identifier"],
|
|
|
|
"title": amendment["title"],
|
|
|
|
"amendment_paragraphs": amendment["amendment_paragraphs"],
|
2019-03-09 15:00:03 +01:00
|
|
|
"merge_amendment_into_diff": merge_amendment_into_diff,
|
2019-02-14 16:35:25 +01:00
|
|
|
"merge_amendment_into_final": merge_amendment_into_final,
|
|
|
|
}
|
|
|
|
)
|
2019-02-03 19:30:07 +01:00
|
|
|
return amendment_data
|
|
|
|
|
2019-02-14 16:35:25 +01:00
|
|
|
|
2019-03-23 12:06:57 +01:00
|
|
|
async def get_amendment_base_motion(amendment, all_data):
|
2019-02-03 19:30:07 +01:00
|
|
|
try:
|
|
|
|
motion = all_data["motions/motion"][amendment["parent_id"]]
|
|
|
|
except KeyError:
|
|
|
|
motion_id = amendment["parent_id"]
|
|
|
|
raise ProjectorElementException(f"motion with id {motion_id} does not exist")
|
|
|
|
|
|
|
|
return {
|
|
|
|
"identifier": motion["identifier"],
|
|
|
|
"title": motion["title"],
|
|
|
|
"text": motion["text"],
|
|
|
|
}
|
|
|
|
|
2019-02-14 16:35:25 +01:00
|
|
|
|
2019-03-23 12:06:57 +01:00
|
|
|
async def get_amendment_base_statute(amendment, all_data):
|
2019-02-03 19:30:07 +01:00
|
|
|
try:
|
2019-02-14 16:35:25 +01:00
|
|
|
statute = all_data["motions/statute-paragraph"][
|
|
|
|
amendment["statute_paragraph_id"]
|
|
|
|
]
|
2019-02-03 19:30:07 +01:00
|
|
|
except KeyError:
|
|
|
|
statute_id = amendment["statute_paragraph_id"]
|
|
|
|
raise ProjectorElementException(f"statute with id {statute_id} does not exist")
|
|
|
|
|
2019-02-14 16:35:25 +01:00
|
|
|
return {"title": statute["title"], "text": statute["text"]}
|
|
|
|
|
2019-01-19 21:36:30 +01:00
|
|
|
|
2019-03-23 12:06:57 +01:00
|
|
|
async def extend_reference_motion_dict(
|
2019-03-06 16:59:18 +01:00
|
|
|
all_data: AllData,
|
2019-04-29 11:18:40 +02:00
|
|
|
recommendation: Optional[str],
|
2019-03-06 16:59:18 +01:00
|
|
|
referenced_motions: Dict[int, Dict[str, str]],
|
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Extends a dict of motion ids mapped to their title information.
|
|
|
|
The client can replace the placeholders in the recommendation correctly.
|
|
|
|
"""
|
2019-04-29 11:18:40 +02:00
|
|
|
if recommendation is None:
|
|
|
|
return
|
|
|
|
|
2019-03-06 16:59:18 +01:00
|
|
|
# Collect all meantioned motions via [motion:<id>]
|
|
|
|
referenced_ids = [
|
|
|
|
int(id) for id in motion_placeholder_regex.findall(recommendation)
|
|
|
|
]
|
|
|
|
for id in referenced_ids:
|
|
|
|
# Put every referenced motion into the referenced_motions dict
|
|
|
|
if id not in referenced_motions and id in all_data["motions/motion"]:
|
|
|
|
referenced_motions[id] = {
|
|
|
|
"title": all_data["motions/motion"][id]["title"],
|
|
|
|
"identifier": all_data["motions/motion"][id]["identifier"],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-23 12:06:57 +01:00
|
|
|
async def motion_slide(
|
2019-02-21 14:40:07 +01:00
|
|
|
all_data: AllData, element: Dict[str, Any], projector_id: int
|
|
|
|
) -> Dict[str, Any]:
|
2016-10-01 20:42:44 +02:00
|
|
|
"""
|
2018-12-23 11:05:38 +01:00
|
|
|
Motion slide.
|
2019-01-19 21:36:30 +01:00
|
|
|
|
|
|
|
The returned dict can contain the following fields:
|
|
|
|
* identifier
|
|
|
|
* title
|
|
|
|
* text
|
|
|
|
* amendment_paragraphs
|
|
|
|
* is_child
|
|
|
|
* show_meta_box
|
|
|
|
* reason
|
|
|
|
* modified_final_version
|
|
|
|
* recommendation
|
|
|
|
* recommendation_extension
|
2019-01-26 20:37:49 +01:00
|
|
|
* recommender
|
|
|
|
* change_recommendations
|
2019-01-19 21:36:30 +01:00
|
|
|
* submitter
|
2016-10-01 20:42:44 +02:00
|
|
|
"""
|
2019-07-17 16:13:49 +02:00
|
|
|
# Get motion
|
2019-03-23 12:06:57 +01:00
|
|
|
mode = element.get(
|
|
|
|
"mode", await get_config(all_data, "motions_recommendation_text_mode")
|
|
|
|
)
|
2019-01-19 21:36:30 +01:00
|
|
|
motion_id = element.get("id")
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2019-01-19 21:36:30 +01:00
|
|
|
if motion_id is None:
|
2019-02-15 12:01:16 +01:00
|
|
|
raise ProjectorElementException("id is required for motion slide")
|
2016-10-01 20:42:44 +02:00
|
|
|
|
2019-01-19 21:36:30 +01:00
|
|
|
try:
|
|
|
|
motion = all_data["motions/motion"][motion_id]
|
|
|
|
except KeyError:
|
2019-01-27 13:17:17 +01:00
|
|
|
raise ProjectorElementException(f"motion with id {motion_id} does not exist")
|
2019-01-19 21:36:30 +01:00
|
|
|
|
2019-07-17 16:13:49 +02:00
|
|
|
# Get some needed config values
|
2019-03-23 12:06:57 +01:00
|
|
|
show_meta_box = not await get_config(
|
|
|
|
all_data, "motions_disable_sidebox_on_projector"
|
|
|
|
)
|
|
|
|
line_length = await get_config(all_data, "motions_line_length")
|
|
|
|
line_numbering_mode = await get_config(all_data, "motions_default_line_numbering")
|
|
|
|
motions_preamble = await get_config(all_data, "motions_preamble")
|
2019-02-03 19:30:07 +01:00
|
|
|
|
2019-07-17 16:13:49 +02:00
|
|
|
# Query all change-recommendation and amendment related things.
|
|
|
|
change_recommendations = [] # type: ignore
|
|
|
|
amendments = [] # type: ignore
|
|
|
|
base_motion = None
|
|
|
|
base_statute = None
|
2019-02-03 19:30:07 +01:00
|
|
|
if motion["statute_paragraph_id"]:
|
2019-03-23 12:06:57 +01:00
|
|
|
base_statute = await get_amendment_base_statute(motion, all_data)
|
2019-07-17 16:13:49 +02:00
|
|
|
elif motion["parent_id"] is not None and motion["amendment_paragraphs"]:
|
2019-03-23 12:06:57 +01:00
|
|
|
base_motion = await get_amendment_base_motion(motion, all_data)
|
2019-02-03 19:30:07 +01:00
|
|
|
else:
|
2019-07-17 16:13:49 +02:00
|
|
|
for change_recommendation_id in motion["change_recommendations_id"]:
|
|
|
|
cr = all_data["motions/motion-change-recommendation"].get(
|
|
|
|
change_recommendation_id
|
2019-02-14 16:35:25 +01:00
|
|
|
)
|
2019-07-17 16:13:49 +02:00
|
|
|
if cr is not None and not cr["internal"]:
|
|
|
|
change_recommendations.append(cr)
|
2019-03-23 12:06:57 +01:00
|
|
|
amendments = await get_amendments_for_motion(motion, all_data)
|
2019-01-19 21:36:30 +01:00
|
|
|
|
2019-07-17 16:13:49 +02:00
|
|
|
# The base return value. More fields will get added below.
|
2019-01-19 21:36:30 +01:00
|
|
|
return_value = {
|
|
|
|
"identifier": motion["identifier"],
|
|
|
|
"title": motion["title"],
|
2019-02-03 19:30:07 +01:00
|
|
|
"preamble": motions_preamble,
|
2019-01-19 21:36:30 +01:00
|
|
|
"text": motion["text"],
|
|
|
|
"amendment_paragraphs": motion["amendment_paragraphs"],
|
2019-02-03 19:30:07 +01:00
|
|
|
"base_motion": base_motion,
|
|
|
|
"base_statute": base_statute,
|
2019-01-19 21:36:30 +01:00
|
|
|
"is_child": bool(motion["parent_id"]),
|
|
|
|
"show_meta_box": show_meta_box,
|
2019-02-03 19:30:07 +01:00
|
|
|
"change_recommendations": change_recommendations,
|
|
|
|
"amendments": amendments,
|
|
|
|
"line_length": line_length,
|
|
|
|
"line_numbering_mode": line_numbering_mode,
|
2019-01-19 21:36:30 +01:00
|
|
|
}
|
|
|
|
|
2019-03-23 12:06:57 +01:00
|
|
|
if not await get_config(all_data, "motions_disable_reason_on_projector"):
|
2019-01-19 21:36:30 +01:00
|
|
|
return_value["reason"] = motion["reason"]
|
2019-01-27 13:17:17 +01:00
|
|
|
|
2019-01-19 21:36:30 +01:00
|
|
|
if mode == "final":
|
|
|
|
return_value["modified_final_version"] = motion["modified_final_version"]
|
|
|
|
|
|
|
|
if show_meta_box:
|
2019-06-26 16:16:53 +02:00
|
|
|
# Add recommendation, if enabled in config (and the motion has one)
|
2019-01-19 21:36:30 +01:00
|
|
|
if (
|
2019-03-23 12:06:57 +01:00
|
|
|
not await get_config(
|
|
|
|
all_data, "motions_disable_recommendation_on_projector"
|
|
|
|
)
|
2019-01-19 21:36:30 +01:00
|
|
|
and motion["recommendation_id"]
|
|
|
|
):
|
2019-03-23 12:06:57 +01:00
|
|
|
recommendation_state = await get_state(
|
2019-05-10 07:07:59 +02:00
|
|
|
all_data, motion, "recommendation_id"
|
2019-01-27 13:17:17 +01:00
|
|
|
)
|
|
|
|
return_value["recommendation"] = recommendation_state[
|
|
|
|
"recommendation_label"
|
2019-01-19 21:36:30 +01:00
|
|
|
]
|
2019-01-27 13:17:17 +01:00
|
|
|
if recommendation_state["show_recommendation_extension_field"]:
|
2019-03-06 16:59:18 +01:00
|
|
|
recommendation_extension = motion["recommendation_extension"]
|
|
|
|
# All title information for referenced motions in the recommendation
|
|
|
|
referenced_motions: Dict[int, Dict[str, str]] = {}
|
2019-03-23 12:06:57 +01:00
|
|
|
await extend_reference_motion_dict(
|
2019-03-06 16:59:18 +01:00
|
|
|
all_data, recommendation_extension, referenced_motions
|
|
|
|
)
|
|
|
|
return_value["recommendation_extension"] = recommendation_extension
|
|
|
|
return_value["referenced_motions"] = referenced_motions
|
2019-01-27 13:17:17 +01:00
|
|
|
|
2019-03-23 12:06:57 +01:00
|
|
|
return_value["recommender"] = await get_config(
|
2019-01-26 20:37:49 +01:00
|
|
|
all_data, "motions_recommendations_by"
|
|
|
|
)
|
2019-01-19 21:36:30 +01:00
|
|
|
|
2019-06-26 16:16:53 +02:00
|
|
|
# Add submitters
|
2019-01-19 21:36:30 +01:00
|
|
|
return_value["submitter"] = [
|
2019-03-23 12:06:57 +01:00
|
|
|
await get_user_name(all_data, submitter["user_id"])
|
2019-01-27 13:17:17 +01:00
|
|
|
for submitter in sorted(
|
|
|
|
motion["submitters"], key=lambda submitter: submitter["weight"]
|
|
|
|
)
|
2019-01-19 21:36:30 +01:00
|
|
|
]
|
|
|
|
|
2019-06-26 16:16:53 +02:00
|
|
|
# Add recommendation-referencing motions
|
|
|
|
return_value[
|
|
|
|
"recommendation_referencing_motions"
|
|
|
|
] = await get_recommendation_referencing_motions(all_data, motion_id)
|
|
|
|
|
2019-01-19 21:36:30 +01:00
|
|
|
return return_value
|
|
|
|
|
|
|
|
|
2019-06-26 16:16:53 +02:00
|
|
|
async def get_recommendation_referencing_motions(
|
|
|
|
all_data: AllData, motion_id: int
|
|
|
|
) -> Optional[List[Dict[str, Any]]]:
|
|
|
|
"""
|
|
|
|
Returns all title information for motions, that are referencing
|
|
|
|
the given motion (by id) in their recommendation. If there are no
|
|
|
|
motions, None is returned (instead of []).
|
|
|
|
"""
|
|
|
|
recommendation_referencing_motions = []
|
|
|
|
for motion in all_data["motions/motion"].values():
|
|
|
|
# Motion must have a recommendation and a recommendaiton extension
|
|
|
|
if not motion["recommendation_id"] or not motion["recommendation_extension"]:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# The recommendation must allow the extension field (there might be left-overs
|
|
|
|
# in a motions recommendation extension..)
|
|
|
|
recommendation = await get_state(all_data, motion, "recommendation_id")
|
|
|
|
if not recommendation["show_recommendation_extension_field"]:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Find referenced motion ids
|
|
|
|
referenced_ids = [
|
|
|
|
int(id)
|
|
|
|
for id in motion_placeholder_regex.findall(
|
|
|
|
motion["recommendation_extension"]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
# if one of the referenced ids is the given motion, add the current motion.
|
|
|
|
if motion_id in referenced_ids:
|
|
|
|
recommendation_referencing_motions.append(
|
|
|
|
{"title": motion["title"], "identifier": motion["identifier"]}
|
|
|
|
)
|
|
|
|
return recommendation_referencing_motions or None
|
|
|
|
|
|
|
|
|
2019-03-23 12:06:57 +01:00
|
|
|
async def motion_block_slide(
|
2019-02-21 14:40:07 +01:00
|
|
|
all_data: AllData, element: Dict[str, Any], projector_id: int
|
|
|
|
) -> Dict[str, Any]:
|
2018-12-23 11:05:38 +01:00
|
|
|
"""
|
2019-02-21 12:34:13 +01:00
|
|
|
Motion block slide.
|
2018-12-23 11:05:38 +01:00
|
|
|
"""
|
2019-02-21 12:34:13 +01:00
|
|
|
motion_block_id = element.get("id")
|
|
|
|
|
|
|
|
if motion_block_id is None:
|
|
|
|
raise ProjectorElementException("id is required for motion block slide")
|
|
|
|
|
|
|
|
try:
|
|
|
|
motion_block = all_data["motions/motion-block"][motion_block_id]
|
|
|
|
except KeyError:
|
|
|
|
raise ProjectorElementException(
|
|
|
|
f"motion block with id {motion_block_id} does not exist"
|
|
|
|
)
|
|
|
|
|
2019-02-26 14:43:51 +01:00
|
|
|
# All motions in this motion block
|
2019-02-21 12:34:13 +01:00
|
|
|
motions = []
|
2019-02-26 14:43:51 +01:00
|
|
|
|
2019-03-06 16:59:18 +01:00
|
|
|
# All title information for referenced motions in the recommendation
|
2019-02-26 14:43:51 +01:00
|
|
|
referenced_motions: Dict[int, Dict[str, str]] = {}
|
|
|
|
|
|
|
|
# Search motions.
|
2019-02-21 12:34:13 +01:00
|
|
|
for motion in all_data["motions/motion"].values():
|
|
|
|
if motion["motion_block_id"] == motion_block_id:
|
|
|
|
motion_object = {
|
|
|
|
"title": motion["title"],
|
|
|
|
"identifier": motion["identifier"],
|
|
|
|
}
|
|
|
|
|
|
|
|
recommendation_id = motion["recommendation_id"]
|
|
|
|
if recommendation_id is not None:
|
2019-05-10 07:07:59 +02:00
|
|
|
recommendation = await get_state(all_data, motion, "recommendation_id")
|
2019-02-21 12:34:13 +01:00
|
|
|
motion_object["recommendation"] = {
|
2019-02-21 15:06:14 +01:00
|
|
|
"name": recommendation["recommendation_label"],
|
2019-02-21 12:34:13 +01:00
|
|
|
"css_class": recommendation["css_class"],
|
|
|
|
}
|
|
|
|
if recommendation["show_recommendation_extension_field"]:
|
2019-02-26 14:43:51 +01:00
|
|
|
recommendation_extension = motion["recommendation_extension"]
|
2019-03-23 12:06:57 +01:00
|
|
|
await extend_reference_motion_dict(
|
2019-03-06 16:59:18 +01:00
|
|
|
all_data, recommendation_extension, referenced_motions
|
|
|
|
)
|
2019-02-26 14:43:51 +01:00
|
|
|
motion_object["recommendation_extension"] = recommendation_extension
|
2019-02-21 12:34:13 +01:00
|
|
|
|
|
|
|
motions.append(motion_object)
|
|
|
|
|
2019-02-26 14:43:51 +01:00
|
|
|
return {
|
|
|
|
"title": motion_block["title"],
|
|
|
|
"motions": motions,
|
|
|
|
"referenced_motions": referenced_motions,
|
|
|
|
}
|
2017-08-30 00:07:54 +02:00
|
|
|
|
|
|
|
|
2019-01-27 13:17:17 +01:00
|
|
|
def register_projector_slides() -> None:
|
|
|
|
register_projector_slide("motions/motion", motion_slide)
|
|
|
|
register_projector_slide("motions/motion-block", motion_block_slide)
|