113 lines
No EOL
4.3 KiB
Python
113 lines
No EOL
4.3 KiB
Python
"""SaaS multi-tenancy models: User, Organization, OrganizationMember, StatusPage."""
|
|
|
|
import uuid
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import Boolean, DateTime, ForeignKey, String, UniqueConstraint
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.database import Base
|
|
|
|
|
|
def _uuid_str() -> str:
|
|
return str(uuid.uuid4())
|
|
|
|
|
|
class User(Base):
|
|
"""Individual who can log in."""
|
|
|
|
__tablename__ = "users"
|
|
|
|
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid_str)
|
|
email: Mapped[str] = mapped_column(
|
|
String(255), unique=True, nullable=False, index=True
|
|
)
|
|
password_hash: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
display_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
|
is_email_verified: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
email_verify_token: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
|
|
)
|
|
|
|
memberships: Mapped[list["OrganizationMember"]] = relationship(
|
|
back_populates="user"
|
|
)
|
|
|
|
|
|
class Organization(Base):
|
|
"""The tenant; owns status pages."""
|
|
|
|
__tablename__ = "organizations"
|
|
|
|
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid_str)
|
|
slug: Mapped[str] = mapped_column(
|
|
String(50), unique=True, nullable=False, index=True
|
|
)
|
|
name: Mapped[str] = mapped_column(String(100), nullable=False)
|
|
tier: Mapped[str] = mapped_column(String(20), nullable=False, default="free")
|
|
# "free" | "pro" | "team"
|
|
stripe_customer_id: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
|
custom_domain: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
|
|
)
|
|
members: Mapped[list["OrganizationMember"]] = relationship(
|
|
back_populates="organization"
|
|
)
|
|
status_pages: Mapped[list["StatusPage"]] = relationship(
|
|
back_populates="organization"
|
|
)
|
|
# Services linked to this org (from app.models.models.Service)
|
|
|
|
|
|
class OrganizationMember(Base):
|
|
"""Joins users to orgs with roles."""
|
|
|
|
__tablename__ = "organization_members"
|
|
|
|
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid_str)
|
|
organization_id: Mapped[str] = mapped_column(
|
|
String(36), ForeignKey("organizations.id"), nullable=False, index=True
|
|
)
|
|
user_id: Mapped[str] = mapped_column(
|
|
String(36), ForeignKey("users.id"), nullable=False, index=True
|
|
)
|
|
role: Mapped[str] = mapped_column(String(20), nullable=False, default="member")
|
|
# "owner" | "admin" | "member"
|
|
joined_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
|
|
|
organization: Mapped["Organization"] = relationship(back_populates="members")
|
|
user: Mapped["User"] = relationship(back_populates="memberships")
|
|
|
|
__table_args__ = (
|
|
{"sqlite_autoincrement": True},
|
|
)
|
|
|
|
|
|
class StatusPage(Base):
|
|
"""Per-organization status page."""
|
|
|
|
__tablename__ = "status_pages"
|
|
|
|
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid_str)
|
|
organization_id: Mapped[str] = mapped_column(
|
|
String(36), ForeignKey("organizations.id"), nullable=False, index=True
|
|
)
|
|
slug: Mapped[str] = mapped_column(String(50), nullable=False, index=True)
|
|
title: Mapped[str] = mapped_column(String(100), nullable=False)
|
|
subdomain: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
|
custom_domain: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
|
is_public: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
|
|
)
|
|
|
|
organization: Mapped["Organization"] = relationship(back_populates="status_pages")
|
|
|
|
__table_args__ = (
|
|
UniqueConstraint("organization_id", "slug", name="uq_status_page_org_slug"),
|
|
) |