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
|
2018-09-20 14:40:41 +00:00
|
|
|
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
|
2018-07-22 20:42:49 +00:00
|
|
|
from ereuse_devicehub.resources.user import schemas
|
2018-08-03 16:15:08 +00:00
|
|
|
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):
|
2018-07-22 20:42:49 +00:00
|
|
|
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
|
|
|
|
|
2018-09-29 13:55:29 +00:00
|
|
|
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):
|
2019-02-11 20:34:45 +00:00
|
|
|
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-03-02 20:27:14 +00:00
|
|
|
logout1 = app.auth.requires_auth(logout)
|
|
|
|
self.add_url_rule('/logout/', view_func=logout1, 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.')
|
2019-02-11 20:34:45 +00:00
|
|
|
@option('-a', '--agent',
|
|
|
|
help='Create too an Individual agent representing this user, '
|
|
|
|
'and give a name to this individual.')
|
2018-09-20 14:40:41 +00:00
|
|
|
@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:
|
2019-02-11 20:34:45 +00:00
|
|
|
"""Create an user.
|
2018-09-20 14:40:41 +00:00
|
|
|
|
2019-02-11 20:34:45 +00:00
|
|
|
If ``--agent`` is passed, it creates too an ``Individual``
|
|
|
|
agent that represents the user.
|
2018-04-27 17:16:43 +00:00
|
|
|
"""
|
2018-09-20 14:40:41 +00:00
|
|
|
from ereuse_devicehub.resources.agent.models import Individual
|
2018-05-30 10:49:40 +00:00
|
|
|
u = self.SCHEMA(only={'email', 'password'}, exclude=('token',)) \
|
|
|
|
.load({'email': email, 'password': password})
|
2019-01-23 15:55:04 +00:00
|
|
|
if inventory:
|
2019-02-11 20:34:45 +00:00
|
|
|
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)
|
2018-09-20 14:40:41 +00:00
|
|
|
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)
|
2018-05-30 10:49:40 +00:00
|
|
|
db.session.add(user)
|
|
|
|
db.session.commit()
|
|
|
|
return self.schema.dump(user)
|