74 lines
2.5 KiB
Python
74 lines
2.5 KiB
Python
"""
|
|
Database models for organisation module
|
|
|
|
Models:
|
|
- Organisation:
|
|
- id[PK], name, status, intake_questionnaire, root_user_id[FK], billing_contact_id[FK], security_contact_id[FK], owner_contact_id[FK]
|
|
- user_rel: ORM relationship to User via OrgUsers relationship table
|
|
- group_rel: ORM relationship to Group, backprops Group.org_rel
|
|
- root_user_rel: ORM relationship to User with root_user_id FK
|
|
- root_user_email: Calc property root_user_rel.email
|
|
- billing_contact_rel: ORM relationship to Contact with billing_contact FK
|
|
- security_contact_rel: ORM relationship to Contact with security_contact FK
|
|
- owner_contact_rel: ORM relationship to Contact with owner_contact FK
|
|
- OrgUsers: org_id[FK][PK], user_id[FK][PK]
|
|
"""
|
|
|
|
from typing import Any
|
|
|
|
from sqlalchemy import ForeignKey
|
|
from sqlalchemy.orm import relationship, Mapped, mapped_column
|
|
|
|
from src.models import CustomBase
|
|
|
|
|
|
class Organisation(CustomBase):
|
|
__tablename__ = "organisation"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
name: Mapped[str]
|
|
status: Mapped[str] = mapped_column(default="partial")
|
|
intake_questionnaire: Mapped[dict[str, Any] | None]
|
|
|
|
root_user_id: Mapped[int] = mapped_column(ForeignKey("user.id"))
|
|
billing_contact_id: Mapped[int] = mapped_column(ForeignKey("contact.id"), nullable=True)
|
|
security_contact_id: Mapped[int] = mapped_column(
|
|
ForeignKey("contact.id"), nullable=True
|
|
)
|
|
owner_contact_id: Mapped[int] = mapped_column(ForeignKey("contact.id"), nullable=True)
|
|
|
|
user_rel = relationship("User", secondary="orgusers", back_populates="organisation_rel")
|
|
|
|
group_rel = relationship(
|
|
"Group", back_populates="org_rel", cascade="all, delete-orphan"
|
|
)
|
|
root_user_rel = relationship("User", foreign_keys="Organisation.root_user_id")
|
|
|
|
billing_contact_rel = relationship(
|
|
"Contact", foreign_keys="Organisation.billing_contact_id"
|
|
)
|
|
security_contact_rel = relationship(
|
|
"Contact", foreign_keys="Organisation.security_contact_id"
|
|
)
|
|
owner_contact_rel = relationship(
|
|
"Contact", foreign_keys="Organisation.owner_contact_id"
|
|
)
|
|
|
|
permission_rel = relationship(
|
|
"Permission", secondary="org_permissions", back_populates="org_rel"
|
|
)
|
|
|
|
@property
|
|
def root_user_email(self) -> str:
|
|
return self.root_user_rel.email if self.root_user_rel else ""
|
|
|
|
|
|
class OrgUsers(CustomBase):
|
|
__tablename__ = "orgusers"
|
|
|
|
org_id: Mapped[int] = mapped_column(
|
|
ForeignKey("organisation.id", ondelete="CASCADE"), primary_key=True
|
|
)
|
|
user_id: Mapped[int] = mapped_column(
|
|
ForeignKey("user.id", ondelete="CASCADE"), primary_key=True
|
|
)
|