From: root Date: Tue, 19 May 2026 12:41:58 +0000 (+0800) Subject: update: 修复章节列表和知识点列表为空问题,并增加排序效果 X-Git-Url: http://acesimba.cloud/gitweb/?a=commitdiff_plain;h=2c4c51d97f0b0bed41bffc30c40195c5e91f033f;p=studybase.git update: 修复章节列表和知识点列表为空问题,并增加排序效果 --- diff --git a/app.py b/app.py index 8c80782..959a0ff 100644 --- a/app.py +++ b/app.py @@ -3,13 +3,28 @@ from database import query_db app = Flask(__name__) -# --- 1. 页面路由 --- +# ==================== 1. 核心页面路由 ==================== @app.route('/') def index(): return render_template('index.html') -# 科目首页 +# --- 题目列表模块 --- +@app.route('/questions/') +def questions_list(): + return render_template('questions/index.html') + +# --- 试卷列表模块 --- +@app.route('/papers/') +def papers_list(): + return render_template('papers/index.html') + +# --- 系统设定模块 --- +@app.route('/settings/') +def settings_page(): + return render_template('settings/index.html') + +# --- 错题分析模块 (刚才修复的部分) --- @app.route('/subject/') def subject_portal(): subjects = query_db("SELECT ID, Subject as name FROM Subject WHERE enabled = 1") @@ -17,144 +32,103 @@ def subject_portal(): default_id = subjects[0]['ID'] if subjects else None return render_template('subject/index.html', subjects=subjects, stages=stages, default_id=default_id) -# 章节分析页 @app.route('/subject/chapters') def subject_chapters(): subjects = query_db("SELECT DISTINCT Subject FROM Subject ORDER BY Subject") return render_template('subject/chapters.html', subjects=subjects) -# 知识点分析页 @app.route('/subject/knowledge') def subject_knowledge(): subjects = query_db("SELECT DISTINCT Subject FROM Subject ORDER BY Subject") chapter_id = request.args.get('chapter_id', '') return render_template('subject/knowledge.html', subjects=subjects, chapter_id=chapter_id) -@app.route('/questions/') -def questions(): return render_template('questions/index.html') - -@app.route('/papers/') -def papers(): return render_template('papers/index.html') - -@app.route('/settings/') -def settings(): return render_template('settings/index.html') +# ==================== 2. API 数据接口 ==================== -# --- 2. API 接口 --- - -# 科目概览统计 API +# 科目概览统计 @app.route('/api/subject_stats') def get_subject_stats(): sub_id = request.args.get('subject_id') - stage_ids = request.args.get('stage_ids', '').split(',') - if not sub_id or not stage_ids or stage_ids == ['']: - return jsonify({"error": "参数错误"}), 400 + stage_ids_raw = request.args.get('stage_ids', '') + stage_ids = [sid for sid in stage_ids_raw.split(',') if sid] + + if not sub_id or not stage_ids: + return jsonify({"wrong": {"total": 0, "fixed": 0, "ratio": "0%", "remained": 0}, + "push": {"total": 0, "fixed": 0, "ratio": "0%", "remained": 0}}) placeholders = ','.join(['?'] * len(stage_ids)) - - def get_count(q_type, extra=""): - sql = f"SELECT COUNT(*) as c FROM questions WHERE question_type=? AND subject_id=? AND stage_id IN ({placeholders}) {extra}" - res = query_db(sql, [q_type, sub_id] + stage_ids, one=True) + def get_count(q_type, extra_sql=""): + sql = f"SELECT COUNT(*) as c FROM questions WHERE subject_id = ? AND stage_id IN ({placeholders}) AND question_type = ? {extra_sql}" + res = query_db(sql, [sub_id] + stage_ids + [q_type], one=True) return res['c'] if res else 0 - w_total = get_count('错题') - w_fixed = get_count('错题', 'AND right_times > 0') - p_total = get_count('推题') - p_fixed = get_count('推题', 'AND right_times > 0') - - return jsonify({ - "wrong": {"total": w_total, "fixed": w_fixed, "ratio": f"{(w_fixed/w_total*100):.1f}%" if w_total>0 else "0%", "remained": w_total-w_fixed}, - "push": {"total": p_total, "fixed": p_fixed, "ratio": f"{(p_fixed/p_total*100):.1f}%" if p_total>0 else "0%", "remained": p_total-p_fixed} - }) + try: + w_total = get_count('错题'); w_fixed = get_count('错题', 'AND right_times > 0') + p_total = get_count('推题'); p_fixed = get_count('推题', 'AND right_times > 0') + return jsonify({ + "wrong": {"total": w_total, "fixed": w_fixed, "ratio": f"{(w_fixed/w_total*100):.1f}%" if w_total>0 else "0%", "remained": w_total-w_fixed}, + "push": {"total": p_total, "fixed": p_fixed, "ratio": f"{(p_fixed/p_total*100):.1f}%" if p_total>0 else "0%", "remained": p_total-p_fixed} + }) + except Exception as e: + return jsonify({"error": str(e)}), 500 -# 章节数据 API (基于顶级知识点) +# 章节数据接口 (已适配 chapter_desc) @app.route('/api/chapters') def get_chapters(): - subject = request.args.get('subject', '全部') + subject_name = request.args.get('subject', '全部') keyword = request.args.get('keyword', '') params = [] - - # 过滤 parent_id 为空作为章节 - where = " WHERE kp.parent_id IS NULL AND kp.is_enabled = 1" - if subject != '全部': - where += " AND s.Subject = ?" - params.append(subject) + where = " WHERE 1=1" + if subject_name != '全部': + where += " AND s.Subject = ?"; params.append(subject_name) if keyword: - where += " AND kp.content LIKE ?" - params.append(f"%{keyword}%") + where += " AND c.chapter_desc LIKE ?"; params.append(f"%{keyword}%") sql = f""" - SELECT - kp.id AS chapter_id, - s.Subject AS SubjectName, - kp.content AS ChapterDesc, - (SELECT COUNT(DISTINCT r.question_id) FROM question_kp_relation r WHERE r.kp_id = kp.id) AS total, - (SELECT COUNT(DISTINCT r.question_id) FROM question_kp_relation r - JOIN questions q ON r.question_id = q.id - WHERE r.kp_id = kp.id AND q.right_times > 0) AS correct - FROM knowledge_point kp - JOIN Subject s ON kp.subject_id = s.ID - {where} + SELECT c.id AS chapter_id, s.Subject AS SubjectName, c.chapter_desc AS ChapterDesc, + (SELECT COUNT(DISTINCT r.question_id) FROM question_kp_relation r JOIN knowledge_point kp ON r.kp_id = kp.id WHERE kp.chapter_id = c.id) AS total, + (SELECT COUNT(DISTINCT r.question_id) FROM question_kp_relation r JOIN knowledge_point kp ON r.kp_id = kp.id JOIN questions q ON r.question_id = q.id WHERE kp.chapter_id = c.id AND q.right_times > 0) AS correct + FROM chapters c JOIN Subject s ON c.subject_id = s.ID {where} """ try: rows = query_db(sql, params) - result = [] - for row in rows: - item = dict(row) # 必须转字典,Row 不支持赋值 + result = [dict(row) for row in rows] + for item in result: item['not_correct'] = (item['total'] or 0) - (item['correct'] or 0) - # 只要有题目就显示 - if item['total'] > 0: - result.append(item) - return jsonify(result) + return jsonify([i for i in result if i['total'] > 0]) except Exception as e: - print(f"SQL Error in chapters: {e}") return jsonify({"error": str(e)}), 500 -# 知识点数据 API +# 知识点数据接口 @app.route('/api/knowledge') def get_knowledge(): - subject = request.args.get('subject', '全部') + subject_name = request.args.get('subject', '全部') keyword = request.args.get('keyword', '') chapter_id = request.args.get('chapter_id', '') params = [] - where = " WHERE kp.is_enabled = 1" if chapter_id: - # 如果有章节ID,查询该节点及其子节点 - where += " AND (kp.id = ? OR kp.parent_id = ?)" - params.extend([chapter_id, chapter_id]) - if subject != '全部': - where += " AND s.Subject = ?" - params.append(subject) + where += " AND kp.chapter_id = ?"; params.append(chapter_id) + if subject_name != '全部': + where += " AND s.Subject = ?"; params.append(subject_name) if keyword: - where += " AND kp.content LIKE ?" - params.append(f"%{keyword}%") + where += " AND kp.content LIKE ?"; params.append(f"%{keyword}%") sql = f""" - SELECT - kp.id, - s.Subject as subject_name, - kp.content, + SELECT kp.id, s.Subject as subject_name, kp.content, (SELECT COUNT(*) FROM question_kp_relation r WHERE r.kp_id = kp.id) as total, - (SELECT COUNT(DISTINCT r.question_id) FROM question_kp_relation r - JOIN questions q ON r.question_id = q.id - WHERE r.kp_id = kp.id AND q.right_times >= 1) as correct - FROM knowledge_point kp - JOIN Subject s ON kp.subject_id = s.ID - {where} + (SELECT COUNT(DISTINCT r.question_id) FROM question_kp_relation r JOIN questions q ON r.question_id = q.id WHERE r.kp_id = kp.id AND q.right_times >= 1) as correct + FROM knowledge_point kp JOIN Subject s ON kp.subject_id = s.ID {where} """ try: rows = query_db(sql, params) - result = [] - for row in rows: - item = dict(row) + result = [dict(row) for row in rows] + for item in result: item['not_correct'] = (item['total'] or 0) - (item['correct'] or 0) - result.append(item) return jsonify(result) except Exception as e: - print(f"SQL Error in knowledge: {e}") return jsonify({"error": str(e)}), 500 if __name__ == '__main__': - # 确保在服务器上运行时监听 0.0.0.0 app.run(host='0.0.0.0', port=5000, debug=True) diff --git a/templates/subject/chapters.html b/templates/subject/chapters.html index b7d5cba..75a0eca 100644 --- a/templates/subject/chapters.html +++ b/templates/subject/chapters.html @@ -6,159 +6,97 @@ 章节分析 - - +
-
- - + + + 返回
-
- +
- - - - + + + + - - - - - +
科目章节名称错题总数尚未正确科目章节总数待完成
正在加载数据...
- -
- 返回科目首页 -
diff --git a/templates/subject/knowledge.html b/templates/subject/knowledge.html index 80dc95b..e2197cc 100644 --- a/templates/subject/knowledge.html +++ b/templates/subject/knowledge.html @@ -1,64 +1,101 @@ - + - - + + 知识点分析 + - +
- + + 返回
- - - - - - - - - - -
科目知识点内容总题数待完成
- 返回科目首页 +
+ + + + + + + + + + +
科目知识点内容总数待完成
+