Skip to content

Commit

Permalink
Add list update fix for repeating sections (#1438)
Browse files Browse the repository at this point in the history
  • Loading branch information
petechd authored Jul 26, 2024
1 parent 0cbb427 commit 25fe398
Show file tree
Hide file tree
Showing 4 changed files with 340 additions and 14 deletions.
13 changes: 9 additions & 4 deletions app/questionnaire/questionnaire_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,9 @@ def _update_dependencies_for_calculated_summary_dependency(
)
dependent = self._get_dependent_for_block_id(block_id=dependent_block["id"])
for answer_id in calculated_summary_answer_ids:
if list_name := self.get_list_name_for_answer_id(answer_id):
if list_name := self.get_list_name_for_answer_id(
answer_id, value_source_update=False
):
# dynamic/repeating answers means the calculated summary also depends on the list those answers loop over
self._list_dependencies_map[list_name].add(dependent)
self._answer_dependencies_map[answer_id].add(dependent)
Expand Down Expand Up @@ -859,9 +861,12 @@ def is_repeating_answer(
or self.is_answer_dynamic(answer_id)
)

def get_list_name_for_answer_id(self, answer_id: str) -> str | None:
def get_list_name_for_answer_id(
self, answer_id: str, value_source_update: bool = True
) -> str | 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 the answer is updated for calculated summary value source, return the name of the list, if updated for calculated summary dependency and the answer
is part of a repeating section, return None.
"""
# Type ignore: safe to assume block exists, same for section below.
block: ImmutableDict = self.get_block_for_answer_id(answer_id) # type: ignore
Expand All @@ -872,7 +877,7 @@ def get_list_name_for_answer_id(self, answer_id: str) -> str | None:
return self.list_names_by_list_repeating_block_id[block_id]
if self.is_answer_in_list_collector_block(answer_id):
return block["for_list"] # type: ignore
if self.is_answer_in_repeating_section(answer_id):
if self.is_answer_in_repeating_section(answer_id) and value_source_update:
section_id: str = self.get_section_id_for_block_id(block_id) # type: ignore
return self.get_repeating_list_for_section(section_id)

Expand Down
300 changes: 298 additions & 2 deletions schemas/test/en/test_calculated_summary_dependent_questions.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
"title": "A test schema to demo Calculated Summary",
"description": "A schema to showcase Calculated Summary with dependent questions.",
"questionnaire_flow": {
"type": "Linear",
"options": {}
"type": "Hub",
"options": {
"required_completed_sections": ["default-section", "list-collector-section"]
}
},
"sections": [
{
Expand Down Expand Up @@ -147,6 +149,300 @@
]
}
]
},
{
"id": "list-collector-section",
"title": "Additional sites",
"summary": {
"show_on_completion": false,
"collapsible": false,
"items": [
{
"type": "List",
"for_list": "additional_sites_name",
"title": "What was the business name for this site?",
"item_anchor_answer_id": "business-name",
"item_label": "<p>Business name</p>",
"add_link_text": "Add item to this list",
"empty_list_text": "There are no items"
}
],
"show_non_item_answers": true
},
"show_on_hub": true,
"groups": [
{
"id": "list-collector-group",
"blocks": [
{
"id": "additional-sites-for-your-business",
"type": "ListCollectorDrivingQuestion",
"for_list": "additional_sites_name",
"question": {
"id": "question-driving-further-additional-sites-for-your-business",
"type": "General",
"title": "Did your business have any additional sites that were staffed for a minimum of 20 hours per week or had planned activity for more than a year?",
"answers": [
{
"id": "additional-sites-answer",
"mandatory": true,
"type": "Radio",
"options": [
{
"label": "Yes",
"value": "Yes",
"action": {
"type": "RedirectToListAddBlock",
"params": {
"block_id": "add-block-business-name-trading-style-and-address-for-this-additional-site",
"list_name": "additional_sites_name"
}
}
},
{
"label": "No",
"value": "No"
}
]
}
]
},
"routing_rules": [
{
"section": "End",
"when": {
"in": [
{
"source": "answers",
"identifier": "additional-sites-answer"
},
["No"]
]
}
},
{
"block": "further-additional-sites-for-your-business"
}
],
"page_title": "Additional sites for your business"
},
{
"id": "further-additional-sites-for-your-business",
"type": "ListCollector",
"page_title": "Further additional sites for your business",
"for_list": "additional_sites_name",
"question": {
"id": "list-collector-question-further-additional-sites-for-your-business",
"type": "General",
"title": "Did you have any other sites that were staffed for a minimum of 20 hours per week or had planned activity for more than a year?",
"answers": [
{
"id": "any-other-additional-sites-answer",
"mandatory": true,
"type": "Radio",
"options": [
{
"label": "Yes",
"value": "Yes",
"action": {
"type": "RedirectToListAddBlock"
}
},
{
"label": "No",
"value": "No"
}
]
}
]
},
"add_block": {
"id": "add-block-business-name-trading-style-and-address-for-this-additional-site",
"type": "ListAddQuestion",
"page_title": "Business name, trading style and address for this additional site",
"cancel_text": "Don’t need to add this item",
"question": {
"id": "add-block-question-business-name-trading-style-and-address-for-this-additional-site",
"type": "General",
"title": "What was the business name for this site?",
"answers": [
{
"id": "business-name",
"mandatory": true,
"type": "TextField",
"label": "Business name"
}
]
}
},
"edit_block": {
"id": "edit-block-business-name-trading-style-and-address-for-this-additional-site",
"type": "ListEditQuestion",
"page_title": "Business name, trading style and address for this additional site",
"cancel_text": "Don’t need to edit this item",
"question": {
"id": "edit-block-question-business-name-trading-style-and-address-for-this-additional-site",
"type": "General",
"title": "What was the business name, trading style and address for this site?",
"answers": [
{
"id": "business-name",
"mandatory": true,
"type": "TextField",
"label": "Business name"
}
]
}
},
"remove_block": {
"id": "remove-block-business-name-trading-style-and-address-for-this-additional-site",
"type": "ListRemoveQuestion",
"cancel_text": "Don’t need to remove this item?",
"question": {
"id": "remove-block-question-business-name-trading-style-and-address-for-this-additional-site",
"type": "General",
"title": "Are you sure you want to remove this item?",
"warning": "All of the information about this item will be deleted",
"answers": [
{
"id": "remove-confirmation-business-name-trading-style-and-address-for-this-additional-site",
"mandatory": true,
"type": "Radio",
"options": [
{
"label": "Yes",
"value": "Yes",
"action": {
"type": "RemoveListItemAndAnswers"
}
},
{
"label": "No",
"value": "No"
}
]
}
]
}
},
"summary": {
"title": "Summary",
"item_title": {
"text": "{listcollector_summary_placeholder}",
"placeholders": [
{
"placeholder": "listcollector_summary_placeholder",
"transforms": [
{
"arguments": {
"delimiter": " ",
"list_to_concatenate": [
{
"source": "answers",
"identifier": "business-name"
}
]
},
"transform": "concatenate_list"
}
]
}
]
}
}
}
]
}
]
},
{
"id": "repeating-section",
"title": "Additional site details",
"repeat": {
"for_list": "additional_sites_name",
"title": {
"text": "{repeat_title_placeholder}",
"placeholders": [
{
"placeholder": "repeat_title_placeholder",
"transforms": [
{
"arguments": {
"delimiter": " ",
"list_to_concatenate": [
{
"source": "answers",
"identifier": "business-name"
}
]
},
"transform": "concatenate_list"
}
]
}
]
}
},
"summary": {
"show_on_completion": false,
"page_title": "Details of Additional Sites in Great Britain",
"collapsible": false
},
"show_on_hub": true,
"groups": [
{
"id": "repeating-group",
"blocks": [
{
"id": "number-of-employees-working-at-this-additional-site",
"type": "Question",
"question": {
"id": "question-number-of-employees-working-at-this-additional-site",
"title": "What was the number of full-time and part-time employees that your business paid from its payroll, for this site?",
"type": "General",
"answers": [
{
"id": "number-full-time-employees",
"mandatory": true,
"type": "Number",
"label": "Number of full-time employees",
"decimal_places": 0
},
{
"id": "number-part-time-employees",
"mandatory": true,
"type": "Number",
"label": "Number of part-time employees",
"decimal_places": 0
}
]
},
"page_title": "Number of employees working at this additional site"
},
{
"id": "calculated-number-of-employees-for-this-additional-site",
"type": "CalculatedSummary",
"page_title": "Calculated number of employees for this additional site",
"title": "We have calculated the total number of employees that your business paid from its payroll, for this site, to be %(total)s. Is this correct?",
"calculation": {
"operation": {
"+": [
{
"identifier": "number-full-time-employees",
"source": "answers"
},
{
"identifier": "number-part-time-employees",
"source": "answers"
}
]
},
"title": "Total number of employees paid from your business&#39;s payroll"
}
}
]
}
]
}
],
"theme": "default",
Expand Down
10 changes: 2 additions & 8 deletions tests/app/questionnaire/test_questionnaire_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,18 +1022,13 @@ def test_grand_calculated_summary_dependencies():
schema = load_schema_from_name(
"test_grand_calculated_summary_inside_repeating_section"
)
gcs = Dependent(
section_id="vehicle-details-section",
block_id="grand-calculated-summary-vehicle",
for_list="vehicles",
)

gcs_dependent = Dependent(
section_id="vehicle-details-section",
block_id="gcs-breakdown-block",
for_list="vehicles",
)
assert gcs in schema.list_dependencies["costs"]
assert gcs in schema.list_dependencies["vehicles"]

assert gcs_dependent in schema.list_dependencies["costs"]
assert gcs_dependent in schema.list_dependencies["vehicles"]
gcs_answers = [
Expand All @@ -1043,7 +1038,6 @@ def test_grand_calculated_summary_dependencies():
"vehicle-fuel-cost",
]
for answer in gcs_answers:
assert gcs in schema.answer_dependencies[answer]
assert gcs_dependent in schema.answer_dependencies[answer]


Expand Down
Loading

0 comments on commit 25fe398

Please sign in to comment.