Files
DinQuant/backend_api_python/app/routes/community.py
T

425 lines
14 KiB
Python
Raw Normal View History

2026-01-31 02:59:49 +08:00
"""
Community APIs - indicator community interface
2026-01-31 02:59:49 +08:00
REST API that provides indicator markets, buying, commenting, and more.
2026-01-31 02:59:49 +08:00
"""
from flask import Blueprint, jsonify, request, g
from app.utils.auth import login_required
from app.utils.logger import get_logger
from app.services.community_service import get_community_service
logger = get_logger(__name__)
community_bp = Blueprint("community", __name__)
# ==========================================
# indicator market
2026-01-31 02:59:49 +08:00
# ==========================================
@community_bp.route("/indicators", methods=["GET"])
@login_required
def get_market_indicators():
"""
Get a list of market indicators
2026-01-31 02:59:49 +08:00
Query params:
page: page number (default 1)
page_size: Number of pages per page (default 12)
keyword: search keyword
pricing_type: 'free' / 'paid' / empty (all)
2026-01-31 02:59:49 +08:00
sort_by: 'newest' / 'hot' / 'price_asc' / 'price_desc' / 'rating'
"""
try:
page = int(request.args.get('page', 1))
page_size = int(request.args.get('page_size', 12))
keyword = request.args.get('keyword', '').strip()
pricing_type = request.args.get('pricing_type', '').strip() or None
sort_by = request.args.get('sort_by', 'newest').strip()
# Limit the number of pages per page
2026-01-31 02:59:49 +08:00
page_size = min(max(page_size, 1), 50)
service = get_community_service()
result = service.get_market_indicators(
page=page,
page_size=page_size,
keyword=keyword if keyword else None,
pricing_type=pricing_type,
sort_by=sort_by,
user_id=g.user_id
)
return jsonify({'code': 1, 'msg': 'success', 'data': result})
except Exception as e:
logger.error(f"get_market_indicators failed: {e}")
return jsonify({'code': 0, 'msg': str(e), 'data': None}), 500
@community_bp.route("/indicators/<int:indicator_id>", methods=["GET"])
@login_required
def get_indicator_detail(indicator_id: int):
"""Get indicator details"""
2026-01-31 02:59:49 +08:00
try:
service = get_community_service()
result = service.get_indicator_detail(indicator_id, user_id=g.user_id)
if not result:
return jsonify({'code': 0, 'msg': 'indicator_not_found', 'data': None}), 404
return jsonify({'code': 1, 'msg': 'success', 'data': result})
except Exception as e:
logger.error(f"get_indicator_detail failed: {e}")
return jsonify({'code': 0, 'msg': str(e), 'data': None}), 500
# ==========================================
# Purchase function
2026-01-31 02:59:49 +08:00
# ==========================================
@community_bp.route("/indicators/<int:indicator_id>/purchase", methods=["POST"])
@login_required
def purchase_indicator(indicator_id: int):
"""
buy indicator
2026-01-31 02:59:49 +08:00
will automatically:
1. Check whether the points are sufficient
2. Deduct buyer points and increase seller points
3. Create purchase records
4. Copy the indicator to the buyers account
2026-01-31 02:59:49 +08:00
"""
try:
service = get_community_service()
success, message, data = service.purchase_indicator(
buyer_id=g.user_id,
indicator_id=indicator_id
)
if success:
return jsonify({'code': 1, 'msg': message, 'data': data})
else:
return jsonify({'code': 0, 'msg': message, 'data': data}), 400
except Exception as e:
logger.error(f"purchase_indicator failed: {e}")
return jsonify({'code': 0, 'msg': str(e), 'data': None}), 500
@community_bp.route("/my-purchases", methods=["GET"])
@login_required
def get_my_purchases():
"""Get a list of indicators I purchased"""
2026-01-31 02:59:49 +08:00
try:
page = int(request.args.get('page', 1))
page_size = int(request.args.get('page_size', 20))
page_size = min(max(page_size, 1), 50)
service = get_community_service()
result = service.get_my_purchases(
user_id=g.user_id,
page=page,
page_size=page_size
)
return jsonify({'code': 1, 'msg': 'success', 'data': result})
except Exception as e:
logger.error(f"get_my_purchases failed: {e}")
return jsonify({'code': 0, 'msg': str(e), 'data': None}), 500
# ==========================================
# Comment function
2026-01-31 02:59:49 +08:00
# ==========================================
@community_bp.route("/indicators/<int:indicator_id>/comments", methods=["GET"])
@login_required
def get_comments(indicator_id: int):
"""Get a list of indicator comments"""
2026-01-31 02:59:49 +08:00
try:
page = int(request.args.get('page', 1))
page_size = int(request.args.get('page_size', 20))
page_size = min(max(page_size, 1), 50)
service = get_community_service()
result = service.get_comments(
indicator_id=indicator_id,
page=page,
page_size=page_size
)
return jsonify({'code': 1, 'msg': 'success', 'data': result})
except Exception as e:
logger.error(f"get_comments failed: {e}")
return jsonify({'code': 0, 'msg': str(e), 'data': None}), 500
@community_bp.route("/indicators/<int:indicator_id>/comments", methods=["POST"])
@login_required
def add_comment(indicator_id: int):
"""
Add comment
2026-01-31 02:59:49 +08:00
Request body:
rating: 1-5 star rating
content: Comment content (optional, up to 500 words)
2026-01-31 02:59:49 +08:00
Note: Only users who have purchased can comment, and they can only comment once
2026-01-31 02:59:49 +08:00
"""
try:
data = request.get_json() or {}
rating = int(data.get('rating', 5))
content = (data.get('content') or '').strip()
service = get_community_service()
success, message, result = service.add_comment(
user_id=g.user_id,
indicator_id=indicator_id,
rating=rating,
content=content
)
if success:
return jsonify({'code': 1, 'msg': message, 'data': result})
else:
return jsonify({'code': 0, 'msg': message, 'data': result}), 400
except Exception as e:
logger.error(f"add_comment failed: {e}")
return jsonify({'code': 0, 'msg': str(e), 'data': None}), 500
@community_bp.route("/indicators/<int:indicator_id>/comments/<int:comment_id>", methods=["PUT"])
@login_required
def update_comment(indicator_id: int, comment_id: int):
"""
Update comments (you can only modify your own comments)
2026-01-31 02:59:49 +08:00
Request body:
rating: 1-5 star rating
content: Comment content (up to 500 words)
2026-01-31 02:59:49 +08:00
"""
try:
data = request.get_json() or {}
rating = int(data.get('rating', 5))
content = (data.get('content') or '').strip()
service = get_community_service()
success, message, result = service.update_comment(
user_id=g.user_id,
comment_id=comment_id,
indicator_id=indicator_id,
rating=rating,
content=content
)
if success:
return jsonify({'code': 1, 'msg': message, 'data': result})
else:
return jsonify({'code': 0, 'msg': message, 'data': result}), 400
except Exception as e:
logger.error(f"update_comment failed: {e}")
return jsonify({'code': 0, 'msg': str(e), 'data': None}), 500
@community_bp.route("/indicators/<int:indicator_id>/my-comment", methods=["GET"])
@login_required
def get_my_comment(indicator_id: int):
"""Get the current user's comments on the specified indicator (for editing)"""
2026-01-31 02:59:49 +08:00
try:
service = get_community_service()
result = service.get_user_comment(
user_id=g.user_id,
indicator_id=indicator_id
)
return jsonify({'code': 1, 'msg': 'success', 'data': result})
except Exception as e:
logger.error(f"get_my_comment failed: {e}")
return jsonify({'code': 0, 'msg': str(e), 'data': None}), 500
# ==========================================
# Real offer performance
2026-01-31 02:59:49 +08:00
# ==========================================
@community_bp.route("/indicators/<int:indicator_id>/performance", methods=["GET"])
@login_required
def get_indicator_performance(indicator_id: int):
"""Get real performance statistics of indicators"""
2026-01-31 02:59:49 +08:00
try:
service = get_community_service()
result = service.get_indicator_performance(indicator_id)
return jsonify({'code': 1, 'msg': 'success', 'data': result})
except Exception as e:
logger.error(f"get_indicator_performance failed: {e}")
return jsonify({'code': 0, 'msg': str(e), 'data': None}), 500
# ==========================================
# Administrator review function
2026-01-31 02:59:49 +08:00
# ==========================================
def _is_admin():
"""Check if the current user is an administrator"""
2026-01-31 02:59:49 +08:00
role = getattr(g, 'user_role', None)
return role == 'admin'
@community_bp.route("/admin/pending-indicators", methods=["GET"])
@login_required
def get_pending_indicators():
"""
Get the list of indicators to be reviewed (for administrators only)
2026-01-31 02:59:49 +08:00
Query params:
page: page number (default 1)
page_size: Number of pages per page (default 20)
2026-01-31 02:59:49 +08:00
review_status: 'pending' / 'approved' / 'rejected' / 'all'
"""
try:
if not _is_admin():
return jsonify({'code': 0, 'msg': 'admin_required', 'data': None}), 403
page = int(request.args.get('page', 1))
page_size = int(request.args.get('page_size', 20))
review_status = request.args.get('review_status', 'pending').strip() or 'pending'
page_size = min(max(page_size, 1), 100)
service = get_community_service()
result = service.get_pending_indicators(
page=page,
page_size=page_size,
review_status=review_status
)
return jsonify({'code': 1, 'msg': 'success', 'data': result})
except Exception as e:
logger.error(f"get_pending_indicators failed: {e}")
return jsonify({'code': 0, 'msg': str(e), 'data': None}), 500
@community_bp.route("/admin/review-stats", methods=["GET"])
@login_required
def get_review_stats():
"""Get audit statistics (for administrators only)"""
2026-01-31 02:59:49 +08:00
try:
if not _is_admin():
return jsonify({'code': 0, 'msg': 'admin_required', 'data': None}), 403
service = get_community_service()
result = service.get_review_stats()
return jsonify({'code': 1, 'msg': 'success', 'data': result})
except Exception as e:
logger.error(f"get_review_stats failed: {e}")
return jsonify({'code': 0, 'msg': str(e), 'data': None}), 500
@community_bp.route("/admin/indicators/<int:indicator_id>/review", methods=["POST"])
@login_required
def review_indicator(indicator_id: int):
"""
Audit indicators (for administrators only)
2026-01-31 02:59:49 +08:00
Request body:
action: 'approve' / 'reject'
note: review notes (optional)
2026-01-31 02:59:49 +08:00
"""
try:
if not _is_admin():
return jsonify({'code': 0, 'msg': 'admin_required', 'data': None}), 403
data = request.get_json() or {}
action = data.get('action', '').strip()
note = data.get('note', '').strip()
if action not in ('approve', 'reject'):
return jsonify({'code': 0, 'msg': 'invalid_action', 'data': None}), 400
service = get_community_service()
success, message = service.review_indicator(
admin_id=g.user_id,
indicator_id=indicator_id,
action=action,
note=note
)
if success:
return jsonify({'code': 1, 'msg': message, 'data': None})
else:
return jsonify({'code': 0, 'msg': message, 'data': None}), 400
except Exception as e:
logger.error(f"review_indicator failed: {e}")
return jsonify({'code': 0, 'msg': str(e), 'data': None}), 500
@community_bp.route("/admin/indicators/<int:indicator_id>/unpublish", methods=["POST"])
@login_required
def unpublish_indicator(indicator_id: int):
"""
Delisting indicator (for administrators only)
2026-01-31 02:59:49 +08:00
Request body:
note: Reason for delisting (optional)
2026-01-31 02:59:49 +08:00
"""
try:
if not _is_admin():
return jsonify({'code': 0, 'msg': 'admin_required', 'data': None}), 403
data = request.get_json() or {}
note = data.get('note', '').strip()
service = get_community_service()
success, message = service.unpublish_indicator(
admin_id=g.user_id,
indicator_id=indicator_id,
note=note
)
if success:
return jsonify({'code': 1, 'msg': message, 'data': None})
else:
return jsonify({'code': 0, 'msg': message, 'data': None}), 400
except Exception as e:
logger.error(f"unpublish_indicator failed: {e}")
return jsonify({'code': 0, 'msg': str(e), 'data': None}), 500
@community_bp.route("/admin/indicators/<int:indicator_id>", methods=["DELETE"])
@login_required
def admin_delete_indicator(indicator_id: int):
"""Delete indicator (only for administrators)"""
2026-01-31 02:59:49 +08:00
try:
if not _is_admin():
return jsonify({'code': 0, 'msg': 'admin_required', 'data': None}), 403
service = get_community_service()
success, message = service.admin_delete_indicator(
admin_id=g.user_id,
indicator_id=indicator_id
)
if success:
return jsonify({'code': 1, 'msg': message, 'data': None})
else:
return jsonify({'code': 0, 'msg': message, 'data': None}), 400
except Exception as e:
logger.error(f"admin_delete_indicator failed: {e}")
return jsonify({'code': 0, 'msg': str(e), 'data': None}), 500