/
ivanstrike
/
tasker
Обзор
Документация
Войти
/
ivanstrike
/
tasker
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
python_Sklyarenko/Lab1/app/models.py
67 строк
3 KB
ivanstrike
Lab3
13 дек 2025, 11:15
13 дек 2025, 11:15
15c0fa5
Код
Авторство
О чём код?
from sqlalchemy import Column, Integer, String, DateTime, Enum, ForeignKey, Text, UniqueConstraint from sqlalchemy.orm import relationship, Mapped, mapped_column from datetime import datetime import enum from .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) password_hash: Mapped[str] = mapped_column(String(255), nullable=False) created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow) projects = relationship("Project", back_populates="owner") tasks = relationship("Task", back_populates="assignee") class Project(Base): __tablename__ = "projects" id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) name: Mapped[str] = mapped_column(String(255), nullable=False, index=True) description: Mapped[str | None] = mapped_column(Text, nullable=True) owner_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False) created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow) owner = relationship("User", back_populates="projects") tasks = relationship("Task", back_populates="project", cascade="all, delete-orphan") class TaskStatus(str, enum.Enum): todo = "todo" in_progress = "in_progress" done = "done" class TaskPriority(str, enum.Enum): low = "low" medium = "medium" high = "high" class Task(Base): __tablename__ = "tasks" id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) title: Mapped[str] = mapped_column(String(255), nullable=False, index=True) description: Mapped[str | None] = mapped_column(Text, nullable=True) status: Mapped[TaskStatus] = mapped_column(Enum(TaskStatus), default=TaskStatus.todo, nullable=False) due_date: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) # v2 additive: priority (default medium). Present in DB, but hidden from v1 schema. priority: Mapped[TaskPriority] = mapped_column(Enum(TaskPriority), default=TaskPriority.medium, nullable=False) project_id: Mapped[int] = mapped_column(ForeignKey("projects.id"), nullable=False, index=True) assignee_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"), nullable=True) project = relationship("Project", back_populates="tasks") assignee = relationship("User", back_populates="tasks") class IdempotencyKey(Base): __tablename__ = "idempotency_keys" id: Mapped[int] = mapped_column(Integer, primary_key=True) user_id: Mapped[int | None] = mapped_column(Integer, nullable=True) # might be None for unauthenticated endpoint: Mapped[str] = mapped_column(String(255), nullable=False) key: Mapped[str] = mapped_column(String(255), nullable=False) request_hash: Mapped[str] = mapped_column(String(255), nullable=False) response_body: Mapped[str] = mapped_column(Text, nullable=False) status_code: Mapped[int] = mapped_column(Integer, nullable=False) created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow) __table_args__ = (UniqueConstraint("user_id", "endpoint", "key", name="uq_user_endpoint_key"),)