From d93ebd99d3a69a9242f747948c8ddb1ab248f1f3 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Mon, 21 Oct 2019 12:14:41 +0100 Subject: [PATCH] Refactor history off the service model Directly referencing the `ModelList` instances will let us more easily make choices at the view layer about which kinds of events to show, and is one less layer of indirection to jump through. --- app/main/views/history.py | 9 ++++++++- app/models/service.py | 10 ---------- tests/app/main/views/test_history.py | 4 ++-- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/app/main/views/history.py b/app/main/views/history.py index 222a44490..00669219e 100644 --- a/app/main/views/history.py +++ b/app/main/views/history.py @@ -4,6 +4,7 @@ from flask import render_template from app import current_service, format_date_numeric from app.main import main +from app.models.event import APIKeyEvents, ServiceEvents from app.utils import user_has_permissions @@ -12,10 +13,16 @@ from app.utils import user_has_permissions def history(service_id): return render_template( 'views/temp-history.html', - days=_chunk_events_by_day(current_service.history) + days=_chunk_events_by_day( + _get_events(current_service.id) + ) ) +def _get_events(service_id): + return APIKeyEvents(service_id) + ServiceEvents(service_id) + + def _chunk_events_by_day(events): days = defaultdict(list) diff --git a/app/models/service.py b/app/models/service.py index 341ecf6a7..3e3e93fd6 100644 --- a/app/models/service.py +++ b/app/models/service.py @@ -1,5 +1,3 @@ -from operator import attrgetter - from flask import Markup, abort, current_app from notifications_utils.field import Field from notifications_utils.formatters import nl2br @@ -7,7 +5,6 @@ from notifications_utils.take import Take from werkzeug.utils import cached_property from app.models import JSONModel -from app.models.event import APIKeyEvents, ServiceEvents from app.models.organisation import Organisation from app.models.user import InvitedUsers, User, Users from app.notify_client.api_key_api_client import api_key_api_client @@ -634,10 +631,3 @@ class Service(JSONModel): ): if test: yield BASE + '_incomplete' + tag - - @property - def history(self): - return sorted( - ServiceEvents(self.id) + APIKeyEvents(self.id), - key=attrgetter('time'), - ) diff --git a/tests/app/main/views/test_history.py b/tests/app/main/views/test_history.py index 6a41898b8..92e1985de 100644 --- a/tests/app/main/views/test_history.py +++ b/tests/app/main/views/test_history.py @@ -44,9 +44,9 @@ def test_history( '10 October', ( '6ce466d0-fd6a-11e5-82f5-e0accb9d11a6 11:10am ' - 'Created an API key called ‘Good key’ ' + 'Created this service and called it ‘Example service’ ' '6ce466d0-fd6a-11e5-82f5-e0accb9d11a6 11:10am ' - 'Created this service and called it ‘Example service’' + 'Created an API key called ‘Good key’' ), ), ]