新增管理帖子的组件
Change-Id: I7299ad6f735064dd112f92bab7c87daa952e6dc2
diff --git a/WZY/xhs_server/__pycache__/config.cpython-312.pyc b/WZY/xhs_server/__pycache__/config.cpython-312.pyc
index 4e1a3be..262103a 100644
--- a/WZY/xhs_server/__pycache__/config.cpython-312.pyc
+++ b/WZY/xhs_server/__pycache__/config.cpython-312.pyc
Binary files differ
diff --git a/WZY/xhs_server/__pycache__/extensions.cpython-312.pyc b/WZY/xhs_server/__pycache__/extensions.cpython-312.pyc
index 27ba8c2..0743449 100644
--- a/WZY/xhs_server/__pycache__/extensions.cpython-312.pyc
+++ b/WZY/xhs_server/__pycache__/extensions.cpython-312.pyc
Binary files differ
diff --git a/WZY/xhs_server/models/__pycache__/__init__.cpython-312.pyc b/WZY/xhs_server/models/__pycache__/__init__.cpython-312.pyc
index fe53b2a..6b72b82 100644
--- a/WZY/xhs_server/models/__pycache__/__init__.cpython-312.pyc
+++ b/WZY/xhs_server/models/__pycache__/__init__.cpython-312.pyc
Binary files differ
diff --git a/WZY/xhs_server/models/__pycache__/behavior.cpython-312.pyc b/WZY/xhs_server/models/__pycache__/behavior.cpython-312.pyc
index 71f3757..d8b7d89 100644
--- a/WZY/xhs_server/models/__pycache__/behavior.cpython-312.pyc
+++ b/WZY/xhs_server/models/__pycache__/behavior.cpython-312.pyc
Binary files differ
diff --git a/WZY/xhs_server/models/__pycache__/comment.cpython-312.pyc b/WZY/xhs_server/models/__pycache__/comment.cpython-312.pyc
index 393c6d3..c67a633 100644
--- a/WZY/xhs_server/models/__pycache__/comment.cpython-312.pyc
+++ b/WZY/xhs_server/models/__pycache__/comment.cpython-312.pyc
Binary files differ
diff --git a/WZY/xhs_server/models/__pycache__/post.cpython-312.pyc b/WZY/xhs_server/models/__pycache__/post.cpython-312.pyc
index 1d64737..ce4a560 100644
--- a/WZY/xhs_server/models/__pycache__/post.cpython-312.pyc
+++ b/WZY/xhs_server/models/__pycache__/post.cpython-312.pyc
Binary files differ
diff --git a/WZY/xhs_server/models/__pycache__/tag.cpython-312.pyc b/WZY/xhs_server/models/__pycache__/tag.cpython-312.pyc
index d76c0e0..2c109f9 100644
--- a/WZY/xhs_server/models/__pycache__/tag.cpython-312.pyc
+++ b/WZY/xhs_server/models/__pycache__/tag.cpython-312.pyc
Binary files differ
diff --git a/WZY/xhs_server/models/__pycache__/topic.cpython-312.pyc b/WZY/xhs_server/models/__pycache__/topic.cpython-312.pyc
index a779595..053f4bd 100644
--- a/WZY/xhs_server/models/__pycache__/topic.cpython-312.pyc
+++ b/WZY/xhs_server/models/__pycache__/topic.cpython-312.pyc
Binary files differ
diff --git a/WZY/xhs_server/models/__pycache__/user.cpython-312.pyc b/WZY/xhs_server/models/__pycache__/user.cpython-312.pyc
index e3841c7..1d18d94 100644
--- a/WZY/xhs_server/models/__pycache__/user.cpython-312.pyc
+++ b/WZY/xhs_server/models/__pycache__/user.cpython-312.pyc
Binary files differ
diff --git a/WZY/xhs_server/routes/__pycache__/__init__.cpython-312.pyc b/WZY/xhs_server/routes/__pycache__/__init__.cpython-312.pyc
index acde7ed..e463ed3 100644
--- a/WZY/xhs_server/routes/__pycache__/__init__.cpython-312.pyc
+++ b/WZY/xhs_server/routes/__pycache__/__init__.cpython-312.pyc
Binary files differ
diff --git a/WZY/xhs_server/routes/__pycache__/comments.cpython-312.pyc b/WZY/xhs_server/routes/__pycache__/comments.cpython-312.pyc
index 4bd83ee..94555ac 100644
--- a/WZY/xhs_server/routes/__pycache__/comments.cpython-312.pyc
+++ b/WZY/xhs_server/routes/__pycache__/comments.cpython-312.pyc
Binary files differ
diff --git a/WZY/xhs_server/routes/__pycache__/posts.cpython-312.pyc b/WZY/xhs_server/routes/__pycache__/posts.cpython-312.pyc
index a957747..ac244d0 100644
--- a/WZY/xhs_server/routes/__pycache__/posts.cpython-312.pyc
+++ b/WZY/xhs_server/routes/__pycache__/posts.cpython-312.pyc
Binary files differ
diff --git a/WZY/xhs_server/routes/posts.py b/WZY/xhs_server/routes/posts.py
index e01bdd8..11c0b9e 100644
--- a/WZY/xhs_server/routes/posts.py
+++ b/WZY/xhs_server/routes/posts.py
@@ -1,4 +1,5 @@
# routes/posts.py
+
from flask import Blueprint, request, jsonify, abort
from extensions import db
from models.post import Post
@@ -16,11 +17,24 @@
@posts_bp.route('', methods=['GET'])
def list_posts():
- posts = Post.query.filter_by(status='published').all()
+ """
+ 获取帖子列表,支持:
+ - GET /posts 返回所有已发布帖子
+ - GET /posts?user_id=123 返回指定用户 user_id 的所有帖子
+ """
+ user_id = request.args.get('user_id', type=int)
+ query = Post.query
+ if user_id is not None:
+ query = query.filter_by(user_id=user_id)
+ else:
+ query = query.filter_by(status='published')
+ posts = query.all()
+
return jsonify([{
'id': p.id,
'title': p.title,
'heat': p.heat,
+ 'status': p.status,
'created_at': p.created_at.isoformat()
} for p in posts])
@@ -30,6 +44,7 @@
return jsonify({
'id': post.id,
'user_id': post.user_id,
+ 'topic_id': post.topic_id,
'title': post.title,
'content': post.content,
'media_urls': post.media_urls,
@@ -42,20 +57,11 @@
@posts_bp.route('/<int:post_id>', methods=['PUT'])
def update_post(post_id):
"""
- 修改帖子
- URL 参数:
- post_id - 要修改的帖子 ID
- JSON Body 可选字段:
- title (string)
- content (string)
- topic_id (int) — 必须是 topics 表中已有的 ID
- media_urls (list) — 字符串数组
- status (string) — 'draft','pending','published','deleted','rejected'
+ 修改帖子字段(可选字段:title, content, topic_id, media_urls, status)
"""
post = Post.query.get_or_404(post_id)
data = request.get_json() or {}
- # 只更新客户端传来的字段
- for key in ('title','content','topic_id','media_urls','status'):
+ for key in ('title', 'content', 'topic_id', 'media_urls', 'status'):
if key in data:
setattr(post, key, data[key])
db.session.commit()
@@ -68,7 +74,6 @@
db.session.commit()
return '', 204
-
@posts_bp.route('/<int:post_id>/<action>', methods=['POST'])
def post_action(post_id, action):
"""
@@ -104,8 +109,6 @@
db.session.commit()
return '', 201
-
-# 取消点赞
@posts_bp.route('/<int:post_id>/like', methods=['DELETE'])
def unlike(post_id):
user_id = request.get_json(silent=True) and request.get_json().get('user_id')
@@ -127,7 +130,6 @@
db.session.commit()
return '', 204
-# 取消收藏
@posts_bp.route('/<int:post_id>/favorite', methods=['DELETE'])
def unfavorite(post_id):
user_id = request.get_json(silent=True) and request.get_json().get('user_id')