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

109 lines
3.7 KiB
Python
Raw Normal View History

2023-04-10 20:23:31 +08:00
from apps import BaseAuditDBModel
from apps.client import schemas
from apps.setup.models import District, Province
from apps.user import conf
from apps.user.abstract import AbstractBaseUser
from apps.user.models import UserAuth
from core.uid_gen import FelicitySAID
2023-03-19 23:21:32 +08:00
from sqlalchemy import Boolean, Column, ForeignKey, String
from sqlalchemy.orm import backref, relationship
2021-01-06 19:52:14 +08:00
class Client(BaseAuditDBModel):
2021-01-06 19:52:14 +08:00
"""Client/Facility"""
2021-01-06 19:52:14 +08:00
name = Column(String, nullable=False)
code = Column(String, index=True, unique=True, nullable=False)
2023-03-19 23:21:32 +08:00
district_uid = Column(FelicitySAID, ForeignKey("district.uid"), nullable=True)
2021-11-08 01:01:12 +08:00
district = relationship(District, backref="clients", lazy="selectin")
2023-03-19 23:21:32 +08:00
province_uid = Column(FelicitySAID, 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):
2023-03-19 23:21:32 +08:00
auth_uid = Column(FelicitySAID, ForeignKey("userauth.uid"), nullable=True)
2021-01-06 19:52:14 +08:00
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)
2023-03-19 23:21:32 +08:00
client_uid = Column(FelicitySAID, ForeignKey("client.uid"), nullable=False)
client = relationship(
Client,
backref=backref(
"contacts",
uselist=False,
),
lazy="selectin",
)
2021-03-26 04:22:59 +08:00
@classmethod
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
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)