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-08 15:31:37 +01:00
|
|
|
|
2026-05-25 09:05:17 +01:00
|
|
|
from sqlalchemy import Column, Integer, String, ForeignKey, JSON
|
2026-05-25 10:21:15 +01:00
|
|
|
from sqlalchemy.orm import relationship
|
2026-04-06 12:41:49 +01:00
|
|
|
|
|
|
|
|
from src.database import Base
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Organisation(Base):
|
|
|
|
|
__tablename__ = "organisation"
|
|
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
2026-05-25 09:05:17 +01:00
|
|
|
name = Column(String, unique=True)
|
2026-04-06 12:41:49 +01:00
|
|
|
status = Column(String, default="partial")
|
|
|
|
|
intake_questionnaire = Column(JSON)
|
|
|
|
|
|
2026-05-25 09:05:17 +01:00
|
|
|
root_user_id = Column(Integer, ForeignKey("user.id"))
|
|
|
|
|
|
2026-04-06 12:41:49 +01:00
|
|
|
billing_contact_id = Column(Integer, ForeignKey("contact.id"))
|
|
|
|
|
security_contact_id = Column(Integer, ForeignKey("contact.id"))
|
|
|
|
|
owner_contact_id = Column(Integer, ForeignKey("contact.id"))
|
|
|
|
|
|
2026-05-25 12:06:24 +01:00
|
|
|
user_rel = relationship(
|
2026-06-08 15:31:37 +01:00
|
|
|
"User", secondary="orgusers", back_populates="organisation_rel"
|
2026-05-25 10:21:15 +01:00
|
|
|
)
|
|
|
|
|
|
2026-05-25 12:06:24 +01:00
|
|
|
group_rel = relationship("Group", back_populates="org_rel")
|
2026-06-16 11:19:22 +01:00
|
|
|
root_user_rel = relationship("User", foreign_keys="Organisation.root_user_id")
|
2026-05-25 12:40:28 +01:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def root_user_email(self):
|
|
|
|
|
return self.root_user_rel.email if self.root_user_rel else None
|
2026-05-25 12:06:24 +01:00
|
|
|
|
2026-06-16 11:19:22 +01:00
|
|
|
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"
|
|
|
|
|
)
|
2026-05-25 15:15:50 +01:00
|
|
|
|
2026-06-16 13:51:31 +01:00
|
|
|
permission_rel = relationship(
|
|
|
|
|
"Permission", secondary="org_permissions", back_populates="org_rel"
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-06 12:41:49 +01:00
|
|
|
|
|
|
|
|
class OrgUsers(Base):
|
|
|
|
|
__tablename__ = "orgusers"
|
|
|
|
|
|
2026-06-08 15:31:37 +01:00
|
|
|
org_id = Column(
|
|
|
|
|
Integer, ForeignKey("organisation.id", ondelete="CASCADE"), primary_key=True
|
|
|
|
|
)
|
|
|
|
|
user_id = Column(
|
|
|
|
|
Integer, ForeignKey("user.id", ondelete="CASCADE"), primary_key=True
|
|
|
|
|
)
|