diff --git a/check_applications.py b/check_applications.py new file mode 100644 index 0000000..c786140 --- /dev/null +++ b/check_applications.py @@ -0,0 +1,50 @@ +""" +检查教师申请数据的完整性 +""" +import sqlite3 + +db_path = 'instance/database.db' +conn = sqlite3.connect(db_path) +cursor = conn.cursor() + +print("检查教师申请数据完整性...\n") + +# 获取所有待审批的申请 +cursor.execute(""" + SELECT id, user_id, contest_id, name, email, status + FROM teacher_application + WHERE status = 'pending' +""") +apps = cursor.fetchall() + +if not apps: + print("没有待审批的教师申请") +else: + print(f"找到 {len(apps)} 个待审批的申请\n") + + for app in apps: + app_id, user_id, contest_id, name, email, status = app + print(f"申请 ID: {app_id}") + print(f" 用户ID: {user_id}, 杯赛ID: {contest_id}") + print(f" 姓名: {name}, 邮箱: {email}") + + # 检查用户是否存在 + cursor.execute("SELECT id, name FROM user WHERE id = ?", (user_id,)) + user = cursor.fetchone() + if user: + print(f" [OK] 用户存在: {user[1]}") + else: + print(f" [ERROR] 用户不存在!") + + # 检查杯赛是否存在 + cursor.execute("SELECT id, name FROM contest WHERE id = ?", (contest_id,)) + contest = cursor.fetchone() + if contest: + print(f" [OK] 杯赛存在: {contest[1]}") + else: + print(f" [ERROR] 杯赛不存在!") + + print() + +conn.close() +print("检查完成") diff --git a/instance/database.db b/instance/database.db index 72519e4..9f7a9f3 100644 Binary files a/instance/database.db and b/instance/database.db differ diff --git a/migrate_db.py b/migrate_db.py new file mode 100644 index 0000000..ef6bc81 --- /dev/null +++ b/migrate_db.py @@ -0,0 +1,62 @@ +""" +数据库迁移脚本:为申请表添加拒绝次数控制字段 +""" +import sqlite3 +import os + +db_path = 'instance/database.db' + +if not os.path.exists(db_path): + print(f"错误:找不到数据库文件 {db_path}") + exit(1) + +conn = sqlite3.connect(db_path) +cursor = conn.cursor() + +print("开始数据库迁移...") + +# 检查并更新 teacher_application 表 +print("\n检查 teacher_application 表...") +cursor.execute("PRAGMA table_info(teacher_application)") +columns = [row[1] for row in cursor.fetchall()] +print(f"现有列: {columns}") + +if 'rejection_count' not in columns: + print("添加 rejection_count 字段...") + cursor.execute("ALTER TABLE teacher_application ADD COLUMN rejection_count INTEGER DEFAULT 0") + print("[OK] 已添加 rejection_count 字段") +else: + print("[OK] rejection_count 字段已存在") + +if 'last_rejected_at' not in columns: + print("添加 last_rejected_at 字段...") + cursor.execute("ALTER TABLE teacher_application ADD COLUMN last_rejected_at DATETIME") + print("[OK] 已添加 last_rejected_at 字段") +else: + print("[OK] last_rejected_at 字段已存在") + +# 检查并更新 contest_application 表 +print("\n检查 contest_application 表...") +cursor.execute("PRAGMA table_info(contest_application)") +columns = [row[1] for row in cursor.fetchall()] +print(f"现有列: {columns}") + +if 'rejection_count' not in columns: + print("添加 rejection_count 字段...") + cursor.execute("ALTER TABLE contest_application ADD COLUMN rejection_count INTEGER DEFAULT 0") + print("[OK] 已添加 rejection_count 字段") +else: + print("[OK] rejection_count 字段已存在") + +if 'last_rejected_at' not in columns: + print("添加 last_rejected_at 字段...") + cursor.execute("ALTER TABLE contest_application ADD COLUMN last_rejected_at DATETIME") + print("[OK] 已添加 last_rejected_at 字段") +else: + print("[OK] last_rejected_at 字段已存在") + +conn.commit() +conn.close() + +print("\n[SUCCESS] 数据库迁移完成!") +print("现在可以正常使用申请功能了。") diff --git a/templates/admin_teacher_applications.html b/templates/admin_teacher_applications.html index 7c69399..411809f 100644 --- a/templates/admin_teacher_applications.html +++ b/templates/admin_teacher_applications.html @@ -4,7 +4,30 @@ {% block admin_content %}
-

教师申请审核

+
+

教师申请审核

+
+ {% if session.user.role == 'admin' %} + 管理员视图 + {% else %} + 杯赛负责人视图 + {% endif %} +
+
+ + {% if apps|length == 0 %} +
+
+ +
+

暂无待审核的教师申请

+ {% if session.user.role != 'admin' %} +

您只能看到自己负责的杯赛的教师申请。如果有新的申请提交,会在此处显示。

+ {% endif %} +
+
+
+ {% endif %}
diff --git a/test_owner_access.py b/test_owner_access.py new file mode 100644 index 0000000..1b21032 --- /dev/null +++ b/test_owner_access.py @@ -0,0 +1,68 @@ +""" +测试杯赛负责人访问教师申请审批页面 +""" +import sqlite3 + +db_path = 'instance/database.db' +conn = sqlite3.connect(db_path) +cursor = conn.cursor() + +print("模拟杯赛负责人访问教师申请审批页面...\n") + +# 获取一个杯赛负责人 +cursor.execute(""" + SELECT cm.user_id, u.name, u.role, cm.contest_id, c.name as contest_name + FROM contest_membership cm + JOIN user u ON cm.user_id = u.id + JOIN contest c ON cm.contest_id = c.id + WHERE cm.role = 'owner' + LIMIT 1 +""") +owner = cursor.fetchone() + +if not owner: + print("没有找到杯赛负责人") + conn.close() + exit() + +user_id, user_name, user_role, contest_id, contest_name = owner +print(f"杯赛负责人信息:") +print(f" 用户ID: {user_id}") +print(f" 用户名: {user_name}") +print(f" 角色: {user_role}") +print(f" 负责杯赛ID: {contest_id}") +print(f" 杯赛名称: {contest_name}") + +# 模拟查询该负责人能看到的申请 +cursor.execute(""" + SELECT cm.contest_id + FROM contest_membership cm + WHERE cm.user_id = ? AND cm.role = 'owner' +""", (user_id,)) +owned_contests = [row[0] for row in cursor.fetchall()] + +print(f"\n该用户负责的所有杯赛ID: {owned_contests}") + +if owned_contests: + placeholders = ','.join('?' * len(owned_contests)) + cursor.execute(f""" + SELECT ta.id, ta.user_id, ta.contest_id, ta.status, u.name, c.name + FROM teacher_application ta + JOIN user u ON ta.user_id = u.id + JOIN contest c ON ta.contest_id = c.id + WHERE ta.status = 'pending' AND ta.contest_id IN ({placeholders}) + ORDER BY ta.applied_at DESC + """, owned_contests) + apps = cursor.fetchall() + + print(f"\n该负责人能看到的待审批申请数: {len(apps)}") + if apps: + for app in apps: + print(f" 申请ID: {app[0]}, 申请人: {app[4]}, 杯赛: {app[5]}") + else: + print(" 暂无待审核的教师申请") +else: + print("\n该用户不负责任何杯赛") + +conn.close() +print("\n测试完成")