Refactor content preview method into generator

This will let us break up this method a bit more, rather than make the
dictionary comprehension even more involved and nested.

Means we need to `list()` it, because generator expressions cast to
boolean are `True`, even if they’re empty – Python doesn’t evaluate
them. ie:

```python
bool(list())
>>> False
```

```python
bool((item for item in list()))
>>> True
```

```python
bool(list(item for item in list()))
>>> False
```
This commit is contained in:
Chris Hill-Scott
2017-06-24 17:20:00 +01:00
parent 4798258e62
commit 1d65c19c4e

View File

@@ -261,7 +261,9 @@ def get_notifications(service_id, message_type, status_override=None):
),
'notifications': render_template(
'views/activity/notifications.html',
notifications=add_preview_of_content_to_notifications(notifications['notifications']),
notifications=list(add_preview_of_content_to_notifications(
notifications['notifications']
)),
page=page,
prev_page=prev_page,
next_page=next_page,
@@ -388,8 +390,8 @@ def get_job_partials(job):
def add_preview_of_content_to_notifications(notifications):
return [
dict(
for notification in notifications:
yield dict(
preview_of_content=(
str(Template(notification['template'], notification['personalisation']))
if notification['template']['template_type'] == 'sms' else
@@ -397,5 +399,3 @@ def add_preview_of_content_to_notifications(notifications):
),
**notification
)
for notification in notifications
]