2018-08-03 16:15:08 +00:00
|
|
|
from itertools import chain
|
|
|
|
from operator import attrgetter
|
|
|
|
from uuid import uuid4
|
|
|
|
|
2018-09-30 10:29:33 +00:00
|
|
|
from citext import CIText
|
2022-04-06 10:35:08 +00:00
|
|
|
from sqlalchemy import Column
|
|
|
|
from sqlalchemy import Enum as DBEnum
|
|
|
|
from sqlalchemy import ForeignKey, Unicode, UniqueConstraint
|
2018-08-03 16:15:08 +00:00
|
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
|
|
from sqlalchemy.ext.declarative import declared_attr
|
2018-09-20 07:28:52 +00:00
|
|
|
from sqlalchemy.orm import backref, relationship, validates
|
2018-08-03 16:15:08 +00:00
|
|
|
from sqlalchemy_utils import EmailType, PhoneNumberType
|
|
|
|
from teal import enums
|
2019-01-23 15:55:04 +00:00
|
|
|
from teal.db import INHERIT_COND, POLYMORPHIC_ID, POLYMORPHIC_ON, check_lower
|
2018-09-20 07:28:52 +00:00
|
|
|
from teal.marshmallow import ValidationError
|
2018-08-03 16:15:08 +00:00
|
|
|
|
2019-01-23 15:55:04 +00:00
|
|
|
from ereuse_devicehub.db import db
|
|
|
|
from ereuse_devicehub.resources.inventory import Inventory
|
2018-09-30 10:29:33 +00:00
|
|
|
from ereuse_devicehub.resources.models import STR_SM_SIZE, Thing
|
2018-09-07 10:38:02 +00:00
|
|
|
from ereuse_devicehub.resources.user.models import User
|
|
|
|
|
2018-08-03 16:15:08 +00:00
|
|
|
|
|
|
|
class JoinedTableMixin:
|
|
|
|
# noinspection PyMethodParameters
|
|
|
|
@declared_attr
|
|
|
|
def id(cls):
|
|
|
|
return Column(UUID(as_uuid=True), ForeignKey(Agent.id), primary_key=True)
|
|
|
|
|
|
|
|
|
|
|
|
class Agent(Thing):
|
|
|
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
|
2019-02-07 12:47:42 +00:00
|
|
|
type = Column(Unicode, nullable=False)
|
2018-09-30 10:29:33 +00:00
|
|
|
name = Column(CIText())
|
2022-04-06 10:35:08 +00:00
|
|
|
last_name = Column(CIText())
|
2019-06-19 11:35:26 +00:00
|
|
|
name.comment = """The name of the organization or person."""
|
2018-09-30 10:29:33 +00:00
|
|
|
tax_id = Column(Unicode(length=STR_SM_SIZE), check_lower('tax_id'))
|
2022-04-06 11:49:15 +00:00
|
|
|
tax_id.comment = """The Tax / Fiscal ID of the organization,
|
2019-06-19 11:35:26 +00:00
|
|
|
e.g. the TIN in the US or the CIF/NIF in Spain.
|
2018-08-03 16:15:08 +00:00
|
|
|
"""
|
|
|
|
country = Column(DBEnum(enums.Country))
|
2019-06-19 11:35:26 +00:00
|
|
|
country.comment = """Country issuing the tax_id number."""
|
2018-08-03 16:15:08 +00:00
|
|
|
telephone = Column(PhoneNumberType())
|
|
|
|
email = Column(EmailType, unique=True)
|
|
|
|
|
|
|
|
__table_args__ = (
|
|
|
|
UniqueConstraint(tax_id, country, name='Registration Number per country.'),
|
2019-02-07 12:47:42 +00:00
|
|
|
UniqueConstraint(tax_id, name, name='One tax ID with one name.'),
|
2022-04-06 10:35:08 +00:00
|
|
|
db.Index('agent_type', type, postgresql_using='hash'),
|
2018-08-03 16:15:08 +00:00
|
|
|
)
|
|
|
|
|
2022-04-06 10:35:08 +00:00
|
|
|
@property
|
|
|
|
def get_full_name(self):
|
|
|
|
if self.last_name:
|
|
|
|
return "{} {}".format(self.name, self.last_name)
|
|
|
|
return self.name
|
|
|
|
|
2018-08-03 16:15:08 +00:00
|
|
|
@declared_attr
|
|
|
|
def __mapper_args__(cls):
|
2019-06-19 11:35:26 +00:00
|
|
|
"""Defines inheritance.
|
2018-08-03 16:15:08 +00:00
|
|
|
|
|
|
|
From `the guide <http://docs.sqlalchemy.org/en/latest/orm/
|
|
|
|
extensions/declarative/api.html
|
|
|
|
#sqlalchemy.ext.declarative.declared_attr>`_
|
|
|
|
"""
|
|
|
|
args = {POLYMORPHIC_ID: cls.t}
|
|
|
|
if cls.t == 'Agent':
|
|
|
|
args[POLYMORPHIC_ON] = cls.type
|
|
|
|
if JoinedTableMixin in cls.mro():
|
|
|
|
args[INHERIT_COND] = cls.id == Agent.id
|
|
|
|
return args
|
|
|
|
|
|
|
|
@property
|
2019-05-11 14:27:22 +00:00
|
|
|
def actions(self) -> list:
|
2018-08-03 16:15:08 +00:00
|
|
|
# todo test
|
2022-04-06 10:35:08 +00:00
|
|
|
return sorted(
|
|
|
|
chain(self.actions_agent, self.actions_to), key=attrgetter('created')
|
|
|
|
)
|
2018-08-03 16:15:08 +00:00
|
|
|
|
2018-09-20 07:28:52 +00:00
|
|
|
@validates('name')
|
|
|
|
def does_not_contain_slash(self, _, value: str):
|
|
|
|
if '/' in value:
|
|
|
|
raise ValidationError('Name cannot contain slash \'')
|
|
|
|
return value
|
|
|
|
|
2018-08-03 16:15:08 +00:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
return '<{0.t} {0.name}>'.format(self)
|
|
|
|
|
|
|
|
|
|
|
|
class Organization(JoinedTableMixin, Agent):
|
2022-04-06 10:35:08 +00:00
|
|
|
default_of = db.relationship(
|
|
|
|
Inventory,
|
|
|
|
uselist=False,
|
|
|
|
lazy=True,
|
|
|
|
backref=backref('org', lazy=True),
|
|
|
|
# We need to use this as we cannot do Inventory.foreign -> Org
|
|
|
|
# as foreign keys can only reference to one table
|
|
|
|
# and we have multiple organization table (one per schema)
|
|
|
|
foreign_keys=[Inventory.org_id],
|
|
|
|
primaryjoin=lambda: Organization.id == Inventory.org_id,
|
|
|
|
)
|
2019-01-23 15:55:04 +00:00
|
|
|
|
2018-08-03 16:15:08 +00:00
|
|
|
def __init__(self, name: str, **kwargs) -> None:
|
|
|
|
super().__init__(**kwargs, name=name)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_default_org_id(cls) -> UUID:
|
|
|
|
"""Retrieves the default organization."""
|
2019-01-23 15:55:04 +00:00
|
|
|
return cls.query.filter_by(default_of=Inventory.current).one().id
|
2018-08-03 16:15:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Individual(JoinedTableMixin, Agent):
|
|
|
|
active_org_id = Column(UUID(as_uuid=True), ForeignKey(Organization.id))
|
2022-04-06 10:35:08 +00:00
|
|
|
|
|
|
|
active_org = relationship(
|
|
|
|
Organization, primaryjoin=active_org_id == Organization.id
|
|
|
|
)
|
2018-08-03 16:15:08 +00:00
|
|
|
|
2018-09-20 14:40:41 +00:00
|
|
|
user_id = Column(UUID(as_uuid=True), ForeignKey(User.id), unique=True)
|
2022-04-06 10:35:08 +00:00
|
|
|
user = relationship(
|
|
|
|
User,
|
|
|
|
backref=backref('individuals', lazy=True, collection_class=set),
|
|
|
|
primaryjoin=user_id == User.id,
|
|
|
|
)
|
2018-09-20 14:40:41 +00:00
|
|
|
|
2018-08-03 16:15:08 +00:00
|
|
|
|
|
|
|
class Membership(Thing):
|
|
|
|
"""Organizations that are related to the Individual.
|
|
|
|
|
|
|
|
For example, because the individual works in or because is a member of.
|
|
|
|
"""
|
2022-04-06 10:35:08 +00:00
|
|
|
|
2018-09-30 10:29:33 +00:00
|
|
|
id = Column(Unicode(), check_lower('id'))
|
2022-04-06 10:35:08 +00:00
|
|
|
organization_id = Column(
|
|
|
|
UUID(as_uuid=True), ForeignKey(Organization.id), primary_key=True
|
|
|
|
)
|
|
|
|
organization = relationship(
|
|
|
|
Organization,
|
|
|
|
backref=backref('members', collection_class=set, lazy=True),
|
|
|
|
primaryjoin=organization_id == Organization.id,
|
|
|
|
)
|
|
|
|
individual_id = Column(
|
|
|
|
UUID(as_uuid=True), ForeignKey(Individual.id), primary_key=True
|
|
|
|
)
|
|
|
|
individual = relationship(
|
|
|
|
Individual,
|
|
|
|
backref=backref('member_of', collection_class=set, lazy=True),
|
|
|
|
primaryjoin=individual_id == Individual.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, organization: Organization, individual: Individual, id: str = None
|
|
|
|
) -> None:
|
|
|
|
super().__init__(organization=organization, individual=individual, id=id)
|
2018-08-03 16:15:08 +00:00
|
|
|
|
|
|
|
__table_args__ = (
|
|
|
|
UniqueConstraint(id, organization_id, name='One member id per organization.'),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Person(Individual):
|
2019-06-19 11:35:26 +00:00
|
|
|
"""A person in the system. There can be several persons pointing to
|
2018-08-03 16:15:08 +00:00
|
|
|
a real.
|
|
|
|
"""
|
2022-04-06 10:35:08 +00:00
|
|
|
|
2018-08-03 16:15:08 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class System(Individual):
|
|
|
|
pass
|