2018-09-20 14:40:41 +00:00
|
|
|
import json
|
|
|
|
|
2018-08-03 16:15:08 +00:00
|
|
|
import click
|
2019-02-11 20:34:45 +00:00
|
|
|
from boltons.typeutils import classproperty
|
2018-09-07 10:38:02 +00:00
|
|
|
from teal.resource import Converters, Resource
|
2018-08-03 16:15:08 +00:00
|
|
|
|
|
|
|
from ereuse_devicehub.db import db
|
|
|
|
from ereuse_devicehub.resources.agent import models, schemas
|
|
|
|
|
|
|
|
|
|
|
|
class AgentDef(Resource):
|
|
|
|
SCHEMA = schemas.Agent
|
|
|
|
VIEW = None
|
|
|
|
AUTH = True
|
|
|
|
ID_CONVERTER = Converters.uuid
|
|
|
|
|
|
|
|
|
|
|
|
class OrganizationDef(AgentDef):
|
|
|
|
SCHEMA = schemas.Organization
|
|
|
|
VIEW = None
|
|
|
|
|
2018-10-05 12:35:51 +00:00
|
|
|
def __init__(self, app, import_name=__name__.split('.')[0], static_folder=None,
|
|
|
|
static_url_path=None,
|
2018-08-03 16:15:08 +00:00
|
|
|
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_org, 'add'),)
|
2018-08-03 16:15:08 +00:00
|
|
|
super().__init__(app, import_name, static_folder, static_url_path, template_folder,
|
|
|
|
url_prefix, subdomain, url_defaults, root_path, cli_commands)
|
|
|
|
|
|
|
|
@click.argument('name')
|
2018-10-05 12:35:51 +00:00
|
|
|
@click.option('--tax_id', '-t')
|
|
|
|
@click.option('--country', '-c')
|
2018-08-03 16:15:08 +00:00
|
|
|
def create_org(self, name: str, tax_id: str = None, country: str = None) -> dict:
|
|
|
|
"""Creates an organization."""
|
|
|
|
org = models.Organization(**self.schema.load(
|
|
|
|
{
|
|
|
|
'name': name,
|
|
|
|
'taxId': tax_id,
|
|
|
|
'country': country
|
|
|
|
}
|
|
|
|
))
|
|
|
|
db.session.add(org)
|
|
|
|
db.session.commit()
|
2018-09-20 14:40:41 +00:00
|
|
|
o = self.schema.dump(org)
|
|
|
|
print(json.dumps(o, indent=2))
|
|
|
|
return o
|
2018-08-03 16:15:08 +00:00
|
|
|
|
2019-02-11 20:34:45 +00:00
|
|
|
@classproperty
|
|
|
|
def cli_name(cls):
|
|
|
|
return 'org'
|
|
|
|
|
2018-08-03 16:15:08 +00:00
|
|
|
|
|
|
|
class Membership(Resource):
|
|
|
|
SCHEMA = schemas.Membership
|
|
|
|
VIEW = None
|
|
|
|
ID_CONVERTER = Converters.string
|
|
|
|
|
|
|
|
|
|
|
|
class IndividualDef(AgentDef):
|
|
|
|
SCHEMA = schemas.Individual
|
|
|
|
VIEW = None
|
|
|
|
|
|
|
|
|
|
|
|
class PersonDef(IndividualDef):
|
|
|
|
SCHEMA = schemas.Person
|
|
|
|
VIEW = None
|
|
|
|
|
|
|
|
|
|
|
|
class SystemDef(IndividualDef):
|
|
|
|
SCHEMA = schemas.System
|
|
|
|
VIEW = None
|