Use same template to validate and send notification

To be absolutely sure that we can send a message we should also validate
it using the same template class that we use to render it.
This commit is contained in:
Chris Hill-Scott
2020-04-06 14:25:43 +01:00
parent 8c8c8b6328
commit 025ac3ea89
9 changed files with 136 additions and 46 deletions

View File

@@ -983,9 +983,14 @@ class TemplateBase(db.Model):
if self.template_type == LETTER_TYPE:
return LetterPrintTemplate(
self.__dict__,
contact_block=self.service.get_default_letter_contact(),
contact_block=self.get_reply_to_text(),
)
def _as_utils_template_with_personalisation(self, values):
template = self._as_utils_template()
template.values = values
return template
def serialize(self):
serialized = {
"id": str(self.id),

View File

@@ -32,11 +32,10 @@ from app.dao.notifications_dao import (
)
from app.v2.errors import BadRequestError
from app.utils import get_template_instance
def create_content_for_notification(template, personalisation):
template_object = get_template_instance(template.__dict__, personalisation)
template_object = template._as_utils_template_with_personalisation(personalisation)
check_placeholders(template_object)
return template_object

View File

@@ -4,6 +4,7 @@ from flask import (
request,
current_app
)
from notifications_utils.template import WithSubjectTemplate
from app import api_user, authenticated_service
from app.config import QueueNames
@@ -35,7 +36,7 @@ from app.schemas import (
notifications_filter_schema
)
from app.service.utils import service_allowed_to_send_to
from app.utils import pagination_links, get_template_instance, get_public_notify_type_text
from app.utils import pagination_links, get_public_notify_type_text
from notifications_utils import SMS_CHAR_COUNT_LIMIT
from notifications_utils.recipients import get_international_phone_info
@@ -149,13 +150,15 @@ def send_notification(notification_type):
def get_notification_return_data(notification_id, notification, template):
output = {
'body': str(template),
'template_version': notification['template_version'],
'notification': {'id': notification_id}
}
if template.template_type == 'email':
output.update({'subject': template.subject})
if template.template_type == SMS_TYPE:
output['body'] = str(template)
else:
output['body'] = WithSubjectTemplate.__str__(template)
output['subject'] = template.subject
return output
@@ -187,7 +190,7 @@ def _service_allowed_to_send_to(notification, service):
def create_template_object_for_notification(template, personalisation):
template_object = get_template_instance(template.__dict__, personalisation)
template_object = template._as_utils_template_with_personalisation(personalisation)
if template_object.missing_data:
message = 'Missing personalisation: {}'.format(", ".join(template_object.missing_data))

View File

@@ -10,7 +10,7 @@ from flask import (
request)
from notifications_utils import SMS_CHAR_COUNT_LIMIT
from notifications_utils.pdf import extract_page_from_pdf
from notifications_utils.template import SMSMessageTemplate
from notifications_utils.template import SMSMessageTemplate, WithSubjectTemplate
from requests import post as requests_post
from sqlalchemy.orm.exc import NoResultFound
@@ -38,7 +38,7 @@ from app.notifications.validators import service_has_permission, check_reply_to
from app.schema_validation import validate
from app.schemas import (template_schema, template_history_schema)
from app.template.template_schemas import post_create_template_schema
from app.utils import get_template_instance, get_public_notify_type_text
from app.utils import get_public_notify_type_text
template_blueprint = Blueprint('template', __name__, url_prefix='/service/<uuid:service_id>/template')
@@ -166,7 +166,9 @@ def get_template_by_id_and_service_id(service_id, template_id):
def preview_template_by_id_and_service_id(service_id, template_id):
fetched_template = dao_get_template_by_id_and_service_id(template_id=template_id, service_id=service_id)
data = template_schema.dump(fetched_template).data
template_object = get_template_instance(data, values=request.args.to_dict())
template_object = fetched_template._as_utils_template_with_personalisation(
request.args.to_dict()
)
if template_object.missing_data:
raise InvalidRequest(
@@ -175,7 +177,11 @@ def preview_template_by_id_and_service_id(service_id, template_id):
]}, status_code=400
)
data['subject'], data['content'] = template_object.subject, str(template_object)
data['subject'] = template_object.subject
if template_object.template_type == SMS_TYPE:
data['content'] = str(template_object)
else:
data['content'] = WithSubjectTemplate.__str__(template_object)
return jsonify(data)

View File

@@ -8,6 +8,7 @@ from flask import request, jsonify, current_app, abort
from notifications_utils.recipients import (
format_postcode_for_printing, is_a_real_uk_postcode, try_validate_and_format_phone_number
)
from notifications_utils.template import WithSubjectTemplate
from app import (
api_user,
@@ -166,23 +167,25 @@ def post_notification(notification_type):
if notification_type == SMS_TYPE:
create_resp_partial = functools.partial(
create_post_sms_response_from_notification,
from_number=reply_to
from_number=reply_to,
content=str(template_with_content),
)
elif notification_type == EMAIL_TYPE:
create_resp_partial = functools.partial(
create_post_email_response_from_notification,
subject=template_with_content.subject,
email_from='{}@{}'.format(authenticated_service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN'])
email_from='{}@{}'.format(authenticated_service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']),
content=WithSubjectTemplate.__str__(template_with_content),
)
elif notification_type == LETTER_TYPE:
create_resp_partial = functools.partial(
create_post_letter_response_from_notification,
subject=template_with_content.subject,
content=WithSubjectTemplate.__str__(template_with_content),
)
resp = create_resp_partial(
notification=notification,
content=str(template_with_content),
url_root=request.url_root,
scheduled_for=scheduled_for
)

View File

@@ -3,7 +3,6 @@ from flask import jsonify, request
from app import authenticated_service
from app.dao import templates_dao
from app.schema_validation import validate
from app.utils import get_template_instance
from app.v2.errors import BadRequestError
from app.v2.template import v2_template_blueprint
from app.v2.template.template_schemas import (
@@ -29,8 +28,9 @@ def post_template_preview(template_id):
template = templates_dao.dao_get_template_by_id_and_service_id(
template_id, authenticated_service.id)
template_object = get_template_instance(
template.__dict__, values=data.get('personalisation'))
template_object = template._as_utils_template_with_personalisation(
data.get('personalisation')
)
check_placeholders(template_object)