mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-25 12:51:05 -05:00
Our other models inherit from `JSONModel`, rather than manually doing lookups of the JSON in the `__init__` method. This commit changes the user model to work in the same way. I had to add a new concept (`DEFAULTS`) to account for some properties not always being present in the (mocked) JSON. In reality it might be that the API does always return these values. This should be looked at in future work, to see which defaults can be safely removed. At least now they: - do not mean any changes are needed to the existing tests - are explicitly separated from the attributes that we do expect to always be in the JSON response
35 lines
902 B
Python
35 lines
902 B
Python
from flask import abort
|
|
|
|
|
|
class JSONModel():
|
|
|
|
ALLOWED_PROPERTIES = set()
|
|
DEFAULTS = dict()
|
|
|
|
def __init__(self, _dict):
|
|
# in the case of a bad request _dict may be `None`
|
|
self._dict = _dict or {}
|
|
for attribute, default_value in self.DEFAULTS.items():
|
|
self._dict[attribute] = self._dict.get(attribute, default_value)
|
|
|
|
def __bool__(self):
|
|
return self._dict != {}
|
|
|
|
def __getattr__(self, attr):
|
|
if attr in self.ALLOWED_PROPERTIES:
|
|
return self._dict[attr]
|
|
raise AttributeError('`{}` is not a {} attribute'.format(
|
|
attr,
|
|
self.__class__.__name__.lower(),
|
|
))
|
|
|
|
def _get_by_id(self, things, id):
|
|
try:
|
|
return next(thing for thing in things if thing['id'] == str(id))
|
|
except StopIteration:
|
|
abort(404)
|
|
|
|
|
|
class InviteTokenError(Exception):
|
|
pass
|