From 200f8aad812398312fc64ea7ffd67e4ac03b1906 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Thu, 11 Feb 2021 16:08:46 +0000 Subject: [PATCH] Use the cached template object. By adding SerialisedTemplate we can avoid a database call for the template. This is useful when sending many many emails/sms for the same template/version. --- app/delivery/send_to_providers.py | 11 +++-- tests/app/delivery/test_send_to_providers.py | 51 ++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/app/delivery/send_to_providers.py b/app/delivery/send_to_providers.py index b30e1aeb2..6fdf31d86 100644 --- a/app/delivery/send_to_providers.py +++ b/app/delivery/send_to_providers.py @@ -28,6 +28,7 @@ from app.models import ( NOTIFICATION_SENDING, NOTIFICATION_STATUS_TYPES_COMPLETED ) +from app.serialised_models import SerialisedTemplate def send_sms_to_provider(notification): @@ -40,10 +41,12 @@ def send_sms_to_provider(notification): if notification.status == 'created': provider = provider_to_use(SMS_TYPE, notification.international) - template_model = dao_get_template_by_id(notification.template_id, notification.template_version) + template_dict = SerialisedTemplate.get_dict(template_id=notification.template_id, + service_id=service_id, + version=notification.template_version)['data'] template = SMSMessageTemplate( - template_model.__dict__, + template_dict, values=notification.personalisation, prefix=service.name, show_prefix=service.prefix_sms, @@ -92,7 +95,9 @@ def send_email_to_provider(notification): if notification.status == 'created': provider = provider_to_use(EMAIL_TYPE) - template_dict = dao_get_template_by_id(notification.template_id, notification.template_version).__dict__ + template_dict = SerialisedTemplate.get_dict(template_id=notification.template_id, + service_id=service_id, + version=notification.template_version)['data'] html_email = HTMLEmailTemplate( template_dict, diff --git a/tests/app/delivery/test_send_to_providers.py b/tests/app/delivery/test_send_to_providers.py index 8be87fd6a..ad8e98f0c 100644 --- a/tests/app/delivery/test_send_to_providers.py +++ b/tests/app/delivery/test_send_to_providers.py @@ -1,3 +1,4 @@ +import json import uuid from collections import namedtuple from datetime import datetime, timedelta @@ -730,3 +731,53 @@ def test_send_email_to_provider_uses_reply_to_from_notification( html_body=ANY, reply_to_address="test@test.com" ) + + +def test_send_sms_to_provider_should_return_template_if_found_in_redis( + mocker, client, sample_template +): + + from app.schemas import template_schema + template_dict = template_schema.dump(sample_template).data + + notification = create_notification(template=sample_template, + to_field= '+447700900855', + normalised_to=validate_and_format_phone_number('+447700900855')) + mocker.patch( + 'app.redis_store.get', + side_effect=[ + json.dumps({'data': template_dict}).encode('utf-8'), + ], + ) + mock_get_template = mocker.patch( + 'app.dao.templates_dao.dao_get_template_by_id_and_service_id' + ) + mocker.patch('app.mmg_client.send_sms') + # mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') + + send_to_providers.send_sms_to_provider(notification) + assert mock_get_template.called is False + + +def test_send_email_to_provider_should_return_template_if_found_in_redis( + mocker, client, sample_email_template +): + from app.schemas import template_schema + template_dict = template_schema.dump(sample_email_template).data + + notification = create_notification(template=sample_email_template, + to_field='test@example.com', + normalised_to='test@example.com') + mocker.patch( + 'app.redis_store.get', + side_effect=[ + json.dumps({'data': template_dict}).encode('utf-8'), + ], + ) + mock_get_template = mocker.patch( + 'app.dao.templates_dao.dao_get_template_by_id_and_service_id' + ) + mocker.patch('app.aws_ses_client.send_email', return_value='reference') + + send_to_providers.send_email_to_provider(notification) + assert mock_get_template.called is False