This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/resources/user/__init__.py

66 lines
2.9 KiB
Python
Raw Permalink Normal View History

2019-01-23 15:55:04 +00:00
from typing import Iterable
2018-04-27 17:16:43 +00:00
from click import argument, option
from flask import current_app
2018-09-07 10:38:02 +00:00
from teal.resource import Converters, Resource
2018-04-27 17:16:43 +00:00
from ereuse_devicehub.db import db
from ereuse_devicehub.resources.user import schemas
from ereuse_devicehub.resources.user.models import User
2021-03-02 20:27:14 +00:00
from ereuse_devicehub.resources.user.views import UserView, login, logout
2018-04-27 17:16:43 +00:00
class UserDef(Resource):
SCHEMA = schemas.User
2018-04-27 17:16:43 +00:00
VIEW = UserView
2018-06-15 13:31:03 +00:00
ID_CONVERTER = Converters.uuid
2018-04-27 17:16:43 +00:00
AUTH = True
def __init__(self, app, import_name=__name__.split('.')[0], static_folder=None,
2018-04-27 17:16:43 +00:00
static_url_path=None, template_folder=None, url_prefix=None, subdomain=None,
url_defaults=None, root_path=None):
cli_commands = ((self.create_user, 'add'),)
2018-04-27 17:16:43 +00:00
super().__init__(app, import_name, static_folder, static_url_path, template_folder,
url_prefix, subdomain, url_defaults, root_path, cli_commands)
2019-01-16 19:40:27 +00:00
self.add_url_rule('/login/', view_func=login, methods={'POST'})
2021-04-12 10:10:57 +00:00
logout_view = app.auth.requires_auth(logout)
self.add_url_rule('/logout/', view_func=logout_view, methods={'GET'})
2018-04-27 17:16:43 +00:00
@argument('email')
2019-01-23 15:55:04 +00:00
@option('-i', '--inventory',
multiple=True,
help='Inventories user has access to. By default this one.')
@option('-a', '--agent',
help='Create too an Individual agent representing this user, '
'and give a name to this individual.')
@option('-c', '--country', help='The country of the agent (if --agent is set).')
@option('-t', '--telephone', help='The telephone of the agent (if --agent is set).')
@option('-t', '--tax-id', help='The tax id of the agent (if --agent is set).')
@option('-p', '--password', prompt=True, hide_input=True, confirmation_prompt=True)
2019-01-23 15:55:04 +00:00
def create_user(self, email: str,
password: str,
inventory: Iterable[str] = tuple(),
agent: str = None,
country: str = None,
telephone: str = None,
tax_id: str = None) -> dict:
"""Create an user.
If ``--agent`` is passed, it creates too an ``Individual``
agent that represents the user.
2018-04-27 17:16:43 +00:00
"""
from ereuse_devicehub.resources.agent.models import Individual
u = self.SCHEMA(only={'email', 'password'}, exclude=('token',)) \
.load({'email': email, 'password': password})
2019-01-23 15:55:04 +00:00
if inventory:
from ereuse_devicehub.resources.inventory import Inventory
2019-01-23 15:55:04 +00:00
inventory = Inventory.query.filter(Inventory.id.in_(inventory))
user = User(**u, inventories=inventory)
agent = Individual(**current_app.resources[Individual.t].schema.load(
dict(name=agent, email=email, country=country, telephone=telephone, taxId=tax_id)
))
user.individuals.add(agent)
db.session.add(user)
db.session.commit()
return self.schema.dump(user)