From e2e04b51fc2c2df62cf4527ea26ad25a7b7dd397 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Fri, 16 Oct 2020 09:02:35 +0100 Subject: [PATCH] Sort broadcasts by start time MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For emails and text messages we sort by the time the user (or API) sent them. This makes sense for broadcasts too, since most users will receive the alert within seconds of it being broadcast. For alerts that haven’t started yet we can sort by `updated_at`, which is when the user preparing the broadcast submitted it for approval. --- app/models/broadcast_message.py | 12 +++++---- tests/__init__.py | 6 +++-- tests/app/main/views/test_broadcast.py | 6 +++-- tests/conftest.py | 35 +++++++++++++++++++++++++- 4 files changed, 49 insertions(+), 10 deletions(-) diff --git a/app/models/broadcast_message.py b/app/models/broadcast_message.py index 372aec925..bffa1dcb4 100644 --- a/app/models/broadcast_message.py +++ b/app/models/broadcast_message.py @@ -42,11 +42,13 @@ class BroadcastMessage(JSONModel): libraries = broadcast_area_libraries def __lt__(self, other): - return ( - self.cancelled_at or self.finishes_at or self.created_at - ) < ( - other.cancelled_at or other.finishes_at or self.created_at - ) + if self.starts_at and other.starts_at: + return self.starts_at < other.starts_at + if self.starts_at and not other.starts_at: + return True + if not self.starts_at and other.starts_at: + return False + return self.updated_at < other.updated_at @classmethod def create(cls, *, service_id, template_id): diff --git a/tests/__init__.py b/tests/__init__.py index 8b0ee34c9..144b545f1 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -649,10 +649,12 @@ def broadcast_message_json( starts_at=None, finishes_at=None, cancelled_at=None, + updated_at=None, approved_by_id=None, cancelled_by_id=None, areas=None, content=None, + template_name='Example template', ): return { 'id': id_, @@ -661,7 +663,7 @@ def broadcast_message_json( 'template_id': template_id, 'template_version': 123, - 'template_name': 'Example template', + 'template_name': template_name, 'content': content or 'This is a test', 'personalisation': {}, @@ -677,7 +679,7 @@ def broadcast_message_json( 'created_at': None, 'approved_at': None, 'cancelled_at': cancelled_at, - 'updated_at': None, + 'updated_at': updated_at, 'created_by_id': created_by_id, 'approved_by_id': approved_by_id, diff --git a/tests/app/main/views/test_broadcast.py b/tests/app/main/views/test_broadcast.py index 3cc0ac221..899fe52ff 100644 --- a/tests/app/main/views/test_broadcast.py +++ b/tests/app/main/views/test_broadcast.py @@ -305,8 +305,10 @@ def test_broadcast_dashboard( assert [ normalize_spaces(row.text) for row in page.select('table')[0].select('tbody tr') ] == [ + 'Half an hour ago This is a test England Scotland Waiting for approval', + 'Hour and a half ago This is a test England Scotland Waiting for approval', 'Example template This is a test England Scotland Live since today at 2:20am', - 'Example template This is a test England Scotland Waiting for approval', + 'Example template This is a test England Scotland Live since today at 1:20am', ] @@ -353,8 +355,8 @@ def test_previous_broadcasts_page( assert [ normalize_spaces(row.text) for row in page.select('table')[0].select('tbody tr') ] == [ - 'Example template This is a test England Scotland Broadcast yesterday at 2:20am', 'Example template This is a test England Scotland Broadcast yesterday at 2:20pm', + 'Example template This is a test England Scotland Broadcast yesterday at 2:20am', ] diff --git a/tests/conftest.py b/tests/conftest.py index 56cd6fb33..ad56a80bb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4309,21 +4309,39 @@ def mock_get_broadcast_messages( def _get(service_id): partial_json = partial( broadcast_message_json, - id_=fake_uuid, service_id=service_id, template_id=fake_uuid, created_by_id=fake_uuid, ) return [ partial_json( + id_=uuid4(), status='draft', ), partial_json( + id_=uuid4(), status='pending-approval', + updated_at=( + datetime.utcnow() - timedelta(minutes=30) + ).isoformat(), + template_name='Half an hour ago', finishes_at=None, ), partial_json( + id_=uuid4(), + status='pending-approval', + updated_at=( + datetime.utcnow() - timedelta(hours=1, minutes=30) + ).isoformat(), + template_name='Hour and a half ago', + finishes_at=None, + ), + partial_json( + id_=uuid4(), status='broadcasting', + updated_at=( + datetime.utcnow() + ).isoformat(), starts_at=( datetime.utcnow() ).isoformat(), @@ -4332,6 +4350,20 @@ def mock_get_broadcast_messages( ).isoformat(), ), partial_json( + id_=uuid4(), + status='broadcasting', + updated_at=( + datetime.utcnow() - timedelta(hours=1) + ).isoformat(), + starts_at=( + datetime.utcnow() - timedelta(hours=1) + ).isoformat(), + finishes_at=( + datetime.utcnow() + timedelta(hours=24) + ).isoformat(), + ), + partial_json( + id_=uuid4(), status='completed', starts_at=( datetime.utcnow() - timedelta(hours=12) @@ -4341,6 +4373,7 @@ def mock_get_broadcast_messages( ).isoformat(), ), partial_json( + id_=uuid4(), status='cancelled', starts_at=( datetime.utcnow() - timedelta(days=1)