mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 15:46:07 -05:00
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.
This commit is contained in:
@@ -28,6 +28,7 @@ from app.models import (
|
|||||||
NOTIFICATION_SENDING,
|
NOTIFICATION_SENDING,
|
||||||
NOTIFICATION_STATUS_TYPES_COMPLETED
|
NOTIFICATION_STATUS_TYPES_COMPLETED
|
||||||
)
|
)
|
||||||
|
from app.serialised_models import SerialisedTemplate
|
||||||
|
|
||||||
|
|
||||||
def send_sms_to_provider(notification):
|
def send_sms_to_provider(notification):
|
||||||
@@ -40,10 +41,12 @@ def send_sms_to_provider(notification):
|
|||||||
if notification.status == 'created':
|
if notification.status == 'created':
|
||||||
provider = provider_to_use(SMS_TYPE, notification.international)
|
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 = SMSMessageTemplate(
|
||||||
template_model.__dict__,
|
template_dict,
|
||||||
values=notification.personalisation,
|
values=notification.personalisation,
|
||||||
prefix=service.name,
|
prefix=service.name,
|
||||||
show_prefix=service.prefix_sms,
|
show_prefix=service.prefix_sms,
|
||||||
@@ -92,7 +95,9 @@ def send_email_to_provider(notification):
|
|||||||
if notification.status == 'created':
|
if notification.status == 'created':
|
||||||
provider = provider_to_use(EMAIL_TYPE)
|
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(
|
html_email = HTMLEmailTemplate(
|
||||||
template_dict,
|
template_dict,
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import json
|
||||||
import uuid
|
import uuid
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
@@ -730,3 +731,53 @@ def test_send_email_to_provider_uses_reply_to_from_notification(
|
|||||||
html_body=ANY,
|
html_body=ANY,
|
||||||
reply_to_address="test@test.com"
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user