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

49 lines
2.2 KiB
Python
Raw Normal View History

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
2018-04-27 17:16:43 +00:00
from ereuse_devicehub.resources.user.views import UserView, login
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, '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')
@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
"""
from ereuse_devicehub.resources.agent.models import Individual
u = self.SCHEMA(only={'email', 'password'}, exclude=('token',)) \
.load({'email': email, 'password': password})
user = User(**u)
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)