Skip to content

Commit

Permalink
Add IR failure template and handle failure (#324)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Maddaford <[email protected]>
  • Loading branch information
JamesGardiner and ajmaddaford authored Oct 5, 2020
1 parent 36719da commit 410cf2c
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 57 deletions.
31 changes: 28 additions & 3 deletions app/routes/errors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask import Blueprint, request
from flask import session as cookie_session
from flask.helpers import url_for
from flask_login import current_user
from flask_wtf.csrf import CSRFError
from sdc.crypto.exceptions import InvalidTokenException
Expand All @@ -14,6 +15,7 @@
from app.helpers.template_helpers import render_template
from app.settings import EQ_SESSION_ID
from app.submitter.submission_failed import SubmissionFailedException
from app.views.handlers.individual_response import FulfilmentRequestFailedException

logger = get_logger()

Expand All @@ -35,13 +37,12 @@ def log_error(error, status_code):
)


def _render_error_page(status_code, template=None):
def _render_error_page(status_code, template=None, **kwargs):
handle_language()
template = template or status_code
using_edge = request.user_agent.browser == "edge"

return (
render_template(template=f"errors/{template}", using_edge=using_edge),
render_template(template=f"errors/{template}", **kwargs),
status_code,
)

Expand Down Expand Up @@ -92,3 +93,27 @@ def internal_server_error(error=None):
def http_exception(error):
log_error(error, error.code)
return _render_error_page(error.code)


@errors_blueprint.app_errorhandler(FulfilmentRequestFailedException)
def fulfilment_request_failed(error):
logger.exception(
"An individual response fulfilment request failed",
url=request.url,
status_code=500,
)

if "mobile_number" in request.args:
blueprint_method = (
"individual_response.individual_response_text_message_confirm"
)
else:
blueprint_method = (
"individual_response.individual_response_post_address_confirm"
)

retry_url = url_for(
blueprint_method, list_item_id=request.view_args["list_item_id"], **request.args
)

return _render_error_page(500, template="fulfilment-request", retry_url=retry_url)
77 changes: 43 additions & 34 deletions app/translations/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2020-10-02 09:53+0100\n"
"POT-Creation-Date: 2020-10-02 10:40+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -335,136 +335,136 @@ msgstr ""
msgid "Error: {page_title}"
msgstr ""

#: app/views/handlers/individual_response.py:93
#: app/views/handlers/individual_response.py:98
msgid "Person {list_item_position}"
msgstr ""

#: app/views/handlers/individual_response.py:190
#: app/views/handlers/individual_response.py:198
msgid "Cannot answer questions for others in your household"
msgstr ""

#: app/views/handlers/individual_response.py:238
#: app/views/handlers/individual_response.py:246
msgid "How would you like <em>{person_name}</em> to receive a separate census?"
msgstr ""

#: app/views/handlers/individual_response.py:244
#: app/views/handlers/individual_response.py:252
msgid ""
"For someone to complete a separate census, we need to send them an "
"individual access code."
msgstr ""

#: app/views/handlers/individual_response.py:247
#: app/views/handlers/individual_response.py:255
msgid "Select how to send access code"
msgstr ""

#: app/views/handlers/individual_response.py:257
#: app/views/handlers/individual_response.py:265
msgid "Text message"
msgstr ""

#: app/views/handlers/individual_response.py:259
#: app/views/handlers/individual_response.py:267
msgid "We will need their mobile number for this"
msgstr ""

#: app/views/handlers/individual_response.py:264
#: app/views/handlers/individual_response.py:272
msgid "Post"
msgstr ""

#: app/views/handlers/individual_response.py:266
#: app/views/handlers/individual_response.py:274
msgid ""
"We can only send this to an unnamed resident at the registered household "
"address"
msgstr ""

#: app/views/handlers/individual_response.py:335
#: app/views/handlers/individual_response.py:343
msgid "Send individual access code"
msgstr ""

#: app/views/handlers/individual_response.py:364
#: app/views/handlers/individual_response.py:372
msgid "How would you like to answer <em>{person_name_possessive}</em> questions?"
msgstr ""

#: app/views/handlers/individual_response.py:377
#: app/views/handlers/individual_response.py:385
msgid "I would like to request a separate census for them to complete"
msgstr ""

#: app/views/handlers/individual_response.py:383
#: app/views/handlers/individual_response.py:391
msgid "I will ask them to answer their own questions"
msgstr ""

#: app/views/handlers/individual_response.py:387
#: app/views/handlers/individual_response.py:395
msgid "They will need the household access code from the letter we sent you"
msgstr ""

#: app/views/handlers/individual_response.py:393
#: app/views/handlers/individual_response.py:401
msgid "I will answer for {person_name}"
msgstr ""

#: app/views/handlers/individual_response.py:505
#: app/views/handlers/individual_response.py:513
msgid "Do you want to send an individual access code for {person_name} by post?"
msgstr ""

#: app/views/handlers/individual_response.py:511
#: app/views/handlers/individual_response.py:519
msgid ""
"A letter with an individual access code will be sent to your registered "
"household address"
msgstr ""

#: app/views/handlers/individual_response.py:518
#: app/views/handlers/individual_response.py:526
msgid ""
"The letter will be addressed to <strong>Individual Resident</strong> "
"instead of the name provided"
msgstr ""

#: app/views/handlers/individual_response.py:531
#: app/views/handlers/individual_response.py:539
msgid "Yes, send the access code by post"
msgstr ""

#: app/views/handlers/individual_response.py:535
#: app/views/handlers/individual_response.py:543
msgid "No, send it another way"
msgstr ""

#: app/views/handlers/individual_response.py:587
#: app/views/handlers/individual_response.py:595
msgid "Confirm address"
msgstr ""

#: app/views/handlers/individual_response.py:649
#: app/views/handlers/individual_response.py:682
#: app/views/handlers/individual_response.py:658
#: app/views/handlers/individual_response.py:691
msgid "Separate Census"
msgstr ""

#: app/views/handlers/individual_response.py:653
#: app/views/handlers/individual_response.py:662
msgid "Who do you need to request a separate census for?"
msgstr ""

#: app/views/handlers/individual_response.py:704
#: app/views/handlers/individual_response.py:713
msgid "What is <em>{person_name_possessive}</em> mobile number?"
msgstr ""

#: app/views/handlers/individual_response.py:714
#: app/views/handlers/individual_response.py:723
msgid "UK mobile number"
msgstr ""

#: app/views/handlers/individual_response.py:715
#: app/views/handlers/individual_response.py:724
msgid "This will not be stored and only used once to send the access code"
msgstr ""

#: app/views/handlers/individual_response.py:767
#: app/views/handlers/individual_response.py:776
msgid "Mobile number"
msgstr ""

#: app/views/handlers/individual_response.py:813
#: app/views/handlers/individual_response.py:822
msgid "Is this mobile number correct?"
msgstr ""

#: app/views/handlers/individual_response.py:822
#: app/views/handlers/individual_response.py:831
msgid "Yes, send the text"
msgstr ""

#: app/views/handlers/individual_response.py:826
#: app/views/handlers/individual_response.py:835
msgid "No, I need to change it"
msgstr ""

#: app/views/handlers/individual_response.py:860
#: app/views/handlers/individual_response.py:869
msgid "Confirm mobile number"
msgstr ""

Expand Down Expand Up @@ -730,12 +730,21 @@ msgstr ""
msgid "If you have started a survey, your answers have been saved."
msgstr ""

#: templates/errors/500.html:9
#: templates/errors/500.html:9 templates/errors/fulfilment-request.html:9
msgid ""
"<a href='{url}'>Contact us</a> if you need to speak to someone about your"
" survey."
msgstr ""

#: templates/errors/fulfilment-request.html:3
#: templates/errors/fulfilment-request.html:6
msgid "Sorry, there was a problem sending the access code"
msgstr ""

#: templates/errors/fulfilment-request.html:8
msgid "If this problem keeps happening, please contact us for help."
msgstr ""

#: templates/errors/no-cookie.html:3
msgid "Page is not available"
msgstr ""
Expand Down
16 changes: 13 additions & 3 deletions app/views/handlers/individual_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from app.forms.validators import sanitise_mobile_number
from app.helpers.template_helpers import render_template
from app.helpers.url_param_serializer import URLParamSerializer
from app.publisher.exceptions import PublicationFailed
from app.questionnaire.placeholder_renderer import PlaceholderRenderer
from app.questionnaire.router import Router
from app.views.contexts.question import build_question_context
Expand All @@ -24,6 +25,10 @@
GB_NIR_REGION_CODE = "GB-NIR"


class FulfilmentRequestFailedException(Exception):
pass


class IndividualResponseHandler:
_person_name_transform: Mapping = {
"arguments": {
Expand Down Expand Up @@ -155,9 +160,12 @@ def get_context(self):
def _publish_fulfilment_request(self, mobile_number=None):
topic_id = current_app.config["EQ_FULFILMENT_TOPIC_ID"]
fulfilment_request = FulfilmentRequest(self._metadata, mobile_number)
return current_app.eq["publisher"].publish(
topic_id, message=fulfilment_request.payload
)
try:
return current_app.eq["publisher"].publish(
topic_id, message=fulfilment_request.payload
)
except PublicationFailed:
raise FulfilmentRequestFailedException

def handle_get(self):
return render_template(
Expand Down Expand Up @@ -588,6 +596,7 @@ def handle_post(self):
if self.selected_option == self.confirm_option:
self._update_section_status(CompletionStatus.INDIVIDUAL_RESPONSE_REQUESTED)
self._publish_fulfilment_request()

return redirect(
url_for(
"individual_response.individual_response_post_address_confirmation",
Expand Down Expand Up @@ -861,6 +870,7 @@ def handle_post(self):
if self.selected_option == self.confirm_option:
self._update_section_status(CompletionStatus.INDIVIDUAL_RESPONSE_REQUESTED)
self._publish_fulfilment_request(self.mobile_number)

return redirect(
url_for(
"individual_response.individual_response_text_message_confirmation",
Expand Down
10 changes: 10 additions & 0 deletions templates/errors/fulfilment-request.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends 'layouts/_base.html' %}

{% set page_title = _("Sorry, there was a problem sending the access code") %}

{% block main %}
<h1>{{ _("Sorry, there was a problem sending the access code") }}</h1>
<p data-qa="retry">{{ _("You can try to <a href='{retry_url}'>request a new access code again</a>.".format(retry_url = retry_url)) }}</p>
<p>{{ _("If this problem keeps happening, please contact us for help.") }}</p>
<p>{{ _("<a href='{url}'>Contact us</a> if you need to speak to someone about your survey.").format(url = contact_us_url) }}</p>
{% endblock %}
Loading

0 comments on commit 410cf2c

Please sign in to comment.