Merge pull request #764 from alphagov/more-breaking-utils-changes

Update utils to 12.1.1
This commit is contained in:
Chris Hill-Scott
2016-12-14 17:46:15 +01:00
committed by GitHub
14 changed files with 70 additions and 99 deletions

View File

@@ -1,12 +1,10 @@
from datetime import datetime
from flask import current_app
from notifications_utils.field import Field
from notifications_utils.recipients import (
validate_and_format_phone_number
)
from notifications_utils.template import Template
from notifications_utils.renderers import HTMLEmail, PlainTextEmail, SMSMessage
from notifications_utils.template import HTMLEmailTemplate, PlainTextEmailTemplate, SMSMessageTemplate
from app import clients, statsd_client, create_uuid
from app.dao.notifications_dao import dao_update_notification
@@ -23,10 +21,11 @@ def send_sms_to_provider(notification):
provider = provider_to_use(SMS_TYPE, notification.id)
if notification.status == 'created':
template_model = dao_get_template_by_id(notification.template_id, notification.template_version)
template = Template(
template = SMSMessageTemplate(
template_model.__dict__,
values={} if not notification.personalisation else notification.personalisation,
renderer=SMSMessage(prefix=service.name, sender=service.sms_sender)
values=notification.personalisation,
prefix=service.name,
sender=service.sms_sender
)
if service.research_mode or notification.key_type == KEY_TYPE_TEST:
send_sms_response.apply_async(
@@ -36,11 +35,11 @@ def send_sms_to_provider(notification):
else:
provider.send_sms(
to=validate_and_format_phone_number(notification.to),
content=template.rendered,
content=str(template),
reference=str(notification.id),
sender=service.sms_sender
)
notification.billable_units = template.sms_fragment_count
notification.billable_units = template.fragment_count
notification.sent_at = datetime.utcnow()
notification.sent_by = provider.get_name()
@@ -60,16 +59,15 @@ def send_email_to_provider(notification):
if notification.status == 'created':
template_dict = dao_get_template_by_id(notification.template_id, notification.template_version).__dict__
html_email = Template(
html_email = HTMLEmailTemplate(
template_dict,
values=notification.personalisation,
renderer=get_html_email_renderer(service)
**get_html_email_options(service)
)
plain_text_email = Template(
plain_text_email = PlainTextEmailTemplate(
template_dict,
values=notification.personalisation,
renderer=PlainTextEmail()
values=notification.personalisation
)
if service.research_mode or notification.key_type == KEY_TYPE_TEST:
@@ -84,9 +82,9 @@ def send_email_to_provider(notification):
reference = provider.send_email(
from_address,
notification.to,
str(Field(plain_text_email.subject, notification.personalisation)),
body=plain_text_email.rendered,
html_body=html_email.rendered,
plain_text_email.subject,
body=str(plain_text_email),
html_body=str(html_email),
reply_to_address=service.reply_to_email_address,
)
@@ -117,7 +115,7 @@ def provider_to_use(notification_type, notification_id):
return clients.get_client_by_name_and_type(active_providers_in_order[0].identifier, notification_type)
def get_html_email_renderer(service):
def get_html_email_options(service):
govuk_banner = service.branding != BRANDING_ORG
if service.organisation:
logo = '{}{}{}'.format(
@@ -133,4 +131,4 @@ def get_html_email_renderer(service):
else:
branding = {}
return HTMLEmail(govuk_banner=govuk_banner, **branding)
return dict(govuk_banner=govuk_banner, **branding)

View File

@@ -1,8 +1,6 @@
from datetime import datetime
from flask import current_app
from notifications_utils.renderers import PassThrough
from notifications_utils.template import Template
from app import redis_store
from app.celery import provider_tasks
@@ -11,14 +9,11 @@ from app.dao.notifications_dao import dao_create_notification, dao_delete_notifi
from app.models import SMS_TYPE, Notification, KEY_TYPE_TEST, EMAIL_TYPE
from app.notifications.validators import check_sms_content_char_count
from app.v2.errors import BadRequestError, SendNotificationToQueueError
from app.utils import get_template_instance
def create_content_for_notification(template, personalisation):
template_object = Template(
template.__dict__,
personalisation,
renderer=PassThrough()
)
template_object = get_template_instance(template.__dict__, personalisation)
check_placeholders(template_object)
if template_object.template_type == SMS_TYPE:

View File

@@ -7,9 +7,6 @@ from flask import (
current_app,
json
)
from notifications_utils.field import Field
from notifications_utils.renderers import PassThrough
from notifications_utils.template import Template
from app import api_user, create_uuid, statsd_client
from app.clients.email.aws_ses import get_aws_responses
@@ -36,7 +33,7 @@ from app.schemas import (
day_schema
)
from app.service.utils import service_allowed_to_send_to
from app.utils import pagination_links
from app.utils import pagination_links, get_template_instance
notifications = Blueprint('notifications', __name__)
@@ -255,13 +252,13 @@ def send_notification(notification_type):
def get_notification_return_data(notification_id, notification, template):
output = {
'body': template.rendered,
'body': str(template),
'template_version': notification['template_version'],
'notification': {'id': notification_id}
}
if template.template_type == 'email':
output.update({'subject': str(Field(template.subject, template.values))})
output.update({'subject': template.subject})
return output
@@ -288,11 +285,8 @@ def _service_allowed_to_send_to(notification, service):
def create_template_object_for_notification(template, personalisation):
template_object = Template(
template.__dict__,
personalisation,
renderer=PassThrough()
)
template_object = get_template_instance(template.__dict__, personalisation)
if template_object.missing_data:
message = 'Missing personalisation: {}'.format(", ".join(template_object.missing_data))
errors = {'template': [message]}

View File

@@ -15,7 +15,6 @@ from marshmallow import (
)
from marshmallow_sqlalchemy import field_for
from notifications_utils.field import Field
from notifications_utils.recipients import (
validate_email_address,
InvalidEmailError,
@@ -24,11 +23,10 @@ from notifications_utils.recipients import (
validate_and_format_phone_number
)
from notifications_utils.renderers import PassThrough
from app import ma
from app import models
from app.dao.permissions_dao import permission_dao
from app.utils import get_template_instance
def _validate_positive_number(value, msg="Not a positive integer"):
@@ -390,19 +388,13 @@ class NotificationWithPersonalisationSchema(NotificationWithTemplateSchema):
@post_dump
def handle_template_merge(self, in_data):
in_data['template'] = in_data.pop('template_history')
from notifications_utils.template import Template
template = Template(
in_data['template'],
in_data['personalisation'],
renderer=PassThrough()
)
in_data['body'] = template.rendered
template_type = in_data['template']['template_type']
if template_type == 'email':
in_data['subject'] = str(Field(template.subject, in_data['personalisation']))
template = get_template_instance(in_data['template'], in_data['personalisation'])
in_data['body'] = str(template)
if in_data['template']['template_type'] == models.EMAIL_TYPE:
in_data['subject'] = template.subject
in_data['content_char_count'] = None
else:
in_data['content_char_count'] = len(in_data['body'])
in_data['content_char_count'] = template.content_count
in_data.pop('personalisation', None)
in_data['template'].pop('content', None)

View File

@@ -13,9 +13,7 @@ from app.dao.templates_dao import (
dao_get_all_templates_for_service,
dao_get_template_versions
)
from notifications_utils.field import Field
from notifications_utils.template import Template
from notifications_utils.renderers import PassThrough
from notifications_utils.template import SMSMessageTemplate
from app.dao.services_dao import dao_fetch_service_by_id
from app.models import SMS_TYPE
from app.schemas import (template_schema, template_history_schema)
@@ -26,13 +24,16 @@ from app.errors import (
register_errors,
InvalidRequest
)
from app.utils import get_template_instance
register_errors(template)
def _content_count_greater_than_limit(content, template_type):
template = Template({'content': content, 'template_type': template_type})
return template_type == SMS_TYPE and template.content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT')
if template_type != SMS_TYPE:
return False
template = SMSMessageTemplate({'content': content, 'template_type': template_type})
return template.content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT')
@template.route('', methods=['POST'])
@@ -93,7 +94,8 @@ 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 = Template(data, values=request.args.to_dict(), renderer=PassThrough())
template_object = get_template_instance(data, values=request.args.to_dict())
if template_object.missing_data:
raise InvalidRequest(
@@ -109,10 +111,7 @@ def preview_template_by_id_and_service_id(service_id, template_id):
]}, status_code=400
)
data['subject'], data['content'] = (
str(Field(template_object.subject, template_object.values)),
template_object.rendered
)
data['subject'], data['content'] = template_object.subject, str(template_object)
return jsonify(data)

View File

@@ -1,4 +1,6 @@
from flask import url_for
from app.models import SMS_TYPE, EMAIL_TYPE
from notifications_utils.template import SMSMessageTemplate, PlainTextEmailTemplate
def pagination_links(pagination, endpoint, **kwargs):
@@ -18,3 +20,9 @@ def url_with_token(data, url, config):
token = generate_token(data, config['SECRET_KEY'], config['DANGEROUS_SALT'])
base_url = config['ADMIN_BASE_URL'] + url
return base_url + token
def get_template_instance(template, values):
return {
SMS_TYPE: SMSMessageTemplate, EMAIL_TYPE: PlainTextEmailTemplate
}[template['template_type']](template, values)

View File

@@ -42,7 +42,7 @@ def post_sms_notification():
send_notification_to_queue(notification, service.research_mode)
sms_sender = service.sms_sender if service.sms_sender else current_app.config.get('FROM_NUMBER')
resp = create_post_sms_response_from_notification(notification,
template_with_content.rendered,
str(template_with_content),
sms_sender,
request.url_root)
return jsonify(resp), 201
@@ -70,7 +70,7 @@ def post_email_notification():
send_notification_to_queue(notification, service.research_mode)
resp = create_post_email_response_from_notification(notification=notification,
content=template_with_content.rendered,
content=str(template_with_content),
subject=template_with_content.subject,
email_from=service.email_from,
url_root=request.url_root)
@@ -89,5 +89,6 @@ def __validate_template(form, service, notification_type):
check_template_is_for_notification_type(notification_type, template.template_type)
check_template_is_active(template)
template_with_content = create_content_for_notification(template, form.get('personalisation', {}))
check_sms_content_char_count(template_with_content.content_count)
if template.template_type == SMS_TYPE:
check_sms_content_char_count(template_with_content.content_count)
return template, template_with_content