|
|
| from flask import jsonify, request, session
|
| import hashlib
|
| import uuid
|
| import time
|
| import json
|
| import os
|
|
|
|
|
| STUDENTS_FILE = 'students.json'
|
|
|
| def hash_password(password):
|
| """密码加密"""
|
| return hashlib.sha256(password.encode()).hexdigest()
|
|
|
| def load_students():
|
| """加载学生数据"""
|
| if os.path.exists(STUDENTS_FILE):
|
| with open(STUDENTS_FILE, 'r', encoding='utf-8') as f:
|
| return json.load(f)
|
| return []
|
|
|
| def save_students(students):
|
| """保存学生数据"""
|
| with open(STUDENTS_FILE, 'w', encoding='utf-8') as f:
|
| json.dump(students, f, ensure_ascii=False, indent=2)
|
|
|
| def register_student_routes(app):
|
| """注册学生管理相关的路由"""
|
|
|
| @app.route('/api/teacher/students', methods=['GET'])
|
| def get_students():
|
| """获取学生列表"""
|
| if not session.get('logged_in') or session.get('user_type') != 'teacher':
|
| return jsonify({'success': False, 'message': '需要教师权限'}), 403
|
|
|
| students = load_students()
|
|
|
| safe_students = []
|
| for student in students:
|
| safe_student = {k: v for k, v in student.items() if k != 'password'}
|
| safe_students.append(safe_student)
|
|
|
| return jsonify({
|
| 'success': True,
|
| 'students': safe_students
|
| })
|
|
|
| @app.route('/api/teacher/students', methods=['POST'])
|
| def create_student():
|
| """创建学生账号"""
|
| if not session.get('logged_in') or session.get('user_type') != 'teacher':
|
| return jsonify({'success': False, 'message': '需要教师权限'}), 403
|
|
|
| data = request.json
|
| username = data.get('username')
|
| password = data.get('password')
|
| name = data.get('name')
|
|
|
| if not username or not password:
|
| return jsonify({'success': False, 'message': '用户名和密码不能为空'}), 400
|
|
|
| students = load_students()
|
|
|
|
|
| if any(s['username'] == username for s in students):
|
| return jsonify({'success': False, 'message': '用户名已存在'}), 400
|
|
|
|
|
| new_student = {
|
| 'id': str(uuid.uuid4()),
|
| 'username': username,
|
| 'password': hash_password(password),
|
| 'name': name,
|
| 'student_id': data.get('student_id'),
|
| 'email': data.get('email'),
|
| 'class': data.get('class'),
|
| 'created_at': time.strftime('%Y-%m-%d %H:%M:%S'),
|
| 'created_by': session.get('username'),
|
| 'total_interactions': 0,
|
| 'last_active': None
|
| }
|
|
|
| students.append(new_student)
|
| save_students(students)
|
|
|
|
|
| safe_student = {k: v for k, v in new_student.items() if k != 'password'}
|
|
|
| return jsonify({
|
| 'success': True,
|
| 'message': '学生账号创建成功',
|
| 'student': safe_student
|
| })
|
|
|
| @app.route('/api/teacher/students/<student_id>', methods=['PUT'])
|
| def update_student(student_id):
|
| """更新学生信息"""
|
| if not session.get('logged_in') or session.get('user_type') != 'teacher':
|
| return jsonify({'success': False, 'message': '需要教师权限'}), 403
|
|
|
| data = request.json
|
| students = load_students()
|
|
|
| student = next((s for s in students if s['id'] == student_id), None)
|
| if not student:
|
| return jsonify({'success': False, 'message': '学生不存在'}), 404
|
|
|
|
|
| if 'name' in data:
|
| student['name'] = data['name']
|
| if 'student_id' in data:
|
| student['student_id'] = data['student_id']
|
| if 'email' in data:
|
| student['email'] = data['email']
|
| if 'class' in data:
|
| student['class'] = data['class']
|
| if 'password' in data and data['password']:
|
| student['password'] = hash_password(data['password'])
|
|
|
| save_students(students)
|
|
|
|
|
| safe_student = {k: v for k, v in student.items() if k != 'password'}
|
|
|
| return jsonify({
|
| 'success': True,
|
| 'message': '学生信息已更新',
|
| 'student': safe_student
|
| })
|
|
|
| @app.route('/api/teacher/students/<student_id>', methods=['DELETE'])
|
| def delete_student(student_id):
|
| """删除学生"""
|
| if not session.get('logged_in') or session.get('user_type') != 'teacher':
|
| return jsonify({'success': False, 'message': '需要教师权限'}), 403
|
|
|
| students = load_students()
|
| students = [s for s in students if s['id'] != student_id]
|
| save_students(students)
|
|
|
| return jsonify({
|
| 'success': True,
|
| 'message': '学生已删除'
|
| })
|
|
|
| @app.route('/api/teacher/students/<student_id>/reset-password', methods=['POST'])
|
| def reset_student_password(student_id):
|
| """重置学生密码"""
|
| if not session.get('logged_in') or session.get('user_type') != 'teacher':
|
| return jsonify({'success': False, 'message': '需要教师权限'}), 403
|
|
|
| students = load_students()
|
| student = next((s for s in students if s['id'] == student_id), None)
|
|
|
| if not student:
|
| return jsonify({'success': False, 'message': '学生不存在'}), 404
|
|
|
|
|
| new_password = str(uuid.uuid4())[:8]
|
| student['password'] = hash_password(new_password)
|
| save_students(students)
|
|
|
| return jsonify({
|
| 'success': True,
|
| 'message': '密码已重置',
|
| 'newPassword': new_password
|
| })
|
|
|
| @app.route('/api/teacher/students/<student_id>/progress', methods=['GET'])
|
| def get_student_progress(student_id):
|
| """获取学生学习进度"""
|
| if not session.get('logged_in') or session.get('user_type') != 'teacher':
|
| return jsonify({'success': False, 'message': '需要教师权限'}), 403
|
|
|
|
|
| from app import student_activities
|
|
|
| students = load_students()
|
| student = next((s for s in students if s['id'] == student_id), None)
|
|
|
| if not student:
|
| return jsonify({'success': False, 'message': '学生不存在'}), 404
|
|
|
|
|
| username = student['username']
|
| activities = student_activities.get(username, [])
|
|
|
|
|
| agent_usage = {}
|
| for activity in activities:
|
| agent_name = activity.get('agent_name')
|
| if agent_name:
|
| agent_usage[agent_name] = agent_usage.get(agent_name, 0) + 1
|
|
|
| frequently_used_agents = sorted(agent_usage.keys(),
|
| key=lambda x: agent_usage[x],
|
| reverse=True)[:3]
|
|
|
|
|
| recent_activities = activities[-10:][::-1]
|
|
|
| return jsonify({
|
| 'success': True,
|
| 'progress': {
|
| 'total_interactions': student.get('total_interactions', 0),
|
| 'last_active': student.get('last_active'),
|
| 'frequently_used_agents': frequently_used_agents,
|
| 'recent_activities': [
|
| {
|
| 'title': a['title'],
|
| 'time': time.strftime('%Y-%m-%d %H:%M',
|
| time.localtime(a['timestamp']))
|
| }
|
| for a in recent_activities
|
| ]
|
| }
|
| }) |