Spaces:
Runtime error
Runtime error
Download routes/history.py from dev2004v/content-detection-backend: direct link, hf CLI and curl.
- Browser
- Download file 2.28 kB
-
https://huggingface.co/spaces/dev2004v/content-detection-backend/resolve/main/routes/history.py
- Command line
-
hf download hf://spaces/dev2004v/content-detection-backend/routes/history.py
-
curl -L -o history.py https://huggingface.co/spaces/dev2004v/content-detection-backend/resolve/main/routes/history.py
2.28 kB
| from flask import Blueprint, request, jsonify | |
| from utils.jwt_helper import decode_jwt | |
| from models.history import History | |
| history_bp = Blueprint('history', __name__) | |
| def get_history(): | |
| try: | |
| # JWT Authentication | |
| auth_header = request.headers.get('Authorization') | |
| if not auth_header or not auth_header.startswith('Bearer '): | |
| return jsonify({"error": "Missing or invalid Authorization header"}), 401 | |
| token = auth_header.split(" ")[1] | |
| try: | |
| user_data = decode_jwt(token) | |
| user_id = user_data.get("user_id") | |
| except Exception as e: | |
| return jsonify({"error": "Invalid or expired token"}), 401 | |
| # Get query parameters | |
| limit = min(int(request.args.get('limit', 20)), 100) # Max 100 items | |
| offset = int(request.args.get('offset', 0)) | |
| # Get user history | |
| history = History.get_user_history(user_id, limit=limit, offset=offset) | |
| return jsonify({ | |
| "status": "success", | |
| "history": history, | |
| "count": len(history), | |
| "limit": limit, | |
| "offset": offset | |
| }), 200 | |
| except Exception as e: | |
| print(f"History error: {e}") | |
| return jsonify({"error": "Internal server error"}), 500 | |
| def get_history_stats(): | |
| try: | |
| # JWT Authentication | |
| auth_header = request.headers.get('Authorization') | |
| if not auth_header or not auth_header.startswith('Bearer '): | |
| return jsonify({"error": "Missing or invalid Authorization header"}), 401 | |
| token = auth_header.split(" ")[1] | |
| try: | |
| user_data = decode_jwt(token) | |
| user_id = user_data.get("user_id") | |
| except Exception as e: | |
| return jsonify({"error": "Invalid or expired token"}), 401 | |
| # Get user stats | |
| stats = History.get_user_stats(user_id) | |
| return jsonify({ | |
| "status": "success", | |
| "stats": stats | |
| }), 200 | |
| except Exception as e: | |
| print(f"Stats error: {e}") | |
| return jsonify({"error": "Internal server error"}), 500 | |