Use utils template to format broadcast content

Brings in:
- [x] https://github.com/alphagov/notifications-utils/pull/801

Formats the content of the template at the time of creating the event.
This means that any downstream code (eg Celery tasks) can assume the
content is already formatted correctly.

Also, these downstream tasks don’t  know which template the broadcast
was created from, so if we support personalisation in the future this is
the most sensible place to bring together the template and the
personalisation.

---

I had to re-create some of the deleted code from utils for stuff like
formatting the timestamp to the CAP standard.
This commit is contained in:
Chris Hill-Scott
2020-10-26 17:39:11 +00:00
parent 55efbb0bb1
commit 28f01bd776
4 changed files with 37 additions and 11 deletions

View File

@@ -185,13 +185,16 @@ def _create_broadcast_event(broadcast_message):
else:
transmitted_finishes_at = broadcast_message.finishes_at
# TODO: This doesn't support placeholders yet. We shouldn't use BroadcastMessageTemplate when we add placeholders
# as that just outputs XML, we need the raw text.
template = broadcast_message.template._as_utils_template_with_personalisation(
# Broadcast events dont support personalisation yet
values={}
)
event = BroadcastEvent(
service=broadcast_message.service,
broadcast_message=broadcast_message,
message_type=msg_types[broadcast_message.status],
transmitted_content={"body": broadcast_message.template.content},
transmitted_content={"body": str(template)},
transmitted_areas=broadcast_message.areas,
# TODO: Probably move this somewhere more standalone too and imply that it shouldn't change. Should it include
# a service based identifier too? eg "flood-warnings@notifications.service.gov.uk" or similar

View File

@@ -2342,7 +2342,31 @@ class BroadcastEvent(db.Model):
@property
def reference(self):
return BroadcastMessageTemplate.from_event(self.serialize()).reference
notify_email_domain = current_app.config['NOTIFY_EMAIL_DOMAIN']
return (
f'https://www.{notify_email_domain}/,'
f'{self.id},'
f'{self.sent_at_as_cap_datetime_string}'
)
@property
def sent_at_as_cap_datetime_string(self):
return self.formatted_datetime_for('sent_at')
def formatted_datetime_for(self, property_name):
return self.convert_naive_utc_datetime_to_cap_standard_string(
getattr(self, property_name)
)
@staticmethod
def convert_naive_utc_datetime_to_cap_standard_string(dt):
"""
As defined in section 3.3.2 of
http://docs.oasis-open.org/emergency/cap/v1.2/CAP-v1.2-os.html
They define the standard "YYYY-MM-DDThh:mm:ssXzh:zm", where X is
`+` if the timezone is > UTC, otherwise `-`
"""
return f"{dt.strftime('%Y-%m-%dT%H:%M:%S')}-00:00"
def get_earlier_message_references(self):
from app.dao.broadcast_message_dao import get_earlier_events_for_broadcast_event

View File

@@ -29,7 +29,7 @@ notifications-python-client==5.7.0
# PaaS
awscli-cwlogs>=1.4,<1.5
git+https://github.com/alphagov/notifications-utils.git@42.2.1#egg=notifications-utils==42.2.1
git+https://github.com/alphagov/notifications-utils.git@43.0.0#egg=notifications-utils==43.0.0
# gds-metrics requires prometheseus 0.2.0, override that requirement as 0.7.1 brings significant performance gains
prometheus-client==0.8.0

View File

@@ -31,7 +31,7 @@ notifications-python-client==5.7.0
# PaaS
awscli-cwlogs>=1.4,<1.5
git+https://github.com/alphagov/notifications-utils.git@42.2.1#egg=notifications-utils==42.2.1
git+https://github.com/alphagov/notifications-utils.git@43.0.0#egg=notifications-utils==43.0.0
# gds-metrics requires prometheseus 0.2.0, override that requirement as 0.7.1 brings significant performance gains
prometheus-client==0.8.0
@@ -42,14 +42,14 @@ alembic==1.4.3
amqp==1.4.9
anyjson==0.3.3
attrs==20.2.0
awscli==1.18.148
awscli==1.18.164
bcrypt==3.2.0
billiard==3.3.0.23
bleach==3.1.4
blinker==1.4
boto==2.49.0
boto3==1.10.38
botocore==1.18.7
botocore==1.19.4
certifi==2020.6.20
chardet==3.0.4
click==7.1.2
@@ -66,7 +66,6 @@ importlib-metadata==2.0.0
Jinja2==2.11.2
jmespath==0.10.0
kombu==3.0.37
lxml==4.5.1
Mako==1.1.3
MarkupSafe==1.1.1
mistune==0.8.4
@@ -89,7 +88,7 @@ s3transfer==0.3.3
six==1.15.0
smartypants==2.0.1
statsd==3.3.0
urllib3==1.25.10
urllib3==1.25.11
webencodings==0.5.1
Werkzeug==1.0.1
zipp==3.2.0
zipp==3.4.0