Skip to content

Commit

Permalink
Simplify new properties
Browse files Browse the repository at this point in the history
  • Loading branch information
katie-gardner committed Dec 11, 2023
1 parent eb6faa9 commit 546e964
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions app/validators/questionnaire_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,30 +139,35 @@ def __init__(self, schema):
self.supplementary_lists = jp.match(
"$..supplementary_data.lists[*]", self.schema
)
self.list_collector_names = jp.match(
'$..blocks[?(@.type=="ListCollector")].for_list', self.schema
self.list_collectors = jp.match(
'$..blocks[?(@.type=="ListCollector")]', self.schema
)
self.list_collector_names = [
list_collector["for_list"] for list_collector in self.list_collectors
]
self.list_names = self.list_collector_names + self.supplementary_lists
self.list_names_by_repeating_block_id = {
block["id"]: list_collector["for_list"]
for list_collector in jp.match(
'$..blocks[?(@.type=="ListCollector")]', self.schema
)
for list_collector in self.list_collectors
for block in list_collector.get("repeating_blocks", [])
}
self.list_names_by_dynamic_answer_id = {
answer["id"]: dynamic_answer["values"]["identifier"]
for dynamic_answer in jp.match("$..dynamic_answers[*]", self.schema)
if dynamic_answer["values"]["source"] == "list"
for answer in dynamic_answer["answers"]
}

self._answers_with_context = {}

@lru_cache
def get_block_ids_for_block_type(self, block_type: str) -> list[str]:
return [block["id"] for block in self.blocks if block["type"] == block_type]

@cached_property
def list_names_by_dynamic_answer_id(self) -> dict[str, str]:
answer_id_to_list: dict[str, str] = {}
for dynamic_answer in jp.match("$..dynamic_answers[*]", self.schema):
if dynamic_answer["values"]["source"] == "list":
list_name = dynamic_answer["values"]["identifier"]
answer_id_to_list.update(
{answer["id"]: list_name for answer in dynamic_answer["answers"]}
)
return answer_id_to_list

@cached_property
def numeric_answer_ranges(self):
numeric_answer_ranges = {}
Expand Down Expand Up @@ -410,7 +415,10 @@ def get_all_dynamic_answer_ids(self, block_id):
}

def get_list_name_for_answer_id(self, answer_id: str) -> str | None:
"""If the answer is repeating, return the name of the list it repeats over, otherwise None"""
"""
If the answer is dynamic or in a repeating block or section, return the name of the list it repeats over
otherwise None
"""
if list_name := self.list_names_by_dynamic_answer_id.get(answer_id):
return list_name
block = self.get_block_by_answer_id(answer_id)
Expand Down

0 comments on commit 546e964

Please sign in to comment.