2018-05-11 16:58:48 +00:00
|
|
|
from marshmallow import post_dump
|
2023-03-21 11:08:13 +00:00
|
|
|
from marshmallow.fields import UUID, Email, String
|
2018-04-27 17:16:43 +00:00
|
|
|
|
2019-01-19 18:19:35 +00:00
|
|
|
from ereuse_devicehub import auth
|
2018-08-03 16:15:08 +00:00
|
|
|
from ereuse_devicehub.marshmallow import NestedOn
|
|
|
|
from ereuse_devicehub.resources.agent.schemas import Individual
|
2019-01-23 15:55:04 +00:00
|
|
|
from ereuse_devicehub.resources.inventory.schema import Inventory
|
2018-04-27 17:16:43 +00:00
|
|
|
from ereuse_devicehub.resources.schemas import Thing
|
2023-03-21 11:08:13 +00:00
|
|
|
from ereuse_devicehub.teal.marshmallow import SanitizedStr
|
2018-04-27 17:16:43 +00:00
|
|
|
|
|
|
|
|
2021-04-15 19:17:24 +00:00
|
|
|
class Session(Thing):
|
|
|
|
token = String(dump_only=True)
|
|
|
|
|
|
|
|
|
2018-04-27 17:16:43 +00:00
|
|
|
class User(Thing):
|
|
|
|
id = UUID(dump_only=True)
|
|
|
|
email = Email(required=True)
|
2018-09-30 10:29:33 +00:00
|
|
|
password = SanitizedStr(load_only=True, required=True)
|
2018-08-03 16:15:08 +00:00
|
|
|
individuals = NestedOn(Individual, many=True, dump_only=True)
|
2018-09-30 10:29:33 +00:00
|
|
|
name = SanitizedStr()
|
2023-03-21 11:08:13 +00:00
|
|
|
token = String(
|
|
|
|
dump_only=True,
|
|
|
|
description='Use this token in an Authorization header to access the app.'
|
|
|
|
'The token can change overtime.',
|
|
|
|
)
|
2019-01-23 15:55:04 +00:00
|
|
|
inventories = NestedOn(Inventory, many=True, dump_only=True)
|
2021-07-01 10:08:18 +00:00
|
|
|
code = String(dump_only=True, description='Code of inactive accounts')
|
2018-04-27 17:16:43 +00:00
|
|
|
|
2023-03-21 11:08:13 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
only=None,
|
|
|
|
exclude=('token',),
|
|
|
|
prefix='',
|
|
|
|
many=False,
|
|
|
|
context=None,
|
|
|
|
load_only=(),
|
|
|
|
dump_only=(),
|
|
|
|
partial=False,
|
|
|
|
):
|
2019-06-19 11:35:26 +00:00
|
|
|
"""Instantiates the User.
|
2018-05-11 16:58:48 +00:00
|
|
|
|
|
|
|
By default we exclude token from both load/dump
|
|
|
|
so they are not taken / set in normal usage by mistake.
|
|
|
|
"""
|
2023-03-21 11:08:13 +00:00
|
|
|
super().__init__(
|
|
|
|
only, exclude, prefix, many, context, load_only, dump_only, partial
|
|
|
|
)
|
2018-05-11 16:58:48 +00:00
|
|
|
|
|
|
|
@post_dump
|
2018-04-27 17:16:43 +00:00
|
|
|
def base64encode_token(self, data: dict):
|
|
|
|
"""Encodes the token to base64 so clients don't have to."""
|
2018-05-11 16:58:48 +00:00
|
|
|
if 'token' in data:
|
|
|
|
# In many cases we don't dump the token (ex. relationships)
|
|
|
|
# Framework needs ':' at the end
|
2019-01-19 18:19:35 +00:00
|
|
|
data['token'] = auth.Auth.encode(data['token'])
|
2018-04-27 17:16:43 +00:00
|
|
|
return data
|