redirect from address_line_n to address textarea page

if someone starts a new one-off flow they'll get taken to the address
page. However, if someone hits the back button, they'll cycle backwards
through placeholders and will end up on the individual line pages. Lets
redirect them to the correct place.

We'll additionally need to reconstruct the address block from the
various session variables that may or may not be populated
This commit is contained in:
Leo Hemsted
2020-03-31 16:59:48 +01:00
parent c3fa8ab2e1
commit 3529188968
2 changed files with 30 additions and 44 deletions
+19 -1
View File
@@ -335,6 +335,18 @@ def get_notification_check_endpoint(service_id, template):
)) ))
def get_letter_address_from_session():
"""
Takes non-blank-string address values out of the placeholders, and returns a multi line string
"""
keys = [f'address line {i}' for i in range(1, 7)] + ['postcode']
return '\n'.join([
session['placeholders'][key]
for key in keys
if session['placeholders'].get(key)
])
@main.route( @main.route(
"/services/<uuid:service_id>/send/<uuid:template_id>/one-off/address", "/services/<uuid:service_id>/send/<uuid:template_id>/one-off/address",
methods=['GET', 'POST'] methods=['GET', 'POST']
@@ -364,7 +376,9 @@ def send_one_off_letter_address(service_id, template_id):
sms_sender=None sms_sender=None
) )
form = LetterAddressForm() current_session_address = get_letter_address_from_session()
form = LetterAddressForm(address=current_session_address)
if form.validate_on_submit(): if form.validate_on_submit():
session['placeholders'].update(form.as_address_lines_1_to_7_with_postcode) session['placeholders'].update(form.as_address_lines_1_to_7_with_postcode)
@@ -467,6 +481,10 @@ def send_test_step(service_id, template_id, step_index):
template_id=template_id, template_id=template_id,
)) ))
# if we're in a letter, we should show address block rather than "address line #" or "postcode"
if db_template['template_type'] == 'letter' and current_placeholder in first_column_headings['letter']:
return redirect(url_for('.send_one_off_letter_address', service_id=service_id, template_id=template_id))
optional_placeholder = (current_placeholder in optional_address_columns) optional_placeholder = (current_placeholder in optional_address_columns)
form = get_placeholder_form_instance( form = get_placeholder_form_instance(
current_placeholder, current_placeholder,
+11 -43
View File
@@ -2015,66 +2015,34 @@ def test_send_test_caches_page_count(
assert session['send_test_letter_page_count'] == 9 assert session['send_test_letter_page_count'] == 9
def test_send_test_indicates_optional_address_columns( def test_send_one_off_back_link_populates_address_textarea(
client_request, client_request,
mocker, mocker,
mock_get_service_letter_template, mock_get_service_letter_template,
mock_template_preview,
fake_uuid, fake_uuid,
): ):
mocker.patch('app.main.views.send.get_page_count_for_letter', return_value=1)
with client_request.session_transaction() as session: with client_request.session_transaction() as session:
session['recipient'] = None session['recipient'] = None
session['placeholders'] = {} session['placeholders'] = {'address line 1': 'foo', 'address line 2': 'bar', 'address line 3': ''}
# imagine someone hit the back button to go from line 3 page to line 2 page
page = client_request.get( page = client_request.get(
'main.send_test_step', 'main.send_test_step',
service_id=SERVICE_ONE_ID, service_id=SERVICE_ONE_ID,
template_id=fake_uuid, template_id=fake_uuid,
step_index=3,
)
assert normalize_spaces(page.select('label')[0].text) == (
'address line 4 '
'Optional'
)
assert page.select('.govuk-back-link')[0]['href'] == url_for(
'main.send_one_off_step',
service_id=SERVICE_ONE_ID,
template_id=fake_uuid,
step_index=2, step_index=2,
_follow_redirects=True
) )
assert page.select_one('h1').text.strip() == 'Send Two week reminder'
def test_send_test_allows_empty_optional_address_columns( form = page.select_one('form')
client_request, assert form.select_one('label').text.strip() == 'Address'
mocker,
mock_get_service_letter_template,
fake_uuid,
):
mocker.patch('app.main.views.send.get_page_count_for_letter', return_value=1) textarea = form.select_one('textarea')
assert textarea.attrs['name'] == 'address'
with client_request.session_transaction() as session: assert textarea.text == 'foo\nbar'
session['recipient'] = None
session['placeholders'] = {}
client_request.post(
'main.send_test_step',
service_id=SERVICE_ONE_ID,
template_id=fake_uuid,
step_index=3,
# no data here
_expected_status=302,
_expected_redirect=url_for(
'main.send_test_step',
service_id=SERVICE_ONE_ID,
template_id=fake_uuid,
step_index=4,
_external=True,
),
)
def test_send_test_sms_message_puts_submitted_data_in_session( def test_send_test_sms_message_puts_submitted_data_in_session(