Refactor template list logic into one place

The Jinja template for the ‘choose templates’ page is now pulling in
data from a lot of diparate places in order to work out what to show. As
we add more logic about what to show (in order to make the live search
work) it’s going to get harder to have all this logic in the Jinja
template.

This commit refactors it back into Python where we have more language
features for managing complex logic.

It’s a bit weird to call this file a model, in that it’s dealing with
some presentational logic, rather than just data. Conceptually it’s more
like a view model[1].

1. https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel
This commit is contained in:
Chris Hill-Scott
2018-11-21 09:53:29 +00:00
parent 1cc2a529d5
commit 82d8207612
8 changed files with 159 additions and 82 deletions

View File

@@ -118,6 +118,11 @@ def test_post_add_template_folder_page(client_request, service_one, mocker, pare
['Text message', 'Email', 'Letter'],
[
'folder_one 2 folders',
'folder_one_one 1 template, 1 folder',
'folder_one_one_one 1 template',
'sms_template_nested Text message template',
'letter_template_nested Letter template',
'folder_one_two Empty',
'folder_two Empty',
'sms_template_one Text message template',
'sms_template_two Text message template',
@@ -136,6 +141,9 @@ def test_post_add_template_folder_page(client_request, service_one, mocker, pare
['All', 'Email', 'Letter'],
[
'folder_one 1 folder',
'folder_one_one 1 folder',
'folder_one_one_one 1 template',
'sms_template_nested Text message template',
'sms_template_one Text message template',
'sms_template_two Text message template',
],
@@ -149,6 +157,9 @@ def test_post_add_template_folder_page(client_request, service_one, mocker, pare
['Text message', 'Email', 'Letter'],
[
'folder_one_one 1 template, 1 folder',
'folder_one_one_one 1 template',
'sms_template_nested Text message template',
'letter_template_nested Letter template',
'folder_one_two Empty',
],
None,
@@ -161,6 +172,8 @@ def test_post_add_template_folder_page(client_request, service_one, mocker, pare
['All', 'Email', 'Letter'],
[
'folder_one_one 1 folder',
'folder_one_one_one 1 template',
'sms_template_nested Text message template',
],
None,
),
@@ -181,6 +194,7 @@ def test_post_add_template_folder_page(client_request, service_one, mocker, pare
['Text message', 'Email', 'Letter'],
[
'folder_one_one_one 1 template',
'sms_template_nested Text message template',
'letter_template_nested Letter template',
],
None,
@@ -282,7 +296,6 @@ def test_should_show_templates_folder_page(
assert links_in_page[index].text.strip() == expected_link
page_items = page.select('.template-list-item')
assert len(page_items) == len(expected_items)
for index, expected_item in enumerate(expected_items):

View File

@@ -470,7 +470,7 @@ def test_should_show_live_search_if_service_has_lots_of_folders(
count_of_templates = count_of_templates_and_folders - count_of_folders
assert len(page.select('.live-search')) == 1
assert count_of_folders == 1
assert count_of_folders == 4
assert count_of_templates == 4