From 600e3affc12947d5a7b957b71e3d2864467d6bc2 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Mon, 21 Oct 2019 14:08:18 +0100 Subject: [PATCH] Show user names for events without API changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit introduces a slightly hacky way of putting usernames against events, given that the API only returns user IDs. It does so without: - making changes to the API - making a pages that could potentially fire off dozens of API calls (ie one per user) This comes with the limitation that it can only get names for those team members who are still in the team. Otherwise it will say ‘Unknown’. In the future the API should probably return the name and email address for the user who initiated the event, and whether that user was acting in a platform admin capacity. --- app/main/views/history.py | 3 ++- app/models/user.py | 6 ++++++ app/templates/views/temp-history.html | 2 +- tests/app/main/views/test_history.py | 20 ++++++++++---------- tests/conftest.py | 2 +- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/app/main/views/history.py b/app/main/views/history.py index 2512cfe72..07614db3a 100644 --- a/app/main/views/history.py +++ b/app/main/views/history.py @@ -19,7 +19,8 @@ def history(service_id): days=_chunk_events_by_day(events), show_navigation=request.args.get('selected') or any( isinstance(event, APIKeyEvent) for event in events - ) + ), + user_getter=current_service.active_users.get_name_from_id, ) diff --git a/app/models/user.py b/app/models/user.py index 8810719bc..2c9cf5fce 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -610,6 +610,12 @@ class Users(ModelList): def __init__(self, service_id): self.items = self.client(service_id) + def get_name_from_id(self, id): + for user in self: + if user.id == id: + return user.name + return 'Unknown' + class OrganisationUsers(Users): client = user_api_client.get_users_for_organisation diff --git a/app/templates/views/temp-history.html b/app/templates/views/temp-history.html index 2047e6236..0c1d79f76 100644 --- a/app/templates/views/temp-history.html +++ b/app/templates/views/temp-history.html @@ -35,7 +35,7 @@
- {{ event.user_id }} + {{ user_getter(event.user_id) }}
{{ event.time|format_time }} diff --git a/tests/app/main/views/test_history.py b/tests/app/main/views/test_history.py index 41f746ac4..09da2d0a9 100644 --- a/tests/app/main/views/test_history.py +++ b/tests/app/main/views/test_history.py @@ -8,30 +8,30 @@ from tests.conftest import SERVICE_ONE_ID, normalize_spaces ( '12 December', ( - '6ce466d0-fd6a-11e5-82f5-e0accb9d11a6 12:12pm ' + 'Test User 12:12pm ' 'Renamed this service from ‘Example service’ to ‘Real service’' ), ), ( '11 November', ( - '6ce466d0-fd6a-11e5-82f5-e0accb9d11a6 12:12pm ' + 'Test User 12:12pm ' 'Revoked the ‘Bad key’ API key' ), ), ( '11 November', ( - '6ce466d0-fd6a-11e5-82f5-e0accb9d11a6 11:11am ' + 'Test User 11:11am ' 'Created an API key called ‘Bad key’' ), ), ( '10 October', ( - '6ce466d0-fd6a-11e5-82f5-e0accb9d11a6 11:10am ' + 'Test User 11:10am ' 'Created an API key called ‘Good key’ ' - '6ce466d0-fd6a-11e5-82f5-e0accb9d11a6 2:01am ' + 'Unknown 2:01am ' 'Created this service and called it ‘Example service’' ), ), @@ -40,21 +40,21 @@ from tests.conftest import SERVICE_ONE_ID, normalize_spaces ( '11 November', ( - '6ce466d0-fd6a-11e5-82f5-e0accb9d11a6 12:12pm ' + 'Test User 12:12pm ' 'Revoked the ‘Bad key’ API key' ), ), ( '11 November', ( - '6ce466d0-fd6a-11e5-82f5-e0accb9d11a6 11:11am ' + 'Test User 11:11am ' 'Created an API key called ‘Bad key’' ), ), ( '10 October', ( - '6ce466d0-fd6a-11e5-82f5-e0accb9d11a6 11:10am ' + 'Test User 11:10am ' 'Created an API key called ‘Good key’' ), ), @@ -63,14 +63,14 @@ from tests.conftest import SERVICE_ONE_ID, normalize_spaces ( '12 December', ( - '6ce466d0-fd6a-11e5-82f5-e0accb9d11a6 12:12pm ' + 'Test User 12:12pm ' 'Renamed this service from ‘Example service’ to ‘Real service’' ), ), ( '10 October', ( - '6ce466d0-fd6a-11e5-82f5-e0accb9d11a6 2:01am ' + 'Unknown 2:01am ' 'Created this service and called it ‘Example service’' ), ), diff --git a/tests/conftest.py b/tests/conftest.py index 05792f2e7..271c6001f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3511,7 +3511,7 @@ def mock_get_service_history(mocker): 'name': 'Example service', 'created_at': '2010-10-10T01:01:01.000000Z', 'updated_at': None, - 'created_by_id': sample_uuid(), + 'created_by_id': uuid4(), }, { 'name': 'Real service',