Files
news_all_spo/api/bd/models.py
jze9 615c819e97 fix: корректные source_url для VK постов (screen_name формат)
- VkSource: добавлено поле screen_name, заполняется из VK API при добавлении источника
- vk_parser: source_url теперь в формате vk.com/{screen_name}?w=wall{owner}_{post}
  вместо устаревшего vk.com/wall{owner}_{post}
- _vk_post_exists: проверка через LIKE чтобы находить оба формата URL
- admin_vk: автоматически получает screen_name при создании/обновлении источника
- admin_system: исправлен regex в repair-media-urls — только /news-media/ URL
- Alembic миграция: b2c3d4e5f6a7 добавляет колонку screen_name в vk_sources

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-20 19:32:28 +05:00

90 lines
4.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import uuid
from datetime import datetime
from sqlalchemy import String, Text, DateTime, ForeignKey, Table, Column, Integer, Boolean, Enum as SAEnum
from sqlalchemy.orm import mapped_column, Mapped, relationship
from sqlalchemy.dialects.postgresql import UUID
import enum
from .database import Base
class ArticleStatus(str, enum.Enum):
draft = "draft"
published = "published"
article_tag = Table(
"article_tag",
Base.metadata,
Column("article_id", UUID(as_uuid=True), ForeignKey("articles.id", ondelete="CASCADE")),
Column("tag_id", UUID(as_uuid=True), ForeignKey("tags.id", ondelete="CASCADE")),
)
class Category(Base):
__tablename__ = "categories"
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
name: Mapped[str] = mapped_column(String(100), unique=True)
slug: Mapped[str] = mapped_column(String(100), unique=True, index=True)
articles: Mapped[list["Article"]] = relationship(back_populates="category", lazy="noload")
class Tag(Base):
__tablename__ = "tags"
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
name: Mapped[str] = mapped_column(String(100), unique=True)
slug: Mapped[str] = mapped_column(String(100), unique=True, index=True)
class Article(Base):
__tablename__ = "articles"
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
title: Mapped[str] = mapped_column(String(500))
slug: Mapped[str] = mapped_column(String(500), unique=True, index=True)
content: Mapped[str] = mapped_column(Text, default="")
excerpt: Mapped[str] = mapped_column(String(1000), default="")
cover_url: Mapped[str | None] = mapped_column(String(1000), nullable=True)
font_family: Mapped[str] = mapped_column(String(100), default="Merriweather")
source_url: Mapped[str | None] = mapped_column(String(1000), nullable=True)
status: Mapped[ArticleStatus] = mapped_column(SAEnum(ArticleStatus), default=ArticleStatus.draft)
view_count: Mapped[int] = mapped_column(Integer, default=0)
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
published_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
category_id: Mapped[uuid.UUID | None] = mapped_column(
UUID(as_uuid=True), ForeignKey("categories.id", ondelete="SET NULL"), nullable=True
)
category: Mapped["Category | None"] = relationship(back_populates="articles", lazy="selectin")
tags: Mapped[list["Tag"]] = relationship(secondary=article_tag, lazy="selectin")
class Admin(Base):
__tablename__ = "admins"
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
username: Mapped[str] = mapped_column(String(100), unique=True)
password_hash: Mapped[str] = mapped_column(String(300))
class Media(Base):
__tablename__ = "media"
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
filename: Mapped[str] = mapped_column(String(500))
url: Mapped[str] = mapped_column(String(1000))
media_type: Mapped[str] = mapped_column(String(20)) # image | video
size_bytes: Mapped[int] = mapped_column(Integer, default=0)
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
article_id: Mapped[uuid.UUID | None] = mapped_column(
UUID(as_uuid=True), ForeignKey("articles.id", ondelete="SET NULL"), nullable=True
)
class VkSource(Base):
__tablename__ = "vk_sources"
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
group_id: Mapped[str] = mapped_column(String(50), unique=True) # числовой ID без минуса
group_name: Mapped[str] = mapped_column(String(200)) # станет названием категории
screen_name: Mapped[str | None] = mapped_column(String(200), nullable=True) # screen_name для корректных URL
enabled: Mapped[bool] = mapped_column(Boolean, default=True)
strict_filter: Mapped[bool] = mapped_column(Boolean, default=True) # True = только посты с тегами
last_run: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)