From 7da0d46767d910df6921de0c08000c7bfc460ca6 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Fri, 2 Oct 2020 16:39:24 +0100 Subject: [PATCH 1/3] Serialise content in broadcast message response MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will let us show the content of the broadcast message in places we can’t at the moment. --- app/models.py | 7 +++++++ tests/app/broadcast_message/test_rest.py | 2 ++ 2 files changed, 9 insertions(+) diff --git a/app/models.py b/app/models.py index 4d98f6545..cb410b7c7 100644 --- a/app/models.py +++ b/app/models.py @@ -2242,6 +2242,12 @@ class BroadcastMessage(db.Model): approved_by = db.relationship('User', foreign_keys=[approved_by_id]) cancelled_by = db.relationship('User', foreign_keys=[cancelled_by_id]) + @property + def content(self): + return self.template._as_utils_template_with_personalisation( + self.personalisation + ).content_with_placeholders_filled_in + @property def personalisation(self): if self._personalisation: @@ -2261,6 +2267,7 @@ class BroadcastMessage(db.Model): 'template_id': str(self.template_id), 'template_version': self.template_version, 'template_name': self.template.name, + 'template_content': self.content, 'personalisation': self.personalisation, 'areas': self.areas.get("areas", []), diff --git a/tests/app/broadcast_message/test_rest.py b/tests/app/broadcast_message/test_rest.py index d38c9c353..94d3fcfab 100644 --- a/tests/app/broadcast_message/test_rest.py +++ b/tests/app/broadcast_message/test_rest.py @@ -24,6 +24,8 @@ def test_get_broadcast_message(admin_request, sample_broadcast_service): ) assert response['id'] == str(bm.id) + assert response['template_id'] == str(t.id) + assert response['template_content'] == t.content assert response['template_name'] == t.name assert response['status'] == BroadcastStatusType.DRAFT assert response['created_at'] is not None From 80352bafe16ed1004a5f2a64b343cedb0fe8d6c7 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 7 Oct 2020 10:52:31 +0100 Subject: [PATCH 2/3] Update test to check personalisation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While the API still has a column and field for personalisation I think it makes sense for it to consider the personalisation when serialising the broadcast, so we should have a test for this. This way the API still works as a coherent whole, and the admin app just happens to be a client of the API which doesn’t implement the personalisation feature. If we want to remove personalisation from the API at another time we should do it wholesale. --- tests/app/broadcast_message/test_rest.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/tests/app/broadcast_message/test_rest.py b/tests/app/broadcast_message/test_rest.py index 94d3fcfab..5a315f958 100644 --- a/tests/app/broadcast_message/test_rest.py +++ b/tests/app/broadcast_message/test_rest.py @@ -10,11 +10,21 @@ from tests.app.db import create_broadcast_message, create_template, create_servi def test_get_broadcast_message(admin_request, sample_broadcast_service): - t = create_template(sample_broadcast_service, BROADCAST_TYPE) - bm = create_broadcast_message(t, areas={ - "areas": ['place A', 'region B'], - "simple_polygons": [[[50.1, 1.2], [50.12, 1.2], [50.13, 1.2]]] - }) + t = create_template( + sample_broadcast_service, + BROADCAST_TYPE, + content='This is a ((thing))' + ) + bm = create_broadcast_message( + t, + areas={ + "areas": ['place A', 'region B'], + "simple_polygons": [[[50.1, 1.2], [50.12, 1.2], [50.13, 1.2]]], + }, + personalisation={ + 'thing': 'test', + }, + ) response = admin_request.get( 'broadcast_message.get_broadcast_message', @@ -25,13 +35,13 @@ def test_get_broadcast_message(admin_request, sample_broadcast_service): assert response['id'] == str(bm.id) assert response['template_id'] == str(t.id) - assert response['template_content'] == t.content + assert response['template_content'] == 'This is a test' assert response['template_name'] == t.name assert response['status'] == BroadcastStatusType.DRAFT assert response['created_at'] is not None assert response['starts_at'] is None assert response['areas'] == ['place A', 'region B'] - assert response['personalisation'] == {} + assert response['personalisation'] == {'thing': 'test'} def test_get_broadcast_message_404s_if_message_doesnt_exist(admin_request, sample_broadcast_service): From 57473ef65db52da39b56d8b95f9ffe3a3583bac8 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Thu, 8 Oct 2020 11:39:17 +0100 Subject: [PATCH 3/3] Rename template_content to content `template_content` is a template that hasn't had its placeholders filled in whereas content is where it would have (which is what has happened above) --- app/models.py | 2 +- tests/app/broadcast_message/test_rest.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models.py b/app/models.py index cb410b7c7..cbe9a4e14 100644 --- a/app/models.py +++ b/app/models.py @@ -2267,7 +2267,7 @@ class BroadcastMessage(db.Model): 'template_id': str(self.template_id), 'template_version': self.template_version, 'template_name': self.template.name, - 'template_content': self.content, + 'content': self.content, 'personalisation': self.personalisation, 'areas': self.areas.get("areas", []), diff --git a/tests/app/broadcast_message/test_rest.py b/tests/app/broadcast_message/test_rest.py index 5a315f958..5f2a0f6e2 100644 --- a/tests/app/broadcast_message/test_rest.py +++ b/tests/app/broadcast_message/test_rest.py @@ -35,7 +35,7 @@ def test_get_broadcast_message(admin_request, sample_broadcast_service): assert response['id'] == str(bm.id) assert response['template_id'] == str(t.id) - assert response['template_content'] == 'This is a test' + assert response['content'] == 'This is a test' assert response['template_name'] == t.name assert response['status'] == BroadcastStatusType.DRAFT assert response['created_at'] is not None