1
0
Fork 0
forked from sr2/cloud-api
cloud-api/src/organisation/models.py

52 lines
1.6 KiB
Python
Raw Normal View History

2026-04-06 12:41:49 +01:00
"""
Database models for organisation module
Models:
- Organisation: id[pk], name, status, intake_questionnaire,
billing_contact_id[fk], security_contact_id[fk], owner_contact_id[fk]
- OrgUsers: org_id[fk][cpk], user_id[fk][cpk], is_admin
"""
from sqlalchemy import Column, Integer, String, ForeignKey, JSON
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)
name = Column(String, unique=True)
2026-04-06 12:41:49 +01:00
status = Column(String, default="partial")
intake_questionnaire = Column(JSON)
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"))
user_rel = relationship(
"User",
secondary="orgusers",
back_populates="organisation_rel"
)
group_rel = relationship("Group", back_populates="org_rel")
root_user_rel = relationship("User", foreign_keys=[root_user_id])
@property
def root_user_email(self):
return self.root_user_rel.email if self.root_user_rel else None
billing_contact_rel = relationship("Contact", foreign_keys=[billing_contact_id])
security_contact_rel = relationship("Contact", foreign_keys=[security_contact_id])
owner_contact_rel = relationship("Contact", foreign_keys=[owner_contact_id])
2026-04-06 12:41:49 +01:00
class OrgUsers(Base):
__tablename__ = "orgusers"
org_id = Column(Integer, ForeignKey("organisation.id", ondelete="CASCADE"), primary_key=True)
user_id = Column(Integer, ForeignKey("user.id", ondelete="CASCADE"), primary_key=True)