Initial commit: 公示系统集合 Flask 项目
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
from flask import Flask, abort, jsonify, render_template, request
|
||||
|
||||
from config import APP_CONFIG, PROJECTS
|
||||
from query_service import query_project_stats, safe_date
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
def get_project_or_404(project_key: str):
|
||||
project = PROJECTS.get(project_key)
|
||||
if not project:
|
||||
abort(404)
|
||||
return project
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def home():
|
||||
user_uuid = str(request.args.get("uuid", "") or "").strip()
|
||||
return render_template(
|
||||
"home.html",
|
||||
app_name=APP_CONFIG["app_name"],
|
||||
projects=PROJECTS,
|
||||
user_uuid=user_uuid,
|
||||
)
|
||||
|
||||
|
||||
@app.route("/project/<project_key>")
|
||||
def project_page(project_key: str):
|
||||
project = get_project_or_404(project_key)
|
||||
user_uuid = str(request.args.get("uuid", "") or "").strip()
|
||||
today = safe_date(request.args.get("date", ""))
|
||||
return render_template(
|
||||
"project.html",
|
||||
project_key=project_key,
|
||||
project=project,
|
||||
user_uuid=user_uuid,
|
||||
today=today,
|
||||
app_name=APP_CONFIG["app_name"],
|
||||
)
|
||||
|
||||
|
||||
@app.route("/api/project/<project_key>/stats")
|
||||
def api_project_stats(project_key: str):
|
||||
project = get_project_or_404(project_key)
|
||||
date_str = safe_date(request.args.get("date", ""))
|
||||
user_uuid = str(request.args.get("uuid", "") or "").strip()
|
||||
result = query_project_stats(project, date_str, user_uuid)
|
||||
if not result.get("success"):
|
||||
return jsonify(
|
||||
{
|
||||
"success": False,
|
||||
"project_key": project_key,
|
||||
"date": date_str,
|
||||
"uuid": user_uuid,
|
||||
"error": result.get("error") or "查询失败",
|
||||
}
|
||||
), 500
|
||||
return jsonify(
|
||||
{
|
||||
"success": True,
|
||||
"configured": bool(result.get("configured")),
|
||||
"message": str(result.get("message") or ""),
|
||||
"project_key": project_key,
|
||||
"project_title": project.get("title", ""),
|
||||
"date": date_str,
|
||||
"uuid": user_uuid,
|
||||
"total": len(result["rows"]),
|
||||
"sum_count": result["sum_count"],
|
||||
"has_current_user": result["has_current_user"],
|
||||
"breakdowns": result.get("breakdowns") or [],
|
||||
"rows": result["rows"],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(f"访问地址: http://127.0.0.1:{APP_CONFIG['port']}")
|
||||
app.run(
|
||||
host=APP_CONFIG["host"],
|
||||
port=APP_CONFIG["port"],
|
||||
debug=APP_CONFIG["debug"],
|
||||
)
|
||||
Reference in New Issue
Block a user