felicity-lims/backend/felicity_lims/felicity/apps/client/models.py

99 lines
3.7 KiB
Python
Raw Normal View History

2021-01-06 19:52:14 +08:00
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship
from sqlalchemy.orm import backref
from felicity.apps import BaseAuditDBModel
2021-01-06 19:52:14 +08:00
from felicity.apps.user.abstract import AbstractBaseUser
from felicity.apps.user import conf
from felicity.apps.user.models import UserAuth
from felicity.apps.setup.models import District
from felicity.apps.setup.models import Province
from felicity.apps.client import schemas
class Client(BaseAuditDBModel):
2021-01-06 19:52:14 +08:00
"""Client/Facility"""
name = Column(String, nullable=False)
code = Column(String, index=True, unique=True, nullable=False)
district_uid = Column(Integer, ForeignKey("district.uid"), nullable=True)
2021-11-08 01:01:12 +08:00
district = relationship(District, backref="clients", lazy="selectin")
2021-01-06 19:52:14 +08:00
province_uid = Column(Integer, ForeignKey("province.uid"), nullable=True)
2021-11-08 01:01:12 +08:00
province = relationship(Province, backref="clients", lazy="selectin")
2021-01-06 19:52:14 +08:00
email = Column(String, nullable=True)
email_cc = Column(String, nullable=True)
consent_email = Column(Boolean(), default=False)
phone_mobile = Column(String, nullable=True)
phone_business = Column(String, nullable=True)
consent_sms = Column(Boolean(), default=False)
2021-05-30 22:32:13 +08:00
internal_use = Column(Boolean(), default=False) # e.g Test Client
2021-01-06 19:52:14 +08:00
active = Column(Boolean(), default=False)
2021-01-06 19:52:14 +08:00
@classmethod
2021-09-22 15:34:32 +08:00
async def create(cls, obj_in: schemas.ClientCreate) -> schemas.Client:
2021-09-27 23:45:22 +08:00
exist = await cls.get(code=obj_in.code)
if exist:
2021-01-06 19:52:14 +08:00
raise Exception(f"Client with code {obj_in.code} already Exists")
data = cls._import(obj_in)
2021-09-22 15:34:32 +08:00
return await super().create(**data)
2021-01-06 19:52:14 +08:00
2021-09-22 15:34:32 +08:00
async def update(self, obj_in: schemas.ClientUpdate) -> schemas.Client:
2021-01-06 19:52:14 +08:00
data = self._import(obj_in)
2021-09-22 15:34:32 +08:00
return await super().update(**data)
2021-01-06 19:52:14 +08:00
@property
def get_province(self):
if self.district:
return self.district.province.name
elif self.province:
return self.province.name
else:
return ""
2021-01-06 19:52:14 +08:00
@property
def get_country(self):
if self.district:
return self.district.province.country.name
elif self.province:
return self.province.country.name
else:
return ""
class ClientContact(AbstractBaseUser):
auth_uid = Column(Integer, ForeignKey('userauth.uid'), nullable=True)
auth = relationship(UserAuth, backref=backref(conf.CLIENT_CONTACT, uselist=False))
2021-03-26 04:22:59 +08:00
email = Column(String, unique=False, index=True, nullable=True)
2021-01-06 19:52:14 +08:00
email_cc = Column(String, nullable=True)
consent_sms = Column(Boolean(), default=False)
2021-03-26 04:22:59 +08:00
client_uid = Column(Integer, ForeignKey('client.uid'), nullable=False)
client = relationship(Client, backref=backref('contacts', uselist=False))
@classmethod
2021-09-22 15:34:32 +08:00
async def create(cls, contact_in: schemas.ClientContactCreate) -> schemas.ClientContact:
2021-03-26 04:22:59 +08:00
data = cls._import(contact_in)
2021-09-22 15:34:32 +08:00
return await super().create(**data)
2021-03-26 04:22:59 +08:00
2021-09-22 15:34:32 +08:00
async def update(self, contact_in: schemas.ClientContactUpdate) -> schemas.ClientContact:
2021-03-26 04:22:59 +08:00
data = self._import(contact_in)
2021-09-22 15:34:32 +08:00
return await super().update(**data)
2021-01-06 19:52:14 +08:00
@property
def user_type(self):
return conf.CLIENT_CONTACT
2021-09-22 15:34:32 +08:00
async def propagate_user_type(self):
"""sets the user_type field in auth"""
2021-09-22 15:34:32 +08:00
await self.auth.acquire_user_type(conf.CLIENT_CONTACT)
2021-09-22 15:34:32 +08:00
async def unlink_auth(self):
auth = self.auth
_update = {**self.to_dict(), **{'auth_uid': None, 'auth': None}}
2021-09-22 15:34:32 +08:00
await self.update(_update)
2021-01-06 19:52:14 +08:00
if not self.auth:
auth.delete()
2021-09-22 15:34:32 +08:00
async def link_auth(self, auth_uid):
_update = {**self.to_dict(), **{'auth_uid': auth_uid}}
2021-09-22 15:34:32 +08:00
await self.update(_update)