2020-07-03 14:07:40 +01:00
|
|
|
from abc import abstractmethod
|
2019-06-12 13:40:57 +01:00
|
|
|
|
2019-04-04 11:18:22 +01:00
|
|
|
from flask import abort
|
2024-05-16 10:37:37 -04:00
|
|
|
|
2020-07-03 14:07:40 +01:00
|
|
|
from notifications_utils.serialised_model import (
|
|
|
|
|
SerialisedModel,
|
|
|
|
|
SerialisedModelCollection,
|
|
|
|
|
)
|
2019-04-04 11:18:22 +01:00
|
|
|
|
|
|
|
|
|
2020-07-03 14:07:40 +01:00
|
|
|
class JSONModel(SerialisedModel):
|
2019-04-04 11:18:22 +01:00
|
|
|
def __init__(self, _dict):
|
|
|
|
|
# in the case of a bad request _dict may be `None`
|
|
|
|
|
self._dict = _dict or {}
|
2020-10-19 15:36:50 +01:00
|
|
|
for property in self.ALLOWED_PROPERTIES:
|
2020-10-28 10:03:02 +00:00
|
|
|
if property in self._dict:
|
2020-10-19 15:36:50 +01:00
|
|
|
setattr(self, property, self._dict[property])
|
2019-04-04 11:18:22 +01:00
|
|
|
|
|
|
|
|
def __bool__(self):
|
|
|
|
|
return self._dict != {}
|
|
|
|
|
|
2019-06-12 12:09:26 +01:00
|
|
|
def __hash__(self):
|
|
|
|
|
return hash(self.id)
|
|
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
|
return self.id == other.id
|
|
|
|
|
|
2019-04-04 11:18:22 +01:00
|
|
|
def _get_by_id(self, things, id):
|
|
|
|
|
try:
|
2023-08-25 09:12:23 -07:00
|
|
|
return next(thing for thing in things if thing["id"] == str(id))
|
2019-04-04 11:18:22 +01:00
|
|
|
except StopIteration:
|
|
|
|
|
abort(404)
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
|
|
|
|
|
|
2020-07-03 14:07:40 +01:00
|
|
|
class ModelList(SerialisedModelCollection):
|
2019-06-12 13:40:57 +01:00
|
|
|
@property
|
|
|
|
|
@abstractmethod
|
2020-01-16 15:57:51 +00:00
|
|
|
def client_method(self):
|
2019-06-12 13:40:57 +01:00
|
|
|
pass
|
|
|
|
|
|
2019-10-18 16:09:39 +01:00
|
|
|
def __init__(self, *args):
|
2020-01-16 15:57:51 +00:00
|
|
|
self.items = self.client_method(*args)
|
2020-05-12 17:06:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class PaginatedModelList(ModelList):
|
2023-08-25 09:12:23 -07:00
|
|
|
response_key = "data"
|
2020-05-12 17:06:23 +01:00
|
|
|
|
|
|
|
|
def __init__(self, *args, page=None, **kwargs):
|
|
|
|
|
try:
|
|
|
|
|
self.current_page = int(page)
|
|
|
|
|
except TypeError:
|
|
|
|
|
self.current_page = 1
|
|
|
|
|
response = self.client_method(
|
|
|
|
|
*args,
|
|
|
|
|
**kwargs,
|
|
|
|
|
page=self.current_page,
|
|
|
|
|
)
|
|
|
|
|
self.items = response[self.response_key]
|
2023-08-25 09:12:23 -07:00
|
|
|
self.prev_page = response.get("links", {}).get("prev", None)
|
|
|
|
|
self.next_page = response.get("links", {}).get("next", None)
|
2021-11-11 14:35:55 +00:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
class SortByNameMixin:
|
2021-11-11 14:35:55 +00:00
|
|
|
def __lt__(self, other):
|
|
|
|
|
return self.name.lower() < other.name.lower()
|