mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-22 16:31:15 -05:00
Incorporate breaking utils changes
The `.replaced…` methods on instances of `Template` were removed in https://github.com/alphagov/notifications-utils/pull/84
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
|
from notifications_utils.field import Field
|
||||||
from notifications_utils.recipients import (
|
from notifications_utils.recipients import (
|
||||||
validate_and_format_phone_number
|
validate_and_format_phone_number
|
||||||
)
|
)
|
||||||
from notifications_utils.template import Template, get_sms_fragment_count
|
from notifications_utils.template import Template
|
||||||
from notifications_utils.renderers import HTMLEmail, PlainTextEmail, SMSMessage
|
from notifications_utils.renderers import HTMLEmail, PlainTextEmail, SMSMessage
|
||||||
|
|
||||||
from app import clients, statsd_client, create_uuid
|
from app import clients, statsd_client, create_uuid
|
||||||
@@ -35,11 +36,11 @@ def send_sms_to_provider(notification):
|
|||||||
else:
|
else:
|
||||||
provider.send_sms(
|
provider.send_sms(
|
||||||
to=validate_and_format_phone_number(notification.to),
|
to=validate_and_format_phone_number(notification.to),
|
||||||
content=template.replaced,
|
content=template.rendered,
|
||||||
reference=str(notification.id),
|
reference=str(notification.id),
|
||||||
sender=service.sms_sender
|
sender=service.sms_sender
|
||||||
)
|
)
|
||||||
notification.billable_units = get_sms_fragment_count(template.replaced_content_count)
|
notification.billable_units = template.sms_fragment_count
|
||||||
|
|
||||||
notification.sent_at = datetime.utcnow()
|
notification.sent_at = datetime.utcnow()
|
||||||
notification.sent_by = provider.get_name()
|
notification.sent_by = provider.get_name()
|
||||||
@@ -83,9 +84,9 @@ def send_email_to_provider(notification):
|
|||||||
reference = provider.send_email(
|
reference = provider.send_email(
|
||||||
from_address,
|
from_address,
|
||||||
notification.to,
|
notification.to,
|
||||||
plain_text_email.replaced_subject,
|
str(Field(plain_text_email.subject, notification.personalisation)),
|
||||||
body=plain_text_email.replaced,
|
body=plain_text_email.rendered,
|
||||||
html_body=html_email.replaced,
|
html_body=html_email.rendered,
|
||||||
reply_to_address=service.reply_to_email_address,
|
reply_to_address=service.reply_to_email_address,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ def create_content_for_notification(template, personalisation):
|
|||||||
check_placeholders(template_object)
|
check_placeholders(template_object)
|
||||||
|
|
||||||
if template_object.template_type == SMS_TYPE:
|
if template_object.template_type == SMS_TYPE:
|
||||||
check_sms_content_char_count(template_object.replaced_content_count)
|
check_sms_content_char_count(template_object.content_count)
|
||||||
return template_object
|
return template_object
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ from flask import (
|
|||||||
current_app,
|
current_app,
|
||||||
json
|
json
|
||||||
)
|
)
|
||||||
|
from notifications_utils.field import Field
|
||||||
from notifications_utils.renderers import PassThrough
|
from notifications_utils.renderers import PassThrough
|
||||||
from notifications_utils.template import Template
|
from notifications_utils.template import Template
|
||||||
|
|
||||||
@@ -254,13 +255,13 @@ def send_notification(notification_type):
|
|||||||
|
|
||||||
def get_notification_return_data(notification_id, notification, template):
|
def get_notification_return_data(notification_id, notification, template):
|
||||||
output = {
|
output = {
|
||||||
'body': template.replaced,
|
'body': template.rendered,
|
||||||
'template_version': notification['template_version'],
|
'template_version': notification['template_version'],
|
||||||
'notification': {'id': notification_id}
|
'notification': {'id': notification_id}
|
||||||
}
|
}
|
||||||
|
|
||||||
if template.template_type == 'email':
|
if template.template_type == 'email':
|
||||||
output.update({'subject': template.replaced_subject})
|
output.update({'subject': str(Field(template.subject, template.values))})
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@@ -304,7 +305,7 @@ def create_template_object_for_notification(template, personalisation):
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
template_object.template_type == SMS_TYPE and
|
template_object.template_type == SMS_TYPE and
|
||||||
template_object.replaced_content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT')
|
template_object.content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT')
|
||||||
):
|
):
|
||||||
char_count = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
|
char_count = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
|
||||||
message = 'Content has a character count greater than the limit of {}'.format(char_count)
|
message = 'Content has a character count greater than the limit of {}'.format(char_count)
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ from marshmallow import (
|
|||||||
)
|
)
|
||||||
from marshmallow_sqlalchemy import field_for
|
from marshmallow_sqlalchemy import field_for
|
||||||
|
|
||||||
|
from notifications_utils.field import Field
|
||||||
from notifications_utils.recipients import (
|
from notifications_utils.recipients import (
|
||||||
validate_email_address,
|
validate_email_address,
|
||||||
InvalidEmailError,
|
InvalidEmailError,
|
||||||
@@ -395,10 +396,10 @@ class NotificationWithPersonalisationSchema(NotificationWithTemplateSchema):
|
|||||||
in_data['personalisation'],
|
in_data['personalisation'],
|
||||||
renderer=PassThrough()
|
renderer=PassThrough()
|
||||||
)
|
)
|
||||||
in_data['body'] = template.replaced
|
in_data['body'] = template.rendered
|
||||||
template_type = in_data['template']['template_type']
|
template_type = in_data['template']['template_type']
|
||||||
if template_type == 'email':
|
if template_type == 'email':
|
||||||
in_data['subject'] = template.replaced_subject
|
in_data['subject'] = str(Field(template.subject, in_data['personalisation']))
|
||||||
in_data['content_char_count'] = None
|
in_data['content_char_count'] = None
|
||||||
else:
|
else:
|
||||||
in_data['content_char_count'] = len(in_data['body'])
|
in_data['content_char_count'] = len(in_data['body'])
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ from app.dao.templates_dao import (
|
|||||||
dao_get_all_templates_for_service,
|
dao_get_all_templates_for_service,
|
||||||
dao_get_template_versions
|
dao_get_template_versions
|
||||||
)
|
)
|
||||||
|
from notifications_utils.field import Field
|
||||||
from notifications_utils.template import Template
|
from notifications_utils.template import Template
|
||||||
from notifications_utils.renderers import PassThrough
|
from notifications_utils.renderers import PassThrough
|
||||||
from app.dao.services_dao import dao_fetch_service_by_id
|
from app.dao.services_dao import dao_fetch_service_by_id
|
||||||
@@ -108,7 +109,10 @@ def preview_template_by_id_and_service_id(service_id, template_id):
|
|||||||
]}, status_code=400
|
]}, status_code=400
|
||||||
)
|
)
|
||||||
|
|
||||||
data['subject'], data['content'] = template_object.replaced_subject, template_object.replaced
|
data['subject'], data['content'] = (
|
||||||
|
str(Field(template_object.subject, template_object.values)),
|
||||||
|
template_object.rendered
|
||||||
|
)
|
||||||
|
|
||||||
return jsonify(data)
|
return jsonify(data)
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ def post_sms_notification():
|
|||||||
send_notification_to_queue(notification, service.research_mode)
|
send_notification_to_queue(notification, service.research_mode)
|
||||||
|
|
||||||
resp = create_post_sms_response_from_notification(notification,
|
resp = create_post_sms_response_from_notification(notification,
|
||||||
template_with_content.replaced,
|
template_with_content.rendered,
|
||||||
service.sms_sender,
|
service.sms_sender,
|
||||||
request.url_root)
|
request.url_root)
|
||||||
return jsonify(resp), 201
|
return jsonify(resp), 201
|
||||||
@@ -70,7 +70,7 @@ def post_email_notification():
|
|||||||
send_notification_to_queue(notification, service.research_mode)
|
send_notification_to_queue(notification, service.research_mode)
|
||||||
|
|
||||||
resp = create_post_email_response_from_notification(notification=notification,
|
resp = create_post_email_response_from_notification(notification=notification,
|
||||||
content=template_with_content.replaced,
|
content=template_with_content.rendered,
|
||||||
subject=template_with_content.subject,
|
subject=template_with_content.subject,
|
||||||
email_from=service.email_from,
|
email_from=service.email_from,
|
||||||
url_root=request.url_root)
|
url_root=request.url_root)
|
||||||
@@ -89,5 +89,5 @@ def __validate_template(form, service, notification_type):
|
|||||||
check_template_is_for_notification_type(notification_type, template.template_type)
|
check_template_is_for_notification_type(notification_type, template.template_type)
|
||||||
check_template_is_active(template)
|
check_template_is_active(template)
|
||||||
template_with_content = create_content_for_notification(template, form.get('personalisation', {}))
|
template_with_content = create_content_for_notification(template, form.get('personalisation', {}))
|
||||||
check_sms_content_char_count(template_with_content.replaced_content_count)
|
check_sms_content_char_count(template_with_content.content_count)
|
||||||
return template, template_with_content
|
return template, template_with_content
|
||||||
|
|||||||
@@ -17,14 +17,14 @@ from tests.app.conftest import sample_notification, sample_template, sample_emai
|
|||||||
def test_create_content_for_notification_passes(sample_email_template):
|
def test_create_content_for_notification_passes(sample_email_template):
|
||||||
template = Template.query.get(sample_email_template.id)
|
template = Template.query.get(sample_email_template.id)
|
||||||
content = create_content_for_notification(template, None)
|
content = create_content_for_notification(template, None)
|
||||||
assert content.replaced == template.content
|
assert content.rendered == template.content
|
||||||
|
|
||||||
|
|
||||||
def test_create_content_for_notification_with_placeholders_passes(sample_template_with_placeholders):
|
def test_create_content_for_notification_with_placeholders_passes(sample_template_with_placeholders):
|
||||||
template = Template.query.get(sample_template_with_placeholders.id)
|
template = Template.query.get(sample_template_with_placeholders.id)
|
||||||
content = create_content_for_notification(template, {'name': 'Bobby'})
|
content = create_content_for_notification(template, {'name': 'Bobby'})
|
||||||
assert content.content == template.content
|
assert content.content == template.content
|
||||||
assert 'Bobby' in content.replaced
|
assert 'Bobby' in content.rendered
|
||||||
|
|
||||||
|
|
||||||
def test_create_content_for_notification_fails_with_missing_personalisation(sample_template_with_placeholders):
|
def test_create_content_for_notification_fails_with_missing_personalisation(sample_template_with_placeholders):
|
||||||
|
|||||||
Reference in New Issue
Block a user