2018-04-27 17:16:43 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
|
2018-05-30 10:49:40 +00:00
|
|
|
from flask import current_app as app
|
|
|
|
from sqlalchemy import Column, Unicode, UniqueConstraint
|
2018-04-27 17:16:43 +00:00
|
|
|
from sqlalchemy.dialects.postgresql import UUID
|
2018-05-30 10:49:40 +00:00
|
|
|
from sqlalchemy_utils import CountryType, EmailType, PasswordType
|
2018-04-27 17:16:43 +00:00
|
|
|
|
2018-05-30 10:49:40 +00:00
|
|
|
from ereuse_devicehub.resources.models import STR_SIZE, STR_SM_SIZE, Thing
|
2018-04-27 17:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class User(Thing):
|
|
|
|
__table_args__ = {'schema': 'common'}
|
|
|
|
id = Column(UUID(as_uuid=True), default=uuid4, primary_key=True)
|
|
|
|
email = Column(EmailType, nullable=False, unique=True)
|
|
|
|
password = Column(PasswordType(max_length=STR_SIZE,
|
|
|
|
onload=lambda **kwargs: dict(
|
2018-05-30 10:49:40 +00:00
|
|
|
schemes=app.config['PASSWORD_SCHEMES'],
|
2018-04-27 17:16:43 +00:00
|
|
|
**kwargs
|
|
|
|
)))
|
|
|
|
"""
|
|
|
|
Password field.
|
|
|
|
From `here <https://sqlalchemy-utils.readthedocs.io/en/latest/
|
|
|
|
data_types.html#module-sqlalchemy_utils.types.password>`_
|
|
|
|
"""
|
|
|
|
name = Column(Unicode(length=STR_SIZE))
|
|
|
|
token = Column(UUID(as_uuid=True), default=uuid4, unique=True)
|
2018-05-13 13:13:12 +00:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
2018-05-30 10:49:40 +00:00
|
|
|
return '<{0.t} {0.id} email={0.email}>'.format(self)
|
|
|
|
|
|
|
|
|
|
|
|
class Organization(Thing):
|
|
|
|
id = Column(UUID(as_uuid=True), default=uuid4, primary_key=True)
|
|
|
|
name = Column(Unicode(length=STR_SM_SIZE), unique=True)
|
|
|
|
tax_id = Column(Unicode(length=STR_SM_SIZE),
|
|
|
|
comment='The Tax / Fiscal ID of the organization, '
|
|
|
|
'e.g. the TIN in the US or the CIF/NIF in Spain.')
|
|
|
|
country = Column(CountryType, comment='Country issuing the tax_id number.')
|
|
|
|
|
|
|
|
__table_args__ = (
|
|
|
|
UniqueConstraint(tax_id, country, name='Registration Number per country.'),
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_default_org(cls) -> 'Organization':
|
|
|
|
"""Retrieves the default organization."""
|
|
|
|
return Organization.query.filter_by(**app.config.get_namespace('ORGANIZATION_')).one()
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return '<Org {0.id}: {0.name}>'.format(self)
|