Spaces:
Running
Running
| """ | |
| GraphoLab Backend — User and Organization ORM models. | |
| """ | |
| from __future__ import annotations | |
| import enum | |
| from datetime import datetime | |
| from sqlalchemy import Boolean, DateTime, Enum, ForeignKey, String, func | |
| from sqlalchemy.orm import Mapped, mapped_column, relationship | |
| from backend.database import Base | |
| class Role(str, enum.Enum): | |
| admin = "admin" | |
| examiner = "examiner" # perito — full read/write on own projects | |
| viewer = "viewer" # sola lettura | |
| class Organization(Base): | |
| __tablename__ = "organizations" | |
| id: Mapped[int] = mapped_column(primary_key=True, index=True) | |
| name: Mapped[str] = mapped_column(String(128), unique=True, nullable=False) | |
| created_at: Mapped[datetime] = mapped_column( | |
| DateTime(timezone=True), server_default=func.now() | |
| ) | |
| users: Mapped[list[User]] = relationship("User", back_populates="organization") | |
| class User(Base): | |
| __tablename__ = "users" | |
| id: Mapped[int] = mapped_column(primary_key=True, index=True) | |
| email: Mapped[str] = mapped_column(String(256), unique=True, index=True, nullable=False) | |
| full_name: Mapped[str] = mapped_column(String(256), nullable=False) | |
| hashed_password: Mapped[str] = mapped_column(String(256), nullable=False) | |
| role: Mapped[Role] = mapped_column(Enum(Role), default=Role.examiner, nullable=False) | |
| is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False) | |
| organization_id: Mapped[int | None] = mapped_column( | |
| ForeignKey("organizations.id", ondelete="SET NULL"), nullable=True | |
| ) | |
| organization: Mapped[Organization | None] = relationship( | |
| "Organization", back_populates="users" | |
| ) | |
| created_at: Mapped[datetime] = mapped_column( | |
| DateTime(timezone=True), server_default=func.now() | |
| ) | |
| updated_at: Mapped[datetime] = mapped_column( | |
| DateTime(timezone=True), server_default=func.now(), onupdate=func.now() | |
| ) | |
| projects: Mapped[list] = relationship("Project", back_populates="owner") | |
| settings: Mapped["UserSettings | None"] = relationship( # type: ignore[name-defined] | |
| "UserSettings", back_populates="user", uselist=False, cascade="all, delete-orphan" | |
| ) | |