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
|
2018-04-27 17:16:43 +00:00
|
|
|
from ereuse_devicehub.resources.user.views import UserView, login
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
2018-05-30 10:49:40 +00:00
|
|
|
cli_commands = ((self.create_user, 'create-user'),)
|
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'})
|
2018-04-27 17:16:43 +00:00
|
|
|
|
|
|
|
@argument('email')
|
2018-09-20 14:40:41 +00:00
|
|
|
@option('-a', '--agent', help='The name of an agent to create with the user.')
|
|
|
|
@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)
|
|
|
|
def create_user(self, email: str, password: str, agent: str = None, country: str = None,
|
|
|
|
telephone: str = None, tax_id: str = None) -> dict:
|
|
|
|
"""Creates an user.
|
|
|
|
|
|
|
|
If ``--agent`` is passed, it creates 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})
|
|
|
|
user = User(**u)
|
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)
|