mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
Return rendered HTML when previewing a template
If you’re trying to show what a Notify email will look like in your caseworking system all the API gives you at the moment is raw markdown (with the placeholders replaced). This isn’t that useful if your caseworkers have no idea what markdown is. If we also give teams the HTML then they can embed this in their systems, and the people using those systems will be able to see how headings, bulleted lists, etc. look.
This commit is contained in:
14
app/utils.py
14
app/utils.py
@@ -3,7 +3,7 @@ from datetime import datetime, timedelta
|
|||||||
import pytz
|
import pytz
|
||||||
from flask import url_for
|
from flask import url_for
|
||||||
from sqlalchemy import func
|
from sqlalchemy import func
|
||||||
from notifications_utils.template import SMSMessageTemplate, WithSubjectTemplate
|
from notifications_utils.template import SMSMessageTemplate, WithSubjectTemplate, get_html_email_body
|
||||||
from notifications_utils.timezones import convert_utc_to_bst
|
from notifications_utils.timezones import convert_utc_to_bst
|
||||||
|
|
||||||
local_timezone = pytz.timezone("Europe/London")
|
local_timezone = pytz.timezone("Europe/London")
|
||||||
@@ -35,6 +35,18 @@ def get_template_instance(template, values):
|
|||||||
}[template['template_type']](template, values)
|
}[template['template_type']](template, values)
|
||||||
|
|
||||||
|
|
||||||
|
def get_html_email_body_from_template(template_instance):
|
||||||
|
from app.models import EMAIL_TYPE
|
||||||
|
|
||||||
|
if template_instance.template_type != EMAIL_TYPE:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return get_html_email_body(
|
||||||
|
template_instance.content,
|
||||||
|
template_instance.values,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_london_midnight_in_utc(date):
|
def get_london_midnight_in_utc(date):
|
||||||
"""
|
"""
|
||||||
This function converts date to midnight as BST (British Standard Time) to UTC,
|
This function converts date to midnight as BST (British Standard Time) to UTC,
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from app.models import SMS_TYPE, TEMPLATE_TYPES
|
from app.models import SMS_TYPE, TEMPLATE_TYPES
|
||||||
from app.schema_validation.definitions import uuid, personalisation
|
from app.schema_validation.definitions import uuid, personalisation
|
||||||
|
from app.utils import get_html_email_body_from_template
|
||||||
|
|
||||||
|
|
||||||
get_template_by_id_request = {
|
get_template_by_id_request = {
|
||||||
@@ -79,6 +80,7 @@ def create_post_template_preview_response(template, template_object):
|
|||||||
"type": template.template_type,
|
"type": template.template_type,
|
||||||
"version": template.version,
|
"version": template.version,
|
||||||
"body": str(template_object),
|
"body": str(template_object),
|
||||||
|
"html": get_html_email_body_from_template(template_object),
|
||||||
"subject": subject,
|
"subject": subject,
|
||||||
"postage": template.postage
|
"postage": template.postage
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import pytest
|
|||||||
|
|
||||||
from flask import json
|
from flask import json
|
||||||
|
|
||||||
from app.models import SMS_TYPE, TEMPLATE_TYPES
|
from app.models import EMAIL_TYPE, SMS_TYPE, TEMPLATE_TYPES
|
||||||
from tests import create_authorization_header
|
from tests import create_authorization_header
|
||||||
from tests.app.db import create_template
|
from tests.app.db import create_template
|
||||||
|
|
||||||
@@ -16,36 +16,68 @@ valid_post = [
|
|||||||
"Some content",
|
"Some content",
|
||||||
None,
|
None,
|
||||||
"Some subject",
|
"Some subject",
|
||||||
"Some content"
|
"Some content",
|
||||||
|
(
|
||||||
|
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
|
||||||
|
'Some content'
|
||||||
|
'</p>'
|
||||||
|
),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"Some subject",
|
"Some subject",
|
||||||
"Dear ((Name)), Hello. Yours Truly, The Government.",
|
"Dear ((Name)), Hello. Yours Truly, The Government.",
|
||||||
valid_personalisation,
|
valid_personalisation,
|
||||||
"Some subject",
|
"Some subject",
|
||||||
"Dear Jo, Hello. Yours Truly, The Government."
|
"Dear Jo, Hello. Yours Truly, The Government.",
|
||||||
|
(
|
||||||
|
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
|
||||||
|
'Dear Jo, Hello. Yours Truly, The Government.'
|
||||||
|
'</p>'
|
||||||
|
),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"Message for ((Name))",
|
"Message for ((Name))",
|
||||||
"Dear ((Name)), Hello. Yours Truly, The Government.",
|
"Dear ((Name)), Hello. Yours Truly, The Government.",
|
||||||
valid_personalisation,
|
valid_personalisation,
|
||||||
"Message for Jo",
|
"Message for Jo",
|
||||||
"Dear Jo, Hello. Yours Truly, The Government."
|
"Dear Jo, Hello. Yours Truly, The Government.",
|
||||||
|
(
|
||||||
|
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
|
||||||
|
'Dear Jo, Hello. Yours Truly, The Government.'
|
||||||
|
'</p>'
|
||||||
|
),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"Message for ((Name))",
|
"Message for ((Name))",
|
||||||
"Some content",
|
"Some content",
|
||||||
valid_personalisation,
|
valid_personalisation,
|
||||||
"Message for Jo",
|
"Message for Jo",
|
||||||
"Some content"
|
"Some content",
|
||||||
|
(
|
||||||
|
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
|
||||||
|
'Some content'
|
||||||
|
'</p>'
|
||||||
|
),
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES)
|
@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES)
|
||||||
@pytest.mark.parametrize("subject,content,post_data,expected_subject,expected_content", valid_post)
|
@pytest.mark.parametrize(
|
||||||
|
"subject,content,post_data,expected_subject,expected_content,expected_html",
|
||||||
|
valid_post
|
||||||
|
)
|
||||||
def test_valid_post_template_returns_200(
|
def test_valid_post_template_returns_200(
|
||||||
client, sample_service, tmp_type, subject, content, post_data, expected_subject, expected_content):
|
client,
|
||||||
|
sample_service,
|
||||||
|
tmp_type,
|
||||||
|
subject,
|
||||||
|
content,
|
||||||
|
post_data,
|
||||||
|
expected_subject,
|
||||||
|
expected_content,
|
||||||
|
expected_html,
|
||||||
|
):
|
||||||
template = create_template(
|
template = create_template(
|
||||||
sample_service,
|
sample_service,
|
||||||
template_type=tmp_type,
|
template_type=tmp_type,
|
||||||
@@ -64,8 +96,15 @@ def test_valid_post_template_returns_200(
|
|||||||
resp_json = json.loads(response.get_data(as_text=True))
|
resp_json = json.loads(response.get_data(as_text=True))
|
||||||
|
|
||||||
assert resp_json['id'] == str(template.id)
|
assert resp_json['id'] == str(template.id)
|
||||||
|
|
||||||
if tmp_type != SMS_TYPE:
|
if tmp_type != SMS_TYPE:
|
||||||
assert expected_subject in resp_json['subject']
|
assert expected_subject in resp_json['subject']
|
||||||
|
|
||||||
|
if tmp_type == EMAIL_TYPE:
|
||||||
|
assert resp_json['html'] == expected_html
|
||||||
|
else:
|
||||||
|
assert resp_json['html'] is None
|
||||||
|
|
||||||
assert expected_content in resp_json['body']
|
assert expected_content in resp_json['body']
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user