| from datetime import UTC, datetime |
| from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, String |
| from sqlalchemy.orm import Mapped, mapped_column, relationship |
| from app.core.database import Base |
|
|
|
|
| class User(Base): |
| __tablename__ = "users" |
|
|
| id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) |
| email: Mapped[str] = mapped_column(String(255), unique=True, index=True, nullable=False) |
| full_name: Mapped[str | None] = mapped_column(String(255), nullable=True) |
| hashed_password: Mapped[str] = mapped_column(String(255), nullable=False) |
| role: Mapped[str] = mapped_column(String(255), nullable=False) |
| is_admin: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False) |
| department_id: Mapped[int | None] = mapped_column( |
| ForeignKey("departments.id", ondelete="SET NULL"), |
| nullable=True, |
| ) |
| is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False) |
| created_at: Mapped[datetime] = mapped_column( |
| DateTime, default=lambda: datetime.now(UTC), nullable=False |
| ) |
| department = relationship("Department", back_populates="users") |
|
|
| @property |
| def department_name(self) -> str | None: |
| if self.department is None: |
| return None |
| return self.department.name |
|
|