Add report flow and post_info by id
This commit is contained in:
70
back/main.py
70
back/main.py
@@ -67,6 +67,16 @@ class Comment(db.Model):
|
||||
created_at = db.Column(db.DateTime, default=lambda: datetime.now())
|
||||
parent_comment_id = db.Column(db.Integer, db.ForeignKey('comments.id'), nullable=True)
|
||||
|
||||
class Report(db.Model):
|
||||
__tablename__ = 'reports'
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||
submission_id = db.Column(db.Integer, nullable=False)
|
||||
title = db.Column(db.String(200), nullable=False)
|
||||
content = db.Column(db.Text, nullable=False)
|
||||
identity_token = db.Column(db.String(36), nullable=True)
|
||||
status = db.Column(db.String(20), default='Pending')
|
||||
created_at = db.Column(db.DateTime, default=lambda: datetime.now())
|
||||
|
||||
class Hashtag(db.Model):
|
||||
__tablename__ = 'hashtags'
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||
@@ -329,6 +339,42 @@ def submit_comment():
|
||||
except Exception as e:
|
||||
return jsonify({"code": 2003, "data": f"评论失败: {str(e)}"})
|
||||
|
||||
@app.route('/api/report', methods=['POST'])
|
||||
def submit_report():
|
||||
try:
|
||||
data = request.get_json()
|
||||
if not data or 'id' not in data or 'title' not in data or 'content' not in data:
|
||||
return jsonify({"code": 2000, "data": "参数错误"})
|
||||
|
||||
submission_id = data.get('id')
|
||||
title = data.get('title')
|
||||
content = data.get('content')
|
||||
|
||||
submission = db.session.get(Submission, submission_id)
|
||||
if not submission:
|
||||
return jsonify({"code": 2002, "data": "投稿不存在"})
|
||||
|
||||
identity_token = data.get('identity')
|
||||
if identity_token:
|
||||
if not Identity.query.filter_by(token=identity_token).first():
|
||||
return jsonify({"code": 2004, "data": "无效的Identity Token"})
|
||||
else:
|
||||
identity_token = None
|
||||
|
||||
report = Report(
|
||||
submission_id=submission_id,
|
||||
title=str(title).strip() if title is not None else '',
|
||||
content=str(content).strip() if content is not None else '',
|
||||
identity_token=identity_token,
|
||||
status='Pending',
|
||||
)
|
||||
db.session.add(report)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({"code": 1001, "data": {"id": report.id}})
|
||||
except Exception as e:
|
||||
return jsonify({"code": 2003, "data": f"投诉失败: {str(e)}"})
|
||||
|
||||
@app.route('/api/get/comment', methods=['GET'])
|
||||
def get_comments():
|
||||
try:
|
||||
@@ -511,6 +557,30 @@ def get_posts_info():
|
||||
})
|
||||
except Exception as e:
|
||||
return jsonify({"code": 2003, "data": str(e)})
|
||||
|
||||
@app.route('/api/post_info', methods=['GET'])
|
||||
def get_post_info():
|
||||
try:
|
||||
post_id = request.args.get("id", type=int)
|
||||
if not post_id:
|
||||
return jsonify({"code": 2000, "data": "参数错误"})
|
||||
|
||||
submission = Submission.query.filter_by(id=post_id, status='Pass').first()
|
||||
if not submission:
|
||||
return jsonify({"code": 2002, "data": "投稿不存在"})
|
||||
|
||||
data = {
|
||||
"id": submission.id,
|
||||
"content": submission.content,
|
||||
"upvotes": submission.upvotes,
|
||||
"downvotes": submission.downvotes,
|
||||
"created_at": submission.created_at.isoformat() if submission.created_at else None,
|
||||
"comment_count": len(submission.comments),
|
||||
}
|
||||
|
||||
return jsonify({"code": 1000, "data": data})
|
||||
except Exception as e:
|
||||
return jsonify({"code": 2003, "data": str(e)})
|
||||
|
||||
# --- 彩蛋 ---
|
||||
@app.route('/api/teapot', methods=['GET'])
|
||||
|
||||
Reference in New Issue
Block a user