Skip to content

Commit

Permalink
Filter list summaries on unrelated question blocks
Browse files Browse the repository at this point in the history
List summaries on unrelated question blocks now only include
people after the current person.
  • Loading branch information
ajmaddaford committed Oct 14, 2020
1 parent b0b2c92 commit 14b1cdc
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 20 deletions.
9 changes: 9 additions & 0 deletions app/views/contexts/list_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def __call__(
edit_block_id=None,
remove_block_id=None,
primary_person_edit_block_id=None,
for_list_item_ids=None,
):
list_items = (
list(
Expand All @@ -25,6 +26,7 @@ def __call__(
edit_block_id,
remove_block_id,
primary_person_edit_block_id,
for_list_item_ids,
)
)
if summary_definition
Expand All @@ -46,8 +48,15 @@ def _build_list_items_context(
edit_block_id,
remove_block_id,
primary_person_edit_block_id,
for_list_item_ids,
):
list_item_ids = self._list_store[for_list]
if for_list_item_ids:
list_item_ids = [
list_item_id
for list_item_id in list_item_ids
if list_item_id in for_list_item_ids
]
primary_person = self._list_store[for_list].primary_person

for list_item_id in list_item_ids:
Expand Down
33 changes: 18 additions & 15 deletions app/views/handlers/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ def rendered_block(self):
**{"question": rendered_question},
}

@cached_property
def list_context(self):
return ListContext(
self._language,
self._schema,
self._questionnaire_store.answer_store,
self._questionnaire_store.list_store,
self._questionnaire_store.progress_store,
self._questionnaire_store.metadata,
)

def get_next_location_url(self):
answer_action = self._get_answer_action()
if self._has_redirect_to_list_add_action(answer_action):
Expand Down Expand Up @@ -132,21 +143,7 @@ def get_context(self):
] = self.get_last_viewed_question_guidance_context()

if "list_summary" in self.rendered_block:
list_context = ListContext(
self._language,
self._schema,
self._questionnaire_store.answer_store,
self._questionnaire_store.list_store,
self._questionnaire_store.progress_store,
self._questionnaire_store.metadata,
)

context.update(
list_context(
self.rendered_block["list_summary"]["summary"],
self.rendered_block["list_summary"]["for_list"],
)
)
context.update(self.get_list_summary_context())

if self.form.errors or self.form.question_errors:
self.page_title = gettext("Error: {page_title}").format(
Expand All @@ -162,6 +159,12 @@ def get_last_viewed_question_guidance_context(self):
).url()
return {"first_location_in_section_url": first_location_in_section_url}

def get_list_summary_context(self):
return self.list_context(
self.rendered_block["list_summary"]["summary"],
self.rendered_block["list_summary"]["for_list"],
)

def handle_post(self):
self.questionnaire_store_updater.update_answers(self.form.data)
if self.questionnaire_store_updater.is_dirty():
Expand Down
22 changes: 19 additions & 3 deletions app/views/handlers/relationships/unrelated_question.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
from functools import cached_property

from app.questionnaire.location import Location
from app.views.handlers.question import Question


class UnrelatedQuestion(Question):
def _get_parent_list_name(self):
@cached_property
def list_name(self):
parent_block_id = self._schema.parent_id_map[self.block["id"]]
return self._schema.get_block(parent_block_id)["for_list"]

@property
@cached_property
def parent_location(self):
parent_block_id = self._schema.parent_id_map[self.block["id"]]
return Location(
section_id=self._current_location.section_id, block_id=parent_block_id
)

@cached_property
def remaining_relationship_list_item_ids(self):
list_model = self._questionnaire_store.list_store[self.list_name]
start = list_model.index(self._current_location.list_item_id) + 1
return list_model[start:]

def get_list_summary_context(self):
return self.list_context(
self.rendered_block["list_summary"]["summary"],
self.rendered_block["list_summary"]["for_list"],
for_list_item_ids=self.remaining_relationship_list_item_ids,
)

def _get_routing_path(self):
return self.router.routing_path(section_id=self.parent_location.section_id)

Expand All @@ -25,7 +41,7 @@ def is_location_valid(self):
if not can_access_parent_location:
return False

if self.current_location.list_name != self._get_parent_list_name() or (
if self.current_location.list_name != self.list_name or (
self.current_location.list_item_id
and not self.router.is_list_item_in_list_store(
self.current_location.list_item_id, self.current_location.list_name
Expand Down
30 changes: 30 additions & 0 deletions tests/app/views/contexts/test_list_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,33 @@ def test_assert_primary_person_string_appended(
assert list_context["list"]["list_items"][0]["primary_person"] is True
assert list_context["list"]["list_items"][0]["item_title"] == "Toni Morrison (You)"
assert list_context["list"]["list_items"][1]["item_title"] == "Barry Pheloung"


@pytest.mark.usefixtures("app")
def test_for_list_item_ids(
list_collector_block, people_answer_store, people_list_store
):
schema = load_schema_from_name("test_list_collector_primary_person")

list_context = ListContext(
language=DEFAULT_LANGUAGE_CODE,
progress_store=ProgressStore(),
list_store=people_list_store,
schema=schema,
answer_store=people_answer_store,
metadata=None,
)
list_context = list_context(
list_collector_block["summary"],
list_collector_block["for_list"],
for_list_item_ids=["UHPLbX"],
)

expected = [
{
"item_title": "Barry Pheloung",
"primary_person": False,
}
]

assert expected == list_context["list"]["list_items"]
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def launch_survey_and_add_people(self):
self.launchSurvey("test_relationships_unrelated", roles=["dumper"])
self.add_person("Marie", "Doe")
self.add_person("John", "Doe")
self.add_person("Jane", "Doe")
self.post({"anyone-else": "No"})

def test_is_accessible_when_list_name_and_list_item_valid(
Expand All @@ -18,8 +19,6 @@ def test_is_accessible_when_list_name_and_list_item_valid(
f"/questionnaire/relationships/people/{first_list_item}/related-to-anyone-else"
)
self.assertInBody("Are any of these people related to you?")
self.assertInBody("Marie Doe")
self.assertInBody("John Doe")

def test_is_not_accessible_when_invalid_list_item(self):
self.launchSurvey("test_relationships_unrelated")
Expand All @@ -45,3 +44,22 @@ def test_is_not_accessible_when_invalid_block_id(self):
f"/questionnaire/relationships/people/{first_list_item}/invalid-block-id"
)
self.assertStatusNotFound()

def test_list_summary(self):
self.launch_survey_and_add_people()

first_list_item = self.dump_debug()["LISTS"][0]["items"][0]
self.get(
f"/questionnaire/relationships/people/{first_list_item}/related-to-anyone-else"
)
self.assertNotInBody("Marie Doe")
self.assertInBody("John Doe")
self.assertInBody("Jane Doe")

second_list_item = self.dump_debug()["LISTS"][0]["items"][1]
self.get(
f"/questionnaire/relationships/people/{second_list_item}/related-to-anyone-else"
)
self.assertNotInBody("Marie Doe")
self.assertNotInBody("John Doe")
self.assertInBody("Jane Doe")

0 comments on commit 14b1cdc

Please sign in to comment.