fix: 主页预嵌入统计数据,日历首次加载即可显示状态圆点

This commit is contained in:
mac
2026-06-01 23:33:10 +08:00
parent b2fcd0c37a
commit 4d3f8099ec
3 changed files with 64 additions and 54 deletions

101
app.py
View File

@@ -68,11 +68,58 @@ def logout():
# ── 主页 ──────────────────────────────────────────────
def compute_stats():
"""计算统计数据,供 API 和模板共用"""
rows = get_all_checkins()
total_days = len(rows)
total_morning = 0
total_evening = 0
total_study = 0
calendar = {}
for row in rows:
d = row['date']
data = row['data']
morning = data.get('morning', [])
evening = data.get('evening', [])
study = data.get('study', [])
total_morning += sum(1 for x in morning if isinstance(x, str) and x.strip())
total_evening += sum(1 for x in evening if (
isinstance(x, str) and x.strip() or
isinstance(x, dict) and (x.get('mistake', '') or '').strip()
))
total_study += sum(1 for x in study if x.get('done'))
day_score = 0
has_morning = any(isinstance(x, str) and x.strip() for x in morning)
has_evening = any(
(isinstance(x, str) and x.strip()) or
(isinstance(x, dict) and (x.get('mistake', '') or '').strip())
for x in evening
)
has_study = any(x.get('done') for x in study)
if has_morning: day_score += 1
if has_evening: day_score += 1
if has_study: day_score += 1
calendar[d] = 'pass' if day_score >= 2 else 'fail'
return dict(
total_days=total_days, total_morning=total_morning,
total_evening=total_evening, total_study=total_study,
calendar=calendar
)
@app.route('/')
@login_required
def index():
import json
stats = compute_stats()
return render_template('index.html',
username=session.get('display_name', session.get('username', '')))
username=session.get('display_name', session.get('username', '')),
initial_stats=json.dumps(stats, ensure_ascii=False))
# ── API ──────────────────────────────────────────────
@@ -116,56 +163,8 @@ def api_history():
@app.route('/api/stats', methods=['GET'])
@login_required
def api_stats():
"""返回统计数据和日历状态映射"""
rows = get_all_checkins()
total_days = len(rows)
total_morning = 0
total_evening = 0
total_study = 0
calendar = {}
for row in rows:
d = row['date']
data = row['data']
morning = data.get('morning', [])
evening = data.get('evening', [])
study = data.get('study', [])
# 统计条目
total_morning += sum(1 for x in morning if isinstance(x, str) and x.strip())
total_evening += sum(1 for x in evening if (
isinstance(x, str) and x.strip() or
isinstance(x, dict) and (x.get('mistake', '') or '').strip()
))
total_study += sum(1 for x in study if x.get('done'))
# 评分
day_score = 0
has_morning = any(isinstance(x, str) and x.strip() for x in morning)
has_evening = any(
(isinstance(x, str) and x.strip()) or
(isinstance(x, dict) and (x.get('mistake', '') or '').strip())
for x in evening
)
has_study = any(x.get('done') for x in study)
if has_morning:
day_score += 1
if has_evening:
day_score += 1
if has_study:
day_score += 1
calendar[d] = 'pass' if day_score >= 2 else 'fail'
return jsonify({
'ok': True,
'total_days': total_days,
'total_morning': total_morning,
'total_evening': total_evening,
'total_study': total_study,
'calendar': calendar
})
stats = compute_stats()
return jsonify({'ok': True, **stats})
@app.route('/api/user', methods=['GET'])