From d92bb8f006173e55cf1a4a59292167c8f2931ea2 Mon Sep 17 00:00:00 2001 From: Polina Date: Thu, 2 Jul 2026 17:07:47 +0000 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20=C2=AB?= =?UTF-8?q?app=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++ app/schemas.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 app/models.py create mode 100644 app/schemas.py diff --git a/app/models.py b/app/models.py new file mode 100644 index 0000000..3cfc5db --- /dev/null +++ b/app/models.py @@ -0,0 +1,47 @@ +from datetime import datetime + +from sqlalchemy import Boolean, DateTime, Float, ForeignKey, Integer, String, Text +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from app.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) + rating: Mapped[float] = mapped_column(Float, default=0.0) + points: Mapped[int] = mapped_column(Integer, default=0) + created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow) + + sessions: Mapped[list["Session"]] = relationship(back_populates="user", cascade="all, delete-orphan") + external_data: Mapped[list["ExternalData"]] = relationship( + back_populates="user", cascade="all, delete-orphan" + ) + + +class Session(Base): + __tablename__ = "sessions" + + id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) + user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False, index=True) + token: Mapped[str] = mapped_column(String(64), unique=True, index=True, nullable=False) + is_active: Mapped[bool] = mapped_column(Boolean, default=True) + created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow) + expires_at: Mapped[datetime] = mapped_column(DateTime, nullable=False) + + user: Mapped["User"] = relationship(back_populates="sessions") + + +class ExternalData(Base): + __tablename__ = "external_data" + + id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) + user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False, index=True) + source: Mapped[str] = mapped_column(String(128), nullable=False) + payload: Mapped[str] = mapped_column(Text, nullable=False) + created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow) + + user: Mapped["User"] = relationship(back_populates="external_data") diff --git a/app/schemas.py b/app/schemas.py new file mode 100644 index 0000000..0dc051c --- /dev/null +++ b/app/schemas.py @@ -0,0 +1,51 @@ +from datetime import datetime + +from pydantic import BaseModel, EmailStr, Field + + +class UserRegister(BaseModel): + email: EmailStr + password: str = Field(min_length=6, max_length=128) + + +class UserLogin(BaseModel): + email: EmailStr + password: str + + +class UserPublic(BaseModel): + id: int + email: str + rating: float + points: int + created_at: datetime + + class Config: + from_attributes = True + + +class SessionPublic(BaseModel): + id: int + created_at: datetime + expires_at: datetime + is_active: bool + + class Config: + from_attributes = True + + +class ExternalDataCreate(BaseModel): + source: str = Field(min_length=1, max_length=128) + payload: dict | str + points_delta: int = 0 + rating_delta: float = 0.0 + + +class ExternalDataPublic(BaseModel): + id: int + source: str + payload: str + created_at: datetime + + class Config: + from_attributes = True