Files
DjangoAdmin_Flask_EleVue/application/models/article.py
T
DjangoAdmin DevandClaude 658ee6d596 refactor: P2 core+common 建设(应用工厂/扩展/工具层迁入 src/)
【结构】
- src/core/:database/response/jwt/password/security/redis_client/mail/scheduler
- src/common/:base_model/base_db/forms(base_form+fields)/constants/utils
- src/models_registry.py:模型聚合器(Flask-Migrate 全量表识别)

【绞杀者 shim】extensions/、utils/、application/constants|forms|models 打兼容转发
【兼容修复】应用工厂抽取至 application/factory.py(P2 前置,P1 已完成)

【基线修复】application/models(25) + middleware(7) 原被嵌套 .gitignore(*)
误排除出版本库,已补入(base_model/base_db 现为 shim,原版在 src/common/)

【登记既有缺陷】
1. flask db CLI 因 .env FLASK_VERSION 与 --version 冲突报错 → Makefile 已加 FLASK_SKIP_DOTENV=1
2. 本机 Redis 未设密码但 REDIS_AUTH=True,/captcha 报 AUTH 错误(环境问题)

【验收】22 模型表全量识别;src/ flake8 零告警;pytest 5 passed;应用启动;
/login 表单校验、/user/page 鉴权拦截冒烟正常

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-07 13:20:46 +08:00

187 lines
4.7 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.
"""
文章模型模块
=============================================================
提供文章管理的数据模型定义,用于管理文章内容。
"""
from application.models.base_db import BaseDB
from application.models.base_model import BaseModel
from config import DB_PREFIX
from extensions import db
from utils.case_converter import convert_camel_to_snake
# ============================================================
# 文章模型
# ============================================================
class Article(BaseModel, BaseDB):
"""
文章模型
用于存储文章信息,包括:分类、标题、封面、图集、导读、内容、作者、状态等。
Attributes:
id: 主键ID(继承自 BaseModel)
category_id: 分类ID(Integer,可选)
title: 文章标题(String(150),可选)
cover: 文章封面(String(255),可选)
images: 文章图集(Text,可选,JSON格式存储多张图片)
intro: 文章导读(String(255),可选)
content: 文章内容(Text,可选,富文本内容)
author: 文章作者(String(100),可选)
status: 文章状态(Integer,0-正常,1-下架,默认0)
click: 文章点击率(Integer,默认0)
sort: 文章排序(Integer,默认0)
"""
# ============================================================
# 表配置
# ============================================================
# 设置表名
__tablename__ = DB_PREFIX + "article"
# ============================================================
# 字段定义
# ============================================================
# 分类ID
category_id = db.Column(
db.Integer,
nullable=True,
comment="分类ID"
)
# 文章标题
title = db.Column(
db.String(150),
nullable=True,
comment="文章标题"
)
# 文章封面
cover = db.Column(
db.String(255),
nullable=True,
comment="文章封面"
)
# 文章图集(JSON格式存储多张图片)
images = db.Column(
db.Text,
nullable=True,
comment="文章图集"
)
# 文章导读
intro = db.Column(
db.String(255),
nullable=True,
comment="文章导读"
)
# 文章内容
content = db.Column(
db.Text,
nullable=True,
comment="文章内容"
)
# 文章作者
author = db.Column(
db.String(100),
nullable=True,
comment="文章作者"
)
# 文章状态:0-正常 1-下架
status = db.Column(
db.Integer,
nullable=False,
default=0,
comment="文章状态:0-正常 1-下架"
)
# 文章点击率
click = db.Column(
db.Integer,
nullable=False,
default=0,
comment="文章点击率"
)
# 文章排序
sort = db.Column(
db.Integer,
nullable=False,
default=0,
comment="文章排序"
)
# ============================================================
# 常量定义
# ============================================================
class Status:
"""文章状态常量"""
NORMAL = 0 # 正常
OFFLINE = 1 # 下架
# ============================================================
# 业务方法
# ============================================================
def increment_click(self):
"""
增加点击率
每次调用点击量加1
"""
self.click = (self.click or 0) + 1
self.save()
def get_images_list(self):
"""
获取图集列表
Returns:
list: 图集URL列表
"""
if not self.images:
return []
return [img.strip() for img in self.images.split(',') if img.strip()]
def get_cover_url(self):
"""
获取封面URL
Returns:
str: 封面URL
"""
from utils.rich_text import get_file_url
return get_file_url(self.cover) if self.cover else ""
# ============================================================
# 魔法方法
# ============================================================
def __repr__(self):
"""
返回模型的字符串表示
Returns:
str: 格式为 "文章:{title}" 的字符串
"""
return '文章:{}'.format(self.title or '未命名')
def __str__(self):
"""
返回模型的字符串表示
Returns:
str: 格式为 "文章:{title}" 的字符串
"""
return self.__repr__()