Skip to content

Commit

Permalink
Fix placeholder transform resolution issue in calc summary pages (#1330)
Browse files Browse the repository at this point in the history
  • Loading branch information
berroar authored Mar 18, 2024
1 parent 4bc45c0 commit 84c2409
Show file tree
Hide file tree
Showing 4 changed files with 669 additions and 18 deletions.
77 changes: 59 additions & 18 deletions app/questionnaire/questionnaire_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,9 @@ def _populate_calculation_summary_section_dependencies(self) -> None:
block["id"]
].update(section_dependencies)

def _get_section_ids_for_answer_ids(self, answer_ids: set[str]) -> OrderedSet[str]:
def _get_section_ids_for_answer_ids(
self, answer_ids: Iterable[str]
) -> OrderedSet[str]:
section_dependencies: OrderedSet[str] = OrderedSet()
for answer_id in answer_ids:
block = self.get_block_for_answer_id(answer_id)
Expand Down Expand Up @@ -1385,26 +1387,53 @@ def get_item_anchor(self, section_id: str, list_name: str) -> str | None:
if item["for_list"] == list_name and item.get("item_anchor_answer_id"):
return f"#{str(item['item_anchor_answer_id'])}"

def _update_dependencies_for_placeholders(
self, answer_ids: Iterable[str], block: ImmutableDict
) -> None:
if dependent_sections := self._get_section_ids_for_answer_ids(
answer_ids=answer_ids
):
# Type Ignore: At this point the section id and block id cannot be None
section_id = self.get_section_id_for_block_id(block["id"])
self._placeholder_transform_section_dependencies_by_block[section_id][ # type: ignore
block["id"]
].update(
dependent_sections
)

def _populate_placeholder_transform_section_dependencies(self) -> None:
for block in self.get_blocks():
transforms = get_mappings_with_key("transform", data=block)
placeholder_answer_ids = {
item["identifier"]
for transform in transforms
if transform["transform"] in TRANSFORMS_REQUIRING_ROUTING_PATH
for item in transform["arguments"]["items"]
if item.get("source") == "answers"
}
placeholder_dependencies = self._get_section_ids_for_answer_ids(
answer_ids=placeholder_answer_ids
)
if placeholder_dependencies:
# Type Ignore: At this point we section id and block id cannot be None
section_id = self.get_section_id_for_block_id(block["id"])
self._placeholder_transform_section_dependencies_by_block[section_id][ # type: ignore
# Calculation summary blocks can indirectly reference placeholders from their dependent blocks, so
# logic is needed to identify the relevant dependent blocks that may contain placeholders and assess
# whether there are any dependent sections
if block["type"] == "GrandCalculatedSummary":
answer_ids = self.get_answer_ids_for_grand_calculated_summary_id(
block["id"]
].update(
placeholder_dependencies
)
elif block["type"] == "CalculatedSummary":
answer_ids = get_calculated_summary_answer_ids(block)
else:
answer_ids = []

if answer_ids:
dependent_blocks = [
self.get_block_for_answer_id(answer_id)
for answer_id in set(answer_ids)
]
for dependent_block in dependent_blocks:
# Type ignore: Block will exist at this point
if placeholder_answer_ids := get_placeholder_answer_ids_requiring_routing_path(
dependent_block # type: ignore
):
self._update_dependencies_for_placeholders(
placeholder_answer_ids, block
)

elif placeholder_answer_ids := get_placeholder_answer_ids_requiring_routing_path(
block
):
self._update_dependencies_for_placeholders(
placeholder_answer_ids, block
)

def update_dependencies_for_dynamic_answers(
Expand Down Expand Up @@ -1470,3 +1499,15 @@ def get_calculated_summary_ids_for_grand_calculated_summary(

def is_list_collector_block_editable(block: Mapping) -> bool:
return bool(block["type"] == "ListCollector")


def get_placeholder_answer_ids_requiring_routing_path(block: ImmutableDict) -> set[str]:
transforms = get_mappings_with_key("transform", data=block)

return {
item["identifier"]
for transform in transforms
if transform["transform"] in TRANSFORMS_REQUIRING_ROUTING_PATH
for item in transform["arguments"]["items"]
if item.get("source") == "answers"
}
Loading

0 comments on commit 84c2409

Please sign in to comment.