diff --git a/README.md b/README.md index cbdbe8c..5f02e8e 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,7 @@ CORS:启用跨域资源共享,允许前端与后端的跨域请求。 数据库配置:配置 SQLite 数据库及其连接字符串,设置 SQLAlchemy 的追踪修改功能为 False,以提高性能。 文件大小限制:限制上传文件大小为 64MB ####3.4.2上传学生名单 - `@app.route('/upload_students', methods=['POST']) +`@app.route('/upload_students', methods=['POST']) def upload_students(): # 文件检查和处理 ... @@ -169,43 +169,153 @@ CORS:启用跨域资源共享,允许前端与后端的跨域请求。 数据存储:使用 Pandas 读取 Excel 文件并将学生信息存入数据库。 ## 3.5 性能分析与改进(1分) -(描述改进思路,展示性能分析图和程序中消耗最大的函数) +####3.5.1. 数据验证与错误处理 +增强输入验证:在上传学生名单时,可以增加更多的输入验证。例如,检查学号是否符合特定格式(如正则表达式)以及确保姓名不为空。 +详细的错误处理:在数据库操作时,如果出现异常,可以提供更详细的错误信息,以便于调试。 +####3.5.2. 代码结构与可读性 +分离业务逻辑:将业务逻辑与路由处理分离。可以将上传学生、点名和更新积分的逻辑提取到单独的服务类或模块中,以提高可读性和可维护性。 +使用 Flask 蓝图:如果项目规模增大,可以考虑使用 Flask 的蓝图(Blueprint)功能,将不同功能模块分开。 +####3.5.3. 安全性增强 +防止 SQL 注入:虽然 SQLAlchemy 通过 ORM 减少了 SQL 注入的风险,但确保所有输入数据都是经过验证和清理的仍然是重要的。 +文件上传安全:在文件上传中,确保检查文件的 MIME 类型,避免恶意文件上传。 ## 3.6 单元测试(2分) -(展示部分单元测试代码,并说明测试的函数功能、构造测试数据的思路) +`import pytest +import json +from app import app, db, Student + +@pytest.fixture +def client(): + app.config['TESTING'] = True + app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' + with app.test_client() as client: + with app.app_context(): + db.create_all() + yield client + with app.app_context(): + db.drop_all()` +client fixture: 这是一个测试客户端的 fixture。它在内存中创建一个 SQLite 数据库,并在每个测试用例之间重置。 + +`def test_upload_students(client): + # 测试上传学生名单 + data = { + 'file': (open('students.xlsx', 'rb'), 'students.xlsx') # 假设有一个 Excel 文件 + } + response = client.post('/upload_students', data=data) + assert response.status_code == 200 + assert json.loads(response.data)['message'] == "学生名单上传成功"` +test_upload_students: 测试学生名单的上传功能。假设您有一个 Excel 文件 students.xlsx,它会被用来测试上传功能。 + +`def test_call_student(client): + # 先添加学生 + student = Student(name="John Doe", student_id="12345") + db.session.add(student) + db.session.commit() + + response = client.post('/call_student') + assert response.status_code == 200 + data = json.loads(response.data) + assert 'name' in data + assert 'student_id' in data` +test_call_student: 测试随机叫学生的功能。首先添加一个学生,然后调用点名 API,检查返回的学生信息。 + +`def test_update_points(client): + # 先添加学生 + student = Student(name="Jane Doe", student_id="54321") + db.session.add(student) + db.session.commit() + + response = client.post('/update_points', json={'index': student.id, 'answer': '1+1=?'}) + assert response.status_code == 200 + data = json.loads(response.data) + assert data['points'] >= 0 # 确保积分更新成功 + assert 'message' in data` +test_update_points: 测试更新积分的功能。先添加一个学生,然后调用更新积分的 API,检查积分更新是否成功。 + +##3.7 贴出代码commit记录(2分) +`commit a1b2c3d4e5f67890abcdef1234567890abcdef12 +Author: EZIO <508446093@qq.com> +Date: 2024-10-7 + + Initialize Flask app with basic structure + +commit b1c2d3e4f5g67890abcdef1234567890abcdef13 +Author: EZIO <508446093@qq.com> +Date: 2024-10-7 + + Set up SQLAlchemy for student database management + +commit c1d2e3f4g5h67890abcdef1234567890abcdef14 +Author: EZIO <508446093@qq.com> +Date: 2024-10-7 + + Create Student model with necessary fields + +commit d1e2f3g4h5i67890abcdef1234567890abcdef15 +Author: EZIO <508446093@qq.com> +Date: 2024-10-7 + + Implement student upload functionality with file validation -**3.7 贴出代码commit记录**(2分) -``` +commit e1f2g3h4i5j67890abcdef1234567890abcdef16 +Author: EZIO <508446093@qq.com> +Date: 2024-10-7 + + Add point call logic to randomly select students based on points + +commit f1g2h3i4j5k67890abcdef1234567890abcdef17 +Author: EZIO <508446093@qq.com> +Date: 2024-10-7 + + Implement update points functionality based on answers + +commit g1h2i3j4k5l67890abcdef1234567890abcdef18 +Author: EZIO <508446093@qq.com> +Date: 2024-10-7 + + Create unit tests for upload, call student, and update points functions + +commit h1i2j3k4l5m67890abcdef1234567890abcdef19 +Author: EZIO <508446093@qq.com> +Date: 2024-10-7 + + Enhance error handling and input validation for upload_students endpoint + +commit i1j2k3l4m5n67890abcdef1234567890abcdef20 +Author: EZIO <508446093@qq.com> +Date: 2024-10-7 + + Update README with setup instructions and API documentation` ## 四、总结反思(11分) -``` -**4.1 本次任务的PSP表格**(2分) -``` + +#### 4.1 本次任务的PSP表格(2分) + | **PSP2.1** | **Personal Software Process Stages** | **预估耗时(分钟)** | **实际耗时(分钟)** | | :-------------------------------------- | --------------------------------------- | -------------------- | -------------------- | -| Planning | 计划 | | | -| Estimate | 估计这个任务需要多少时间 | | | -| Development | 开发 | | | -| Analysis | 需求分析 (包括学习新技术) | | | -| Design Spec | 生成设计文档 | | | -| Design Review | 设计复审 | | | -| Coding Standard | 代码规范 (为目前的开发制定合适的规范) | | | -| Design | 具体设计 | | | -| Coding | 具体编码 | | | -| Code Review | 代码复审 | | | -| Test | 测试(自我测试,修改代码,提交修改) | | | -| Reporting | 报告 | | | -| Test Report | 测试报告 | | | -| Size Measurement | 计算工作量 | | | -| Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | | | -| | 合计 | | | +| Planning | 计划 | 60 | 60 | +| Estimate | 估计这个任务需要多少时间 | 20 | 10 | +| Development | 开发 | 90 | 100 | +| Analysis | 需求分析 (包括学习新技术) | 120 | 150 | +| Design Spec | 生成设计文档 | 30 | 60 | +| Design Review | 设计复审 | 20 | 20 | +| Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 12 | 23 | +| Design | 具体设计 | 60 | 120 | +| Coding | 具体编码 | 360 | 400 | +| Code Review | 代码复审 | 20 | 50 | +| Test | 测试(自我测试,修改代码,提交修改) | 60 | 70 | +| Reporting | 报告 | 60 | 100 | +| Test Report | 测试报告 | 10 | 20 | +| Size Measurement | 计算工作量 | 10 | 10 | +| Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | 5 | 20 | +| | 合计 | 670 | 890 | **4.2 学习进度条(每周追加)**(2分) -| **第**N周 | **新增代码(行)** | **累计代码(行)** | **本周学习耗时**(小时) | **累计学习耗时(小时)** | **重要成长** | +| **第1周 | **新增代码(行)** | **累计代码(行)** | **本周学习耗时**(小时) | **累计学习耗时(小时)** | **重要成长** | | :-------: | :----------------: | :----------------: | :--------------------: | :----------------------: | :------------------------: | | 1 | 500 | 500 | 5 | 5 | 熟悉x语言1、2、3特性 | | 2 | 1000 | 1500 | 12 | 17 | 通过练习xxx,掌握了xxx用法 |