cloud-api/src/organisation/models.py

75 lines
2.5 KiB
Python
Raw Normal View History

2026-04-06 12:41:49 +01:00
"""
Database models for organisation module
Models:
2026-05-28 14:23:36 +01:00
- 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]
2026-04-06 12:41:49 +01:00
"""
2026-06-20 18:42:36 +01:00
from typing import Any
2026-06-20 18:42:36 +01:00
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship, Mapped, mapped_column
2026-04-06 12:41:49 +01:00
2026-06-20 18:42:36 +01:00
from src.models import CustomBase
2026-04-06 12:41:49 +01:00
2026-06-20 18:42:36 +01:00
class Organisation(CustomBase):
2026-04-06 12:41:49 +01:00
__tablename__ = "organisation"
2026-06-20 18:42:36 +01:00
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]
2026-04-06 12:41:49 +01:00
2026-06-20 18:42:36 +01:00
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"
)
2026-06-20 18:42:36 +01:00
@property
def root_user_email(self) -> str:
return self.root_user_rel.email if self.root_user_rel else ""
2026-04-06 12:41:49 +01:00
2026-06-20 18:42:36 +01:00
class OrgUsers(CustomBase):
2026-04-06 12:41:49 +01:00
__tablename__ = "orgusers"
2026-06-20 18:42:36 +01:00
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
)