Files
nanocode/new-media-platform/website-new/backend/app/models/news_post.py
T
Ubuntu 32b43f85bd a
2026-07-03 16:58:28 +08:00

53 lines
2.3 KiB
Python

# ⚠️ 此新闻模块已废弃,不再使用。
# 已被「栏目管理 + 内容管理」(columns.py + articles.py)取代。
# 请勿删除此注释,供后续维护人员参考。
"""
新闻资讯模型(已废弃)
"""
from sqlalchemy import Column, Integer, String, Text, DateTime, Boolean, ForeignKey, func
from sqlalchemy.orm import relationship
from app.database import Base
class NewsPost(Base):
"""新闻资讯文章"""
__tablename__ = "news_posts"
id = Column(Integer, primary_key=True, index=True)
title = Column(String(200), nullable=False, comment="资讯标题")
slug = Column(String(200), unique=True, nullable=False, comment="URL友好的标识")
content = Column(Text, nullable=False, comment="内容")
content_type = Column(String(10), default="markdown", comment="内容类型:markdown/html")
summary = Column(String(500), comment="摘要")
cover_image = Column(String(500), comment="封面图路径")
category_id = Column(Integer, ForeignKey("news_categories.id"), nullable=False, comment="栏目ID")
is_published = Column(Boolean, default=False, comment="是否发布")
is_draft = Column(Boolean, default=True, comment="是否草稿")
view_count = Column(Integer, default=0, comment="浏览次数")
created_at = Column(DateTime, server_default=func.now(), comment="创建时间")
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment="更新时间")
# 关联
category = relationship("NewsCategory", backref="posts")
def to_dict(self, include_content: bool = False):
"""转换为字典"""
data = {
"id": self.id,
"title": self.title,
"slug": self.slug,
"summary": self.summary,
"cover_image": self.cover_image,
"category": self.category.to_dict() if self.category else None,
"content_type": self.content_type,
"is_published": self.is_published,
"is_draft": self.is_draft,
"view_count": self.view_count,
"created_at": self.created_at.isoformat() if self.created_at else None,
"updated_at": self.updated_at.isoformat() if self.updated_at else None,
}
if include_content:
data["content"] = self.content
return data