From 236ddf053db35322f3bd3a2616e26313fdbe9dbd Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 12 May 2020 17:08:13 +0100 Subject: [PATCH] Make created_at a property of contact lists MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The view layer shouldn’t be having to deal with converting dates as strings. This is an artefact of how we send data from the API to the admin app. The model layer should be responsible for turning JSON into richer types, where it can. --- app/models/contact_list.py | 6 +++++- tests/app/models/test_contact_list.py | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 tests/app/models/test_contact_list.py diff --git a/app/models/contact_list.py b/app/models/contact_list.py index 9d4dfaaa7..355991c61 100644 --- a/app/models/contact_list.py +++ b/app/models/contact_list.py @@ -4,6 +4,7 @@ from os import path from flask import abort, current_app from notifications_utils.formatters import strip_whitespace from notifications_utils.recipients import RecipientCSV +from notifications_utils.timezones import utc_string_to_aware_gmt_datetime from werkzeug.utils import cached_property from app.models import JSONModel, ModelList @@ -21,7 +22,6 @@ class ContactList(JSONModel): ALLOWED_PROPERTIES = { 'id', - 'created_at', 'created_by', 'service_id', 'original_file_name', @@ -112,6 +112,10 @@ class ContactList(JSONModel): contact_list_id=self.id, ) + @property + def created_at(self): + return utc_string_to_aware_gmt_datetime(self._dict['created_at']) + @property def contents(self): return self.download(self.service_id, self.id) diff --git a/tests/app/models/test_contact_list.py b/tests/app/models/test_contact_list.py new file mode 100644 index 000000000..fb1f01c11 --- /dev/null +++ b/tests/app/models/test_contact_list.py @@ -0,0 +1,9 @@ +from datetime import datetime + +from app.models.contact_list import ContactList + + +def test_created_at(): + created_at = ContactList({'created_at': '2016-05-06T07:08:09.061258'}).created_at + assert isinstance(created_at, datetime) + assert created_at.isoformat() == '2016-05-06T08:08:09.061258+01:00'