From 5ebeb9937ad801f0db799acd573867bc168bc404 Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Mon, 14 Jan 2019 17:45:56 +0000 Subject: [PATCH] Avoid call to database to get template in persist_notifications --- app/celery/tasks.py | 1 + app/notifications/process_letter_notifications.py | 1 + app/notifications/process_notifications.py | 10 ++++------ app/notifications/rest.py | 1 + app/service/send_notification.py | 1 + tests/app/notifications/test_process_notification.py | 1 + tests/app/service/test_send_one_off_notification.py | 4 ++++ 7 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/celery/tasks.py b/app/celery/tasks.py index 96d26fe7d..1948c0964 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -307,6 +307,7 @@ def save_letter( saved_notification = persist_notification( template_id=notification['template'], template_version=notification['template_version'], + template_postage=template.postage, recipient=recipient, service=service, personalisation=notification['personalisation'], diff --git a/app/notifications/process_letter_notifications.py b/app/notifications/process_letter_notifications.py index 94e52bbd8..06d1127bf 100644 --- a/app/notifications/process_letter_notifications.py +++ b/app/notifications/process_letter_notifications.py @@ -7,6 +7,7 @@ def create_letter_notification(letter_data, template, api_key, status, reply_to_ notification = persist_notification( template_id=template.id, template_version=template.version, + template_postage=template.postage, # we only accept addresses_with_underscores from the API (from CSV we also accept dashes, spaces etc) recipient=letter_data['personalisation']['address_line_1'], service=template.service, diff --git a/app/notifications/process_notifications.py b/app/notifications/process_notifications.py index 88cec5b0d..f8850c3e9 100644 --- a/app/notifications/process_notifications.py +++ b/app/notifications/process_notifications.py @@ -32,8 +32,6 @@ from app.dao.notifications_dao import ( dao_created_scheduled_notification ) -from app.dao.templates_dao import dao_get_template_by_id - from app.v2.errors import BadRequestError from app.utils import ( cache_key_for_service_template_counter, @@ -76,7 +74,8 @@ def persist_notification( status=NOTIFICATION_CREATED, reply_to_text=None, billable_units=None, - postage=None + postage=None, + template_postage=None ): notification_created_at = created_at or datetime.utcnow() if not notification_id: @@ -116,9 +115,8 @@ def persist_notification( if postage: notification.postage = postage else: - template = dao_get_template_by_id(template_id, template_version) - if service.has_permission(CHOOSE_POSTAGE) and template.postage: - notification.postage = template.postage + if service.has_permission(CHOOSE_POSTAGE) and template_postage: + notification.postage = template_postage else: notification.postage = service.postage diff --git a/app/notifications/rest.py b/app/notifications/rest.py index 04286688a..aa4be0ea9 100644 --- a/app/notifications/rest.py +++ b/app/notifications/rest.py @@ -124,6 +124,7 @@ def send_notification(notification_type): simulated = simulated_recipient(notification_form['to'], notification_type) notification_model = persist_notification(template_id=template.id, template_version=template.version, + template_postage=template.postage, recipient=request.get_json()['to'], service=authenticated_service, personalisation=notification_form.get('personalisation', None), diff --git a/app/service/send_notification.py b/app/service/send_notification.py index a00d151a4..26307b8c3 100644 --- a/app/service/send_notification.py +++ b/app/service/send_notification.py @@ -77,6 +77,7 @@ def send_one_off_notification(service_id, post_data): notification = persist_notification( template_id=template.id, template_version=template.version, + template_postage=template.postage, recipient=post_data['to'], service=service, personalisation=personalisation, diff --git a/tests/app/notifications/test_process_notification.py b/tests/app/notifications/test_process_notification.py index 192644bde..9710146e0 100644 --- a/tests/app/notifications/test_process_notification.py +++ b/tests/app/notifications/test_process_notification.py @@ -504,6 +504,7 @@ def test_persist_letter_notification_finds_correct_postage( persist_notification( template_id=template.id, template_version=template.version, + template_postage=template.postage, recipient="Jane Doe, 10 Downing Street, London", service=service, personalisation=None, diff --git a/tests/app/service/test_send_one_off_notification.py b/tests/app/service/test_send_one_off_notification.py index 0c20611dc..b70459fc8 100644 --- a/tests/app/service/test_send_one_off_notification.py +++ b/tests/app/service/test_send_one_off_notification.py @@ -90,6 +90,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_sms( persist_mock.assert_called_once_with( template_id=template.id, template_version=template.version, + template_postage=None, recipient=post_data['to'], service=template.service, personalisation={'name': 'foo'}, @@ -127,6 +128,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_email( persist_mock.assert_called_once_with( template_id=template.id, template_version=template.version, + template_postage=None, recipient=post_data['to'], service=template.service, personalisation={'name': 'foo'}, @@ -153,6 +155,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_letter( template = create_template( service=service, template_type=LETTER_TYPE, + postage='first', subject="Test subject", content="Hello (( Name))\nYour thing is due soon", ) @@ -174,6 +177,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_letter( persist_mock.assert_called_once_with( template_id=template.id, template_version=template.version, + template_postage='first', recipient=post_data['to'], service=template.service, personalisation=post_data['personalisation'],