Munge stuff into a consistent event data type

We store our audit history in two ways:

  1. A list of versions of a service
  2. A list of events to do with API keys

In the future there could be auditing data which we want to display that
is stored in other formats (for example the event table).

This commit adds some objects which wrap around the different types of
auditing data, and expose a consistent interface to them. This
architecture will let us:
- write clean code in the presentation layer to display these events on
  a page
- add more types of events in the future by subclassing the `Event` data
  type, without having to rewrite anything in the presentation layer
This commit is contained in:
Chris Hill-Scott
2019-10-18 16:09:39 +01:00
parent 055f000020
commit 59b4d60c91
11 changed files with 367 additions and 121 deletions

View File

@@ -63,8 +63,8 @@ class ModelList(ABC, Sequence):
def model():
pass
def __init__(self):
self.items = self.client()
def __init__(self, *args):
self.items = self.client(*args)
def __getitem__(self, index):
return self.model(self.items[index])

191
app/models/event.py Normal file
View File

@@ -0,0 +1,191 @@
from abc import ABC, abstractmethod
from notifications_utils.formatters import formatted_list
from app.models import ModelList
from app.notify_client.service_api_client import service_api_client
from app.utils import format_thousands
class Event(ABC):
def __init__(
self,
item,
key=None,
value_from=None,
value_to=None,
):
self.item = item
self.time = item['updated_at'] or item['created_at']
self.user_id = item['created_by_id']
self.key = key
self.value_from = value_from
self.value_to = value_to
@abstractmethod
def __str__(self):
pass
@property
@abstractmethod
def relevant(self):
pass
class ServiceCreationEvent(Event):
relevant = True
def __str__(self):
return 'Created this service and called it {}'.format(
self.item['name']
)
class ServiceEvent(Event):
@property
def relevant(self):
return self.value_from != self.value_to and bool(self._formatter)
def __str__(self):
return self._formatter()
@property
def _formatter(self):
return getattr(self, 'format_{}'.format(self.key), None)
def format_restricted(self):
if self.value_to is False:
return 'Made this service live'
if self.value_to is True:
return 'Put this service back into trial mode'
def format_active(self):
if self.value_to is False:
return 'Deleted this service'
if self.value_to is True:
return 'Unsuspended this service'
def format_contact_link(self):
return 'Set the contact details for this service to {}'.format(
self.value_to
)
def format_email_branding(self):
return 'Updated this services email branding'
def format_inbound_api(self):
return 'Updated the callback for received text messages'
def format_letter_branding(self):
if self.value_to is None:
return 'Removed the logo from this services letters'
return 'Updated the logo on this services letters'
def format_letter_contact_block(self):
return 'Updated the default letter contact block for this service'
def format_message_limit(self):
return (
'{} this services daily message limit from {} to {}'
).format(
'Reduced' if self.value_from > self.value_to else 'Increased',
format_thousands(self.value_from),
format_thousands(self.value_to),
)
def format_name(self):
return (
'Renamed this service from {} to {}'
).format(
self.value_from, self.value_to
)
def format_permissions(self):
added = list(sorted(set(self.value_to) - set(self.value_from)))
removed = list(sorted(set(self.value_from) - set(self.value_to)))
if removed and added:
return 'Removed {} from this services permissions, added {}'.format(
formatted_list(removed),
formatted_list(added),
)
if added:
return 'Added {} to this services permissions'.format(
formatted_list(added)
)
if removed:
return 'Removed {} from this services permissions'.format(
formatted_list(removed)
)
def format_prefix_sms(self):
if self.value_to is True:
return 'Set text messages to start with the name of this service'
else:
return 'Set text messages to not start with the name of this service'
def format_research_mode(self):
if self.value_to is True:
return 'Put this service into research mode'
else:
return 'Took this service out of research mode'
def format_service_callback_api(self):
return 'Updated the callback for delivery receipts'
def format_go_live_user(self):
return 'Requested for this service to go live'
class APIKeyEvent(Event):
relevant = True
def __str__(self):
if self.item['updated_at']:
return (
'Revoked the {} API key'
).format(self.item['name'])
else:
return (
'Created an API key called {}'
).format(self.item['name'])
class APIKeyEvents(ModelList):
model = APIKeyEvent
client = service_api_client.get_service_api_key_history
class ServiceEvents(ModelList):
client = service_api_client.get_service_service_history
@property
def model(self):
return lambda x: x
@staticmethod
def splat(events):
for index, item in enumerate(sorted(
events,
key=lambda event: event['updated_at'] or event['created_at']
)):
if index == 0:
yield ServiceCreationEvent(item)
else:
for key in sorted(item.keys()):
yield ServiceEvent(
item,
key,
events[index - 1][key],
events[index][key],
)
def __init__(self, service_id):
self.items = [
event for event in self.splat(self.client(service_id)) if event.relevant
]

View File

@@ -1,3 +1,5 @@
from operator import attrgetter
from flask import Markup, abort, current_app
from notifications_utils.field import Field
from notifications_utils.formatters import nl2br
@@ -5,6 +7,7 @@ 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
@@ -632,6 +635,9 @@ class Service(JSONModel):
if test:
yield BASE + '_incomplete' + tag
@cached_property
@property
def history(self):
return service_api_client.get_service_history(self.id)['data']
return sorted(
ServiceEvents(self.id) + APIKeyEvents(self.id),
key=attrgetter('time'),
)