/
Vibek
/
Agent_GG
Обзор
Документация
Войти
/
Vibek
/
Agent_GG
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
backend/src/db/models.py
96 строк
4 KB
Vladimir
P0 безопасность API, auth и прод-конфиг
26 май 2026, 19:02
26 май 2026, 19:02
ca07c54
Код
Авторство
О чём код?
from __future__ import annotations from decimal import Decimal from typing import Any from sqlalchemy import DECIMAL, ForeignKey, Index, Integer, String, TIMESTAMP, Text, func, text from sqlalchemy.dialects.postgresql import JSONB from sqlalchemy.orm import Mapped, mapped_column, relationship from .database import Base class User(Base): __tablename__ = "users" __table_args__ = ( Index("ix_users_email_unique", "email", unique=True), ) id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) email: Mapped[str] = mapped_column(String(320), nullable=False) password_hash: Mapped[str] = mapped_column(String(255), nullable=False) created_at: Mapped[Any] = mapped_column(TIMESTAMP, nullable=False, server_default=func.now()) updated_at: Mapped[Any] = mapped_column( TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now() ) class Listing(Base): __tablename__ = "listings" __table_args__ = ( Index("ix_listings_price", "price"), Index("ix_listings_operation_type", "operation_type"), Index( "ix_listings_title_fts", text("to_tsvector('russian', coalesce(title, ''))"), postgresql_using="gin", ), Index( "ix_listings_address_fts", text("to_tsvector('russian', coalesce(address, ''))"), postgresql_using="gin", ), Index( "ix_listings_full_description_fts", text("to_tsvector('russian', coalesce(full_description, ''))"), postgresql_using="gin", ), ) id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) title: Mapped[str] = mapped_column(String(500), nullable=False) link: Mapped[str] = mapped_column(String(1000), nullable=False, unique=True) address: Mapped[str | None] = mapped_column(String(500), nullable=True) full_description: Mapped[str | None] = mapped_column(Text, nullable=True) price: Mapped[Decimal | None] = mapped_column(DECIMAL(15, 2), nullable=True) operation_type: Mapped[str | None] = mapped_column(String(50), nullable=True) created_at: Mapped[Any] = mapped_column( TIMESTAMP, nullable=False, server_default=func.now() ) updated_at: Mapped[Any] = mapped_column( TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now() ) source: Mapped[str] = mapped_column( String(100), nullable=False, server_default="avito" ) params: Mapped[list["ListingParams"]] = relationship( "ListingParams", back_populates="listing", cascade="all, delete-orphan" ) class ListingParams(Base): __tablename__ = "listing_params" __table_args__ = ( Index("ix_listing_params_params_extra_gin", "params_extra", postgresql_using="gin"), ) id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) listing_id: Mapped[int] = mapped_column( ForeignKey("listings.id", ondelete="CASCADE"), nullable=False ) area_total: Mapped[Decimal | None] = mapped_column(DECIMAL(10, 2), nullable=True) area_rent: Mapped[Decimal | None] = mapped_column(DECIMAL(10, 2), nullable=True) floor: Mapped[int | None] = mapped_column(Integer, nullable=True) floors_total: Mapped[int | None] = mapped_column(Integer, nullable=True) district: Mapped[str | None] = mapped_column(String(120), nullable=True) price_total: Mapped[Decimal | None] = mapped_column(DECIMAL(15, 2), nullable=True) price_per_sqm: Mapped[Decimal | None] = mapped_column(DECIMAL(10, 2), nullable=True) power_kw: Mapped[Decimal | None] = mapped_column(DECIMAL(8, 2), nullable=True) ceiling_height: Mapped[Decimal | None] = mapped_column(DECIMAL(5, 2), nullable=True) params_extra: Mapped[dict[str, Any] | None] = mapped_column(JSONB, nullable=True) created_at: Mapped[Any] = mapped_column( TIMESTAMP, nullable=False, server_default=func.now() ) listing: Mapped["Listing"] = relationship("Listing", back_populates="params")