From 5ae53b625baddc65c0ef985683042c750c334765 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 27 Jan 2021 11:24:22 +0000 Subject: [PATCH] Show broadcasts created by the API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Broadcasts created by the API are different in that: - they aren’t created by any user, so don’t have a `created_by_id` - they are created instantly, not in steps, so don’t have an `updated_at` time This commit alters the views to account for when these pieces of information aren’t present. --- app/models/broadcast_message.py | 6 +- .../views/broadcast/macros/area-map.html | 4 +- .../views/broadcast/view-message.html | 26 +++++-- tests/app/main/views/test_broadcast.py | 74 +++++++++++++++++-- 4 files changed, 95 insertions(+), 15 deletions(-) diff --git a/app/models/broadcast_message.py b/app/models/broadcast_message.py index 8dbbc492f..fc31ec1bf 100644 --- a/app/models/broadcast_message.py +++ b/app/models/broadcast_message.py @@ -45,6 +45,10 @@ class BroadcastMessage(JSONModel): return True if not self.starts_at and other.starts_at: return False + if self.updated_at and not other.updated_at: + return self.updated_at < other.created_at + if not self.updated_at and other.updated_at: + return self.created_at < other.updated_at return self.updated_at < other.updated_at @classmethod @@ -129,7 +133,7 @@ class BroadcastMessage(JSONModel): @cached_property def created_by(self): - return User.from_id(self.created_by_id) + return User.from_id(self.created_by_id) if self.created_by_id else None @cached_property def approved_by(self): diff --git a/app/templates/views/broadcast/macros/area-map.html b/app/templates/views/broadcast/macros/area-map.html index 196f4c447..737cd24c0 100644 --- a/app/templates/views/broadcast/macros/area-map.html +++ b/app/templates/views/broadcast/macros/area-map.html @@ -20,7 +20,9 @@ alert
  • - {% if broadcast_message.count_of_phones == broadcast_message.count_of_phones_likely %} + {% if broadcast_message.count_of_phones == 0 %} + Unknown number of phones + {% elif broadcast_message.count_of_phones == broadcast_message.count_of_phones_likely %} {{ broadcast_message.count_of_phones|format_thousands }} phones estimated {% else %} {{ broadcast_message.count_of_phones|format_thousands }} to {{ broadcast_message.count_of_phones_likely|format_thousands }} phones diff --git a/app/templates/views/broadcast/view-message.html b/app/templates/views/broadcast/view-message.html index 2004729d0..08be7e083 100644 --- a/app/templates/views/broadcast/view-message.html +++ b/app/templates/views/broadcast/view-message.html @@ -19,10 +19,15 @@ {% block service_page_title %} {% if broadcast_message.status == 'pending-approval' %} - {% if broadcast_message.created_by == current_user and current_user.has_permissions('send_messages') %} + {% if broadcast_message.created_by and broadcast_message.created_by == current_user and current_user.has_permissions('send_messages') %} {{ broadcast_message.template.name }} is waiting for approval {% elif current_user.has_permissions('send_messages') %} - {{ broadcast_message.created_by.name }} wants to broadcast + {% if broadcast_message.created_by %} + {{ broadcast_message.created_by.name }} + {% else %} + An API call + {% endif %} + wants to broadcast {{ broadcast_message.template.name }} {% else %} This alert is waiting for approval @@ -37,7 +42,7 @@ {{ govukBackLink({ "href": back_link }) }} {% if broadcast_message.status == 'pending-approval' %} - {% if broadcast_message.created_by == current_user and current_user.has_permissions('send_messages') %} + {% if broadcast_message.created_by and broadcast_message.created_by == current_user and current_user.has_permissions('send_messages') %}