Skip to content

Commit

Permalink
Renaming and some simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
petechd committed Jun 7, 2024
1 parent f3d3159 commit 4cedbf2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
27 changes: 15 additions & 12 deletions app/validators/questionnaire_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def __init__(self, schema):
for block in list_collector.get("repeating_blocks", [])
}
self._answers_with_context = {}
self._referenced_lists = {}
self._lists_with_context = {}

@lru_cache
def get_block_ids_for_block_type(self, block_type: str) -> list[str]:
Expand Down Expand Up @@ -211,39 +211,42 @@ def answers_with_context(self):
return self._answers_with_context

@property
def referenced_lists(self):
if self._referenced_lists:
return self._referenced_lists
def lists_with_context(self):
if self._lists_with_context:
return self._lists_with_context

if supplementary_list := self.supplementary_lists:
for list_id in supplementary_list:
self._referenced_lists[list_id] = {"section_index": 0, "block_index": 0}
self._lists_with_context[list_id] = {
"section_index": 0,
"block_index": 0,
}

if blocks := self.get_blocks(type="ListCollector"):
if blocks := self.list_collectors:
for block in blocks:
list_id = block["for_list"]
if list_id not in self._referenced_lists:
if list_id not in self._lists_with_context:
section_id = self.get_section_id_for_block_id(block["id"])
section_index = self.get_section_index_for_section_id(section_id)
self._referenced_lists[list_id] = {
self._lists_with_context[list_id] = {
"section_index": section_index,
"block_index": self.block_ids.index(block["id"]),
}
if blocks := self.get_blocks(type="PrimaryPersonListCollector"):
for block in blocks:
list_id = block["for_list"]
if list_id not in self._referenced_lists or (
if list_id not in self._lists_with_context or (
self.block_ids.index(block["id"])
< self._referenced_lists[list_id]["block_index"]
< self._lists_with_context[list_id]["block_index"]
):
section_id = self.get_section_id_for_block_id(block["id"])
section_index = self.get_section_index_for_section_id(section_id)
self._referenced_lists[list_id] = {
self._lists_with_context[list_id] = {
"section_index": section_index,
"block_index": self.block_ids.index(block["id"]),
}

return self._referenced_lists
return self._lists_with_context

@staticmethod
def capture_answers(*, answers, answers_dict, context):
Expand Down
6 changes: 3 additions & 3 deletions app/validators/questionnaire_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def validate_introduction_block(self):
self.add_error(error_messages.PREVIEW_WITHOUT_INTRODUCTION_BLOCK)

def validate_list_references(self):
referenced_lists = self.questionnaire_schema.referenced_lists
lists_with_context = self.questionnaire_schema.lists_with_context

# We need to keep track of section index for: common_definitions.json#/section_enabled
for section_index, section in enumerate(self.questionnaire_schema.sections):
Expand All @@ -156,7 +156,7 @@ def validate_list_references(self):
)
if (
parent_block_index
< referenced_lists[list_identifier]["block_index"]
< lists_with_context[list_identifier]["block_index"]
):
self.add_error(
error_messages.LIST_REFERENCED_BEFORE_ADDED.format(
Expand All @@ -166,7 +166,7 @@ def validate_list_references(self):
)
elif (
section_index
< referenced_lists[list_identifier]["section_index"]
< lists_with_context[list_identifier]["section_index"]
):
# Section level "enabled" rule that can use list source,
# check: common_definitions.json#/section_enabled
Expand Down

0 comments on commit 4cedbf2

Please sign in to comment.