From 1d65c19c4e4fe583dcf20ed55e7420a7203262d7 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Sat, 24 Jun 2017 17:20:00 +0100 Subject: [PATCH] Refactor content preview method into generator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 ``` --- app/main/views/jobs.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/main/views/jobs.py b/app/main/views/jobs.py index 1f459c886..d852bbb2a 100644 --- a/app/main/views/jobs.py +++ b/app/main/views/jobs.py @@ -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 - ]