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.
This commit is contained in:
Chris Hill-Scott
2019-10-21 12:14:41 +01:00
parent b2ebaf153a
commit d93ebd99d3
3 changed files with 10 additions and 13 deletions

View File

@@ -4,6 +4,7 @@ from flask import render_template
from app import current_service, format_date_numeric from app import current_service, format_date_numeric
from app.main import main from app.main import main
from app.models.event import APIKeyEvents, ServiceEvents
from app.utils import user_has_permissions from app.utils import user_has_permissions
@@ -12,10 +13,16 @@ from app.utils import user_has_permissions
def history(service_id): def history(service_id):
return render_template( return render_template(
'views/temp-history.html', '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): def _chunk_events_by_day(events):
days = defaultdict(list) days = defaultdict(list)

View File

@@ -1,5 +1,3 @@
from operator import attrgetter
from flask import Markup, abort, current_app from flask import Markup, abort, current_app
from notifications_utils.field import Field from notifications_utils.field import Field
from notifications_utils.formatters import nl2br from notifications_utils.formatters import nl2br
@@ -7,7 +5,6 @@ from notifications_utils.take import Take
from werkzeug.utils import cached_property from werkzeug.utils import cached_property
from app.models import JSONModel from app.models import JSONModel
from app.models.event import APIKeyEvents, ServiceEvents
from app.models.organisation import Organisation from app.models.organisation import Organisation
from app.models.user import InvitedUsers, User, Users from app.models.user import InvitedUsers, User, Users
from app.notify_client.api_key_api_client import api_key_api_client from app.notify_client.api_key_api_client import api_key_api_client
@@ -634,10 +631,3 @@ class Service(JSONModel):
): ):
if test: if test:
yield BASE + '_incomplete' + tag yield BASE + '_incomplete' + tag
@property
def history(self):
return sorted(
ServiceEvents(self.id) + APIKeyEvents(self.id),
key=attrgetter('time'),
)

View File

@@ -44,9 +44,9 @@ def test_history(
'10 October', '10 October',
( (
'6ce466d0-fd6a-11e5-82f5-e0accb9d11a6 11:10am ' '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 ' '6ce466d0-fd6a-11e5-82f5-e0accb9d11a6 11:10am '
'Created this service and called it Example service' 'Created an API key called Good key'
), ),
), ),
] ]