Spaces:
Running
Running
Download core/tenancy/models.py from Aynkhn/WorkPilot-AI: direct link, hf CLI and curl.
- Browser
- Download file 2.61 kB
-
https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/core/tenancy/models.py
- Command line
-
hf download hf://spaces/Aynkhn/WorkPilot-AI/core/tenancy/models.py
-
curl -L -o models.py https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/core/tenancy/models.py
2.61 kB
| """Tenants, users and memberships.""" | |
| from __future__ import annotations | |
| import enum | |
| import uuid | |
| from sqlalchemy import Boolean, Enum, ForeignKey, String, UniqueConstraint | |
| from sqlalchemy.dialects.postgresql import JSONB, UUID | |
| from sqlalchemy.orm import Mapped, mapped_column | |
| from core.db.base import Base, TenantMixin, TimestampMixin, new_id | |
| class Role(enum.StrEnum): | |
| OWNER = "owner" | |
| MANAGER = "manager" | |
| AGENT_OPERATOR = "agent_operator" | |
| VIEWER = "viewer" | |
| ROLE_RANK: dict[Role, int] = { | |
| Role.VIEWER: 0, | |
| Role.AGENT_OPERATOR: 1, | |
| Role.MANAGER: 2, | |
| Role.OWNER: 3, | |
| } | |
| class Tenant(TimestampMixin, Base): | |
| """A business. RLS: a session sees only the tenant bound in ``app.tenant_id``.""" | |
| __tablename__ = "tenants" | |
| __rls__ = True # policy on ``id``; see migration 0001 | |
| id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=new_id) | |
| name: Mapped[str] = mapped_column(String(200), nullable=False) | |
| plan: Mapped[str] = mapped_column(String(50), default="free", nullable=False) | |
| locale: Mapped[str] = mapped_column(String(20), default="en", nullable=False) | |
| timezone: Mapped[str] = mapped_column(String(64), default="Asia/Karachi", nullable=False) | |
| settings: Mapped[dict] = mapped_column(JSONB, default=dict, nullable=False) | |
| class User(TimestampMixin, Base): | |
| """A person. Global (not tenant-scoped): one user may belong to several tenants.""" | |
| __tablename__ = "users" | |
| id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=new_id) | |
| email: Mapped[str] = mapped_column(String(320), unique=True, nullable=False) | |
| password_hash: Mapped[str] = mapped_column(String(255), nullable=False) | |
| totp_secret: Mapped[str | None] = mapped_column(String(64), nullable=True) | |
| is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False) | |
| class Membership(TenantMixin, TimestampMixin, Base): | |
| """User ↔ tenant with a role. RLS also lets a user see their own memberships.""" | |
| __tablename__ = "memberships" | |
| __table_args__ = (UniqueConstraint("tenant_id", "user_id", name="uq_memberships_tenant_user"),) | |
| id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=new_id) | |
| user_id: Mapped[uuid.UUID] = mapped_column( | |
| UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False | |
| ) | |
| role: Mapped[Role] = mapped_column( | |
| Enum(Role, name="role", values_callable=lambda e: [m.value for m in e]), nullable=False | |
| ) | |