From 718566efb13fd7e2bc807664805fd3943b6dc1c2 Mon Sep 17 00:00:00 2001 From: Lyanling <1736289433@qq.com> Date: Wed, 9 Oct 2024 22:47:47 +0800 Subject: [PATCH] forth commit --- .../__pycache__/settings.cpython-37.pyc | Bin 2573 -> 2962 bytes .../mysite10_7/mysite10_7/settings.py | 34 +- .../polls/__pycache__/tests.cpython-37.pyc | Bin 0 -> 208 bytes .../polls/__pycache__/urls.cpython-37.pyc | Bin 683 -> 683 bytes .../polls/__pycache__/views.cpython-37.pyc | Bin 4031 -> 4918 bytes .../__pycache__/__init__.cpython-37.pyc | Bin 0 -> 177 bytes .../polls/templates/confirm_roll_call.html | 78 +-- .../polls/templates/leaderboard.html | 54 +- .../mysite10_7/polls/templates/login.html | 30 +- .../mysite10_7/polls/templates/register.html | 30 +- .../polls/templates/upload_students.html | 28 +- pythonProject03/mysite10_7/polls/tests.py | 3 - pythonProject03/mysite10_7/polls/views.py | 489 +++++------------- pythonProject03/mysite10_7/test/__init__.py | 0 .../test/__pycache__/__init__.cpython-37.pyc | Bin 0 -> 166 bytes pythonProject03/mysite10_7/test/test01.py | 6 + .../mysite10_7/test/test_Login_view.py | 19 + .../mysite10_7/test/test_confirm_roll_call.py | 29 ++ .../mysite10_7/test/test_leaderboard.py | 13 + .../mysite10_7/test/test_register.py | 19 + .../mysite10_7/test/test_roll_call.py | 20 + .../mysite10_7/test/tset_upload_students.py | 18 + 22 files changed, 389 insertions(+), 481 deletions(-) create mode 100644 pythonProject03/mysite10_7/polls/__pycache__/tests.cpython-37.pyc create mode 100644 pythonProject03/mysite10_7/polls/templates/__pycache__/__init__.cpython-37.pyc create mode 100644 pythonProject03/mysite10_7/test/__init__.py create mode 100644 pythonProject03/mysite10_7/test/__pycache__/__init__.cpython-37.pyc create mode 100644 pythonProject03/mysite10_7/test/test01.py create mode 100644 pythonProject03/mysite10_7/test/test_Login_view.py create mode 100644 pythonProject03/mysite10_7/test/test_confirm_roll_call.py create mode 100644 pythonProject03/mysite10_7/test/test_leaderboard.py create mode 100644 pythonProject03/mysite10_7/test/test_register.py create mode 100644 pythonProject03/mysite10_7/test/test_roll_call.py create mode 100644 pythonProject03/mysite10_7/test/tset_upload_students.py diff --git a/pythonProject03/mysite10_7/mysite10_7/__pycache__/settings.cpython-37.pyc b/pythonProject03/mysite10_7/mysite10_7/__pycache__/settings.cpython-37.pyc index 405ddbce6546c5fa129ea2e20531c3b0df6cd71c..825fc9295810c9ef3aa2e0d88700a60c1b8d7346 100644 GIT binary patch delta 656 zcmY*VOHUI~6z)u?(+3nvc@$b+MNwLrDn5z2u(l%|(^op9kmG`kbFRav%)p)7fRh-O ztXycfEOcYynyoAUgBvz3-1#fK1x?_bd|&SQzH{zL&WEv2nWe|cWFo}l`+M2`l?sJ^ z_ly70cOG&0A>rfF(*sP|2^ho?Oyeky;W$oU21o4}BryU*9K#$boP-gaBI4`x=TQ>F z>=)4%FoHRp#u=Q&Ia_iBoIjFm0md*7sq6}*Z|SU8d$0Vc4B*LYtMmrq&Y z{Gf;>N3bQx;B}b98!*M!WjS&j)3^dNcoSyv7T)F`%;6oF$Geb6Q4baF9j66NOu~TW zQGbh=NT`4OBD^Z(7SAcAZ3po!*)u6ARXlopD7_3HL_w~qb=6Q$!fUEQqx>8YX_t*k z4WxEUH%`LsX9h^mHFdo(07Dy%dX2H24%zJzpFv_{^NChho8^WY44alkc9=q3%R|St zA!&BmmPehyWRB+sgO=wq;xdK3+aVA}mz~Joed^eb8zj0up^9m9I~dqkET6t&-W%cu zseOf>?INh`Icy8$HZ|>Sv#`+n6Fppm@k*Q19)=eyl+H+)zU`YOSh%L940ds@l=Nh3GOr6J72-j5`ABeU0a{q99A6 Wz*9VB{*oh-(9hn__^K3>q{LriinJR5 delta 266 zcmXv}yGjF56ujr|gKRb%^VnpQY@)8uU5)Q1m580TLW)yQxU6OT06_~Y3%zX$EiGkx z3;)1R5W(6{@EUMnh8Y+J<}tbt)6*a*3xf}G)NNIWufOmQ&zKX#t8ej;o?k1EE2yE4 z2AT-bQZ5H*a~07stYQu8*udt@ z&54^xk(pkKt+{T`b(oW~Im8aPu*+>E4o7gfW7x$W_pr|~4sd7&`j8hQJ1T~BCW{+u pM8&5)wrHS#N_YCrd-q<%G&4u0nSL#saUJ`KKK3ipwe;8@{Q}e-J@^0s diff --git a/pythonProject03/mysite10_7/mysite10_7/settings.py b/pythonProject03/mysite10_7/mysite10_7/settings.py index 3526ca3..bf61e74 100644 --- a/pythonProject03/mysite10_7/mysite10_7/settings.py +++ b/pythonProject03/mysite10_7/mysite10_7/settings.py @@ -40,6 +40,7 @@ INSTALLED_APPS = [ 'polls', 'rest_framework', 'drf_spectacular', + 'corsheaders' ] MIDDLEWARE = [ @@ -50,8 +51,15 @@ MIDDLEWARE = [ 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'corsheaders.middleware.CorsMiddleware', + 'django.middleware.common.CommonMiddleware', + + ] +CORS_ORIGIN_ALLOW_ALL = True +CORS_ALLOW_CREDENTIALS = True +CORS_ALLOW_ALL_ORIGINS = True ROOT_URLCONF = 'mysite10_7.urls' TEMPLATES = [ @@ -133,4 +141,28 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' REST_FRAMEWORK = { 'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema', # 使用 drf-spectacular 生成接口 -} \ No newline at end of file +} +# ----------------------- +CORS_ALLOW_METHODS = ( + 'DELETE', + 'GET', + 'OPTIONS', + 'PATCH', + 'POST', + 'PUT', + 'VIEW', +) + +CORS_ALLOW_HEADERS = ( + 'XMLHttpRequest', + 'X_FILENAME', + 'accept-encoding', + 'authorization', + 'content-type', + 'dnt', + 'origin', + 'user-agent', + 'x-csrftoken', + 'x-requested-with', + 'Pragma', +) \ No newline at end of file diff --git a/pythonProject03/mysite10_7/polls/__pycache__/tests.cpython-37.pyc b/pythonProject03/mysite10_7/polls/__pycache__/tests.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e943c84cce72bacd63403749777f8483e29c29f5 GIT binary patch literal 208 zcmZ?b<>g`kf`f;c((QrtV-N=hn1BoiATAaF5-AKRj5!Rsj8Tk?3@J>(44TX@fuanW zjJG&KQj1HR6N^**G?{L3r(`AOrRVFF0L6+x2CQT#VgXWM;+Lu(&@6`$A6z8`iR=M66EgQdkXAtgN0(9#e=1OZ`C+ELK5**ms(y}PsAnZ?*z zR|)}|Mlo?A4y6fYU1*w8kx+pY5D@;4`>9gBYv&W{S3dQ3?j5fmgzjqYoVn-BeVlvF z@0>G_q|*rvzyDsa$3NPmY5$>z)t?FCm$>|#u4znTdR43Gj;<4JRE?VHn4nE&Rjp^K zFQWLhh!d&VjxFWvYSf8>7lnMxiG_V}CxJe@nsk!5$EvAX+DX?kPDb+M)od;2U{dfB z7;|;3WVNr>@ARYJWU1VFv&^g7Dc#w+Zp@x6_*s^d{6WxttY6aGKyP6K zlHLw_D;t#b4$#}!c1iC9y@TzP^e)i5*ltPhX7;F--*c1nl-GsL-8yqQsMKRY(0Io6 z8(!UaZ$fFr*6+%j+yE_N++}6%mIBc??gj<#LEctw_;J)nE8UeVO=NeV7$PSmQ?qQGDN;Eh9=uXG@Yg~Q` zO@L!$Mp-|lV+-a{5v{qtUmSOvSunewT>qgmkEMwsuN|N8YVMTBC+`gfp5`8HbL8IRwQ0W` zxc7_{_8)I})vAB|rLsHa4>zVoHt@JRUiJf*7pRlw@JVHw!TZtnwiCevElFYRH)@15dNS&* zeDq!=t#kEkcj43JKfk*2>E+J3>zy~xcCNj?+&+n8EYHIV zaEO?o7&QInb{gpIc6Z@4#D|WIK6P}cyYO*$?kZ#@vvcml?)huovnPjW%|n=@`>$7M zpamKzDeRhyqi4dZz+Od!49^$V36D(+6NW94qd}9o_2BXPmwE_N@Qv7LvG1q?xUQ0JRjj?g~DRRpeXdF&>M~k9Y~u~O&{^o8;oTTUH(oqnvu{A zJw<;brVr>b+z0fW-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3tMxnMvDH34IisNTi5jNah%=sy%Tq^E8K^M;Ml3> W(dP)B0H>RE(|Go}``mNx(EkAXXz{54 diff --git a/pythonProject03/mysite10_7/polls/templates/__pycache__/__init__.cpython-37.pyc b/pythonProject03/mysite10_7/polls/templates/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..bc3983e1f4683b72d30febab5e60c3538832f745 GIT binary patch literal 177 zcmZ?b<>g`kg5)Di=^*+sh=2h`Aj1KOi&=m~3PUi1CZpdlIYq;;_lhPbtkwwFBAt8HgDG0Yfgv literal 0 HcmV?d00001 diff --git a/pythonProject03/mysite10_7/polls/templates/confirm_roll_call.html b/pythonProject03/mysite10_7/polls/templates/confirm_roll_call.html index 95eb141..5cc69ed 100644 --- a/pythonProject03/mysite10_7/polls/templates/confirm_roll_call.html +++ b/pythonProject03/mysite10_7/polls/templates/confirm_roll_call.html @@ -1,3 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -31,45 +66,10 @@ + + + + - - - - - 确认点名 - - -
- {% csrf_token %} -

确认点名学生:

-

姓名: {{ student.name }}

-

学号: {{ student.student_id }}

- - - - - - - - - - - -
- -{% if protection_awarded %} -

该学生获得保护权,自动加2分!

-{% endif %} - - - diff --git a/pythonProject03/mysite10_7/polls/templates/leaderboard.html b/pythonProject03/mysite10_7/polls/templates/leaderboard.html index 997ce63..2b5da7a 100644 --- a/pythonProject03/mysite10_7/polls/templates/leaderboard.html +++ b/pythonProject03/mysite10_7/polls/templates/leaderboard.html @@ -1,27 +1,27 @@ - - - - 积分排行榜 - - -

积分排行榜

- - - - - - - - - {% for student in students %} - - - - - - - - {% endfor %} -
排名学号姓名积分到课次数
{{ forloop.counter }} {{ student.student_id }}{{ student.name }}{{ student.score }}{{ student.attendance_count }}
- - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pythonProject03/mysite10_7/polls/templates/login.html b/pythonProject03/mysite10_7/polls/templates/login.html index 4b3567f..0bb44a6 100644 --- a/pythonProject03/mysite10_7/polls/templates/login.html +++ b/pythonProject03/mysite10_7/polls/templates/login.html @@ -1,15 +1,15 @@ - - - - - 登录 - - -
- {% csrf_token %} -

-

- -
- - \ No newline at end of file + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pythonProject03/mysite10_7/polls/templates/register.html b/pythonProject03/mysite10_7/polls/templates/register.html index bb353f2..1515a51 100644 --- a/pythonProject03/mysite10_7/polls/templates/register.html +++ b/pythonProject03/mysite10_7/polls/templates/register.html @@ -1,15 +1,15 @@ - - - - - 注册 - - -
- {% csrf_token %} -

-

- -
- - \ No newline at end of file + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pythonProject03/mysite10_7/polls/templates/upload_students.html b/pythonProject03/mysite10_7/polls/templates/upload_students.html index 68bae50..d99ce94 100644 --- a/pythonProject03/mysite10_7/polls/templates/upload_students.html +++ b/pythonProject03/mysite10_7/polls/templates/upload_students.html @@ -1,15 +1,15 @@ - - - - - Title - - -
- {% csrf_token %} - {{ form.as_p }} - -
+ + + + + + + + + + + + - - \ No newline at end of file + + \ No newline at end of file diff --git a/pythonProject03/mysite10_7/polls/tests.py b/pythonProject03/mysite10_7/polls/tests.py index 7ce503c..e69de29 100644 --- a/pythonProject03/mysite10_7/polls/tests.py +++ b/pythonProject03/mysite10_7/polls/tests.py @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/pythonProject03/mysite10_7/polls/views.py b/pythonProject03/mysite10_7/polls/views.py index b1b155d..b3fff2b 100644 --- a/pythonProject03/mysite10_7/polls/views.py +++ b/pythonProject03/mysite10_7/polls/views.py @@ -1,165 +1,3 @@ -# from django.shortcuts import render -# from django.http import HttpResponse -# from .models import * -# from django.shortcuts import render, redirect, get_object_or_404 -# import random -# import pandas as pd -# from django.db.models import Window, F -# from django.db.models.functions import Rank -# # 导入表单 -# from .forms import UploadFileForm -# -# from rest_framework import viewsets -# from .serializers import StudentSerializer -# -# from rest_framework.response import Response -# from rest_framework.decorators import api_view -# # Create your views here. -# def toLogin_view(request): -# return render(request,'login.html') -# def Login_view(request): -# u=request.POST.get("user",'') -# p=request.POST.get("pwd",'') -# -# if u and p: -# c=StudentInfo.objects.filter(stu_name=u,stu_pwd=p).count() -# if c >= 1: -# return HttpResponse("登录成功!") -# else: -# return HttpResponse("账号密码错误!") -# else: -# return HttpResponse("请输入正确的账号和密码!") -# -# def toregister_view(request): -# return render(request, 'register.html') -# -# # #点击注册后做的逻辑判断 -# def register_view(request): -# u = request.POST.get("user", '') -# p = request.POST.get("pwd", '') -# if u and p: -# stu = StudentInfo(stu_id=random.choice('0123456789'),stu_name=u, stu_pwd=p) -# stu.save() -# return HttpResponse("注册成功") -# else: -# return HttpResponse("请输入完整的账号和密码!") -# -# #导入excel -# # 上传学生名单的视图 -# def upload_students(request): -# if request.method == 'POST': -# form = UploadFileForm(request.POST, request.FILES) -# if form.is_valid(): -# # 读取上传的 Excel 文件 -# excel_file = request.FILES['file'] -# df = pd.read_excel(excel_file) # 使用 pandas 读取 Excel 文件 -# -# # 遍历 DataFrame,将每个学生保存到数据库 -# for _, row in df.iterrows(): -# student_id = row['student_id'] -# name = row['name'] -# Student.objects.get_or_create(student_id=student_id, name=name) # 如果学生已存在则不创建 -# -# return redirect('roll_call') # 完成后重定向到点名页面 -# else: -# form = UploadFileForm() -# -# return render(request, 'upload_students.html', {'form': form}) # 渲染上传页面 -# -# # 开始点名的视图 -# def roll_call(request): -# students = Student.objects.all() # 获取所有学生 -# selected_student = None # 初始化被选中的学生 -# -# # 当教师点击“开始点名”按钮时 -# if request.method == 'POST' and 'start_roll_call' in request.POST: -# # 设置权重:总分越高,被点名的概率越低 -# weights = [1 / (student.score + 1) for student in students] # 根据分数调整被点名概率 -# selected_student = random.choices(students, weights=weights, k=1)[0] # 随机选择一个学生 -# request.session['selected_student_id'] = selected_student.student_id # 存储被点名学生的ID到session中 -# return redirect('confirm_roll_call') # 跳转到确认点名页面 -# -# return render(request, 'roll_call.html', {'selected_student': selected_student}) # 渲染点名页面 -# -# # 确认点名的视图 -# # def confirm_roll_call(request): -# # # 从 session 中获取被点名的学生 -# # student_id = request.session.get('selected_student_id') -# # student = get_object_or_404(Student, student_id=student_id) -# # -# # if request.method == 'POST': -# # # 学生是否到课 -# # if 'attended' in request.POST: # 如果选择了到课 -# # student.score += 1 # 到课加1分 -# # student.attendance_count += 1 # 到课次数加1 -# # -# # # 处理是否准确重复问题 -# # if request.POST['question_repeat'] == 'accurate': -# # student.score += 0.5 # 重复问题准确,加0.5分 -# # else: -# # student.score -= 1 # 重复问题不准确,扣1分 -# # -# # # 处理回答问题的准确性(0-3分) -# # answer_accuracy = float(request.POST.get('answer_accuracy', 0)) -# # student.score += answer_accuracy # 根据回答准确性加分 -# # else: -# # student.score -= 5 # 未到课扣5分 -# # -# # student.save() # 保存更新后的学生信息 -# # return redirect('roll_call') # 返回点名页面,进行下一轮点名 -# # -# # return render(request, 'confirm_roll_call.html', {'student': student}) # 渲染确认点名页面 -# def confirm_roll_call(request): -# student_id = request.session.get('selected_student_id') -# student = get_object_or_404(Student, student_id=student_id) -# protection_awarded = False # 初始化保护权标志 -# -# if request.method == 'POST': -# # 增加学生的被点名次数 -# student.called_count += 1 -# -# # 检查学生是否有“保护权” -# if student.called_count % 3 == 0: -# # 如果被点名次数是3的倍数,赋予保护权,自动加2分 -# student.score += 2 -# protection_awarded = True # 设置保护权标志 -# print(f"Student {student.name} ({student.student_id})获得保护权,自动加2分") -# else: -# # 学生是否到课 -# if 'attended' in request.POST: # 如果选择了到课 -# student.score += 1 # 到课加1分 -# student.attendance_count += 1 # 到课次数加1 -# -# # 处理是否准确重复问题 -# if request.POST['question_repeat'] == 'accurate': -# student.score += 0.5 # 重复问题准确,加0.5分 -# else: -# student.score -= 1 # 重复问题不准确,扣1分 -# -# # 处理回答问题的准确性(0-3分) -# answer_accuracy = float(request.POST.get('answer_accuracy', 0)) -# student.score += answer_accuracy # 根据回答准确性加分 -# else: -# student.score -= 5 # 未到课扣5分 -# -# # 保存更新后的学生信息 -# student.save() -# return redirect('roll_call') # 返回点名页面,进行下一轮点名 -# -# return render(request, 'confirm_roll_call.html', {'student': student, 'protection_awarded': protection_awarded}) # 渲染确认点名页面 -# -# -# -# -# # 积分排行榜 -# def leaderboard(request): -# students = Student.objects.all().order_by('-score') # 按分数降序排列 -# return render(request, 'leaderboard.html', {'students': students}) -# -# -# class StudentViewSet(viewsets.ModelViewSet): -# queryset = Student.objects.all() # 获取所有学生数据 -# serializer_class = StudentSerializer # 指定序列化器 from django.shortcuts import render from django.http import HttpResponse from .models import * @@ -167,86 +5,43 @@ from django.shortcuts import render, redirect, get_object_or_404 import random from django.http import JsonResponse import pandas as pd +import json from django.db.models import Window, F from django.db.models.functions import Rank from django.shortcuts import get_object_or_404 +from django.views.decorators.csrf import csrf_exempt # 导入表单 from .forms import UploadFileForm -# Create your views here. -def toLogin_view(request): - return render(request,'login.html') -# def Login_view(request): -# u=request.POST.get("user",'') -# p=request.POST.get("pwd",'') -# -# if u and p: -# c=StudentInfo.objects.filter(stu_name=u,stu_pwd=p).count() -# if c >= 1: -# return HttpResponse("登录成功!") -# else: -# return HttpResponse("账号密码错误!") -# else: -# return HttpResponse("请输入正确的账号和密码!") -def Login_view(request): - u = request.POST.get("user", '') - p = request.POST.get("pwd", '') - - if u and p: - c = StudentInfo.objects.filter(stu_name=u, stu_pwd=p).count() - if c >= 1: - return JsonResponse({"message": "登录成功!"}) - else: - return JsonResponse({"message": "账号密码错误!"}, status=400) - else: - return JsonResponse({"message": "请输入正确的账号和密码!"}, status=400) +@csrf_exempt def toregister_view(request): - return render(request, 'register.html') + return JsonResponse({'message': '注册成功!'}) +import json +from django.views.decorators.csrf import csrf_exempt - # #点击注册后做的逻辑判断 -# def register_view(request): -# u = request.POST.get("user", '') -# p = request.POST.get("pwd", '') -# if u and p: -# stu = StudentInfo(stu_id=random.choice('0123456789'),stu_name=u, stu_pwd=p) -# stu.save() -# return HttpResponse("注册成功") -# else: -# return HttpResponse("请输入完整的账号和密码!") +@csrf_exempt # 可选,视具体情况决定是否使用 +def register_view(request): + if request.method == 'POST': + try: + data = json.loads(request.body) # 从请求体中加载 JSON 数据 + u = data.get("user", '') + p = data.get("pwd", '') + + if u and p: + stu = StudentInfo(stu_id=random.choice('0123456789'), stu_name=u, stu_pwd=p) + stu.save() + return JsonResponse({"message": "注册成功"}) + else: + return JsonResponse({"message": "请输入完整的账号和密码!"}, status=400) + except json.JSONDecodeError: + return JsonResponse({"message": "无效的 JSON 数据!"}, status=400) + + return JsonResponse({"message": "只支持 POST 请求!"}, status=405) -def register_view(request): - u = request.POST.get("user", '') - p = request.POST.get("pwd", '') - if u and p: - stu = StudentInfo(stu_id=random.choice('0123456789'), stu_name=u, stu_pwd=p) - stu.save() - return JsonResponse({"message": "注册成功"}) - else: - return JsonResponse({"message": "请输入完整的账号和密码!"}, status=400) - -#导入excel -# 上传学生名单的视图 -# def upload_students(request): -# if request.method == 'POST': -# form = UploadFileForm(request.POST, request.FILES) -# if form.is_valid(): -# # 读取上传的 Excel 文件 -# excel_file = request.FILES['file'] -# df = pd.read_excel(excel_file) # 使用 pandas 读取 Excel 文件 -# -# # 遍历 DataFrame,将每个学生保存到数据库 -# for _, row in df.iterrows(): -# student_id = row['student_id'] -# name = row['name'] -# Student.objects.get_or_create(student_id=student_id, name=name) # 如果学生已存在则不创建 -# -# return redirect('roll_call') # 完成后重定向到点名页面 -# else: -# form = UploadFileForm() # -# return render(request, 'upload_students.html', {'form': form}) # 渲染上传页面 +@csrf_exempt def upload_students(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) @@ -268,156 +63,116 @@ def upload_students(request): return render(request, 'upload_students.html', {'form': form}) # 确保在GET请求时返回HttpResponse -# return render(request, 'upload_students.html', {'form': form}) # 渲染上传页面 -# # 开始点名的视图 -# def roll_call(request): -# students = Student.objects.all() # 获取所有学生 -# selected_student = None # 初始化被选中的学生 -# -# # 当教师点击“开始点名”按钮时 -# if request.method == 'POST' and 'start_roll_call' in request.POST: -# # 设置权重:总分越高,被点名的概率越低 -# weights = [1 / (student.score + 1) for student in students] # 根据分数调整被点名概率 -# selected_student = random.choices(students, weights=weights, k=1)[0] # 随机选择一个学生 -# request.session['selected_student_id'] = selected_student.student_id # 存储被点名学生的ID到session中 -# return redirect('confirm_roll_call') # 跳转到确认点名页面 -# -# return render(request, 'roll_call.html', {'selected_student': selected_student}) # 渲染点名页面 + +@csrf_exempt # 如果需要,可以移除此装饰器,确保 CSRF 保护 def roll_call(request): students = Student.objects.all() # 获取所有学生 - selected_student = None # 初始化被选中的学生 # 当教师点击“开始点名”按钮时 - if request.method == 'POST' and 'start_roll_call' in request.POST: - # 设置权重:总分越高,被点名的概率越低 - weights = [1 / (student.score + 1) for student in students] # 根据分数调整被点名概率 - selected_student = random.choices(students, weights=weights, k=1)[0] # 随机选择一个学生 - request.session['selected_student_id'] = selected_student.student_id # 存储被点名学生的ID到session中 - return JsonResponse({"message": "点名成功", "student_id": selected_student.student_id, "student_name": selected_student.name}) - - return render(request, 'roll_call.html', {'selected_student': selected_student}) # 渲染点名页面 -# 确认点名的视图 -# def confirm_roll_call(request): -# # 从 session 中获取被点名的学生 -# student_id = request.session.get('selected_student_id') -# student = get_object_or_404(Student, student_id=student_id) -# -# if request.method == 'POST': -# # 学生是否到课 -# if 'attended' in request.POST: # 如果选择了到课 -# student.score += 1 # 到课加1分 -# student.attendance_count += 1 # 到课次数加1 -# -# # 处理是否准确重复问题 -# if request.POST['question_repeat'] == 'accurate': -# student.score += 0.5 # 重复问题准确,加0.5分 -# else: -# student.score -= 1 # 重复问题不准确,扣1分 -# -# # 处理回答问题的准确性(0-3分) -# answer_accuracy = float(request.POST.get('answer_accuracy', 0)) -# student.score += answer_accuracy # 根据回答准确性加分 -# else: -# student.score -= 5 # 未到课扣5分 -# -# student.save() # 保存更新后的学生信息 -# return redirect('roll_call') # 返回点名页面,进行下一轮点名 -# -# return render(request, 'confirm_roll_call.html', {'student': student}) # 渲染确认点名页面 -# def confirm_roll_call(request): -# student_id = request.session.get('selected_student_id') -# student = get_object_or_404(Student, student_id=student_id) -# protection_awarded = False # 初始化保护权标志 -# -# if request.method == 'POST': -# # 增加学生的被点名次数 -# student.called_count += 1 -# -# # 检查学生是否有“保护权” -# if student.called_count % 3 == 0: -# # 如果被点名次数是3的倍数,赋予保护权,自动加2分 -# student.score += 2 -# protection_awarded = True # 设置保护权标志 -# print(f"Student {student.name} ({student.student_id})获得保护权,自动加2分") -# else: -# # 学生是否到课 -# if 'attended' in request.POST: # 如果选择了到课 -# student.score += 1 # 到课加1分 -# student.attendance_count += 1 # 到课次数加1 -# -# # 处理是否准确重复问题 -# if request.POST['question_repeat'] == 'accurate': -# student.score += 0.5 # 重复问题准确,加0.5分 -# else: -# student.score -= 1 # 重复问题不准确,扣1分 -# -# # 处理回答问题的准确性(0-3分) -# answer_accuracy = float(request.POST.get('answer_accuracy', 0)) -# student.score += answer_accuracy # 根据回答准确性加分 -# else: -# student.score -= 5 # 未到课扣5分 -# -# # 保存更新后的学生信息 -# student.save() -# return redirect('roll_call') # 返回点名页面,进行下一轮点名 -# -# return render(request, 'confirm_roll_call.html', {'student': student, 'protection_awarded': protection_awarded}) # 渲染确认点名页面 -# -# -# -# -# # 积分排行榜 -# def leaderboard(request): -# students = Student.objects.all().order_by('-score') # 按分数降序排列 -# return render(request, 'leaderboard.html', {'students': students}) + if request.method == 'POST': + try: + data = json.loads(request.body) # 从请求中读取 JSON 数据 + if data.get('start_roll_call') == 'true': + # 设置权重:总分越高,被点名的概率越低 + weights = [1 / (student.score + 1) for student in students] # 根据分数调整被点名概率 + selected_student = random.choices(students, weights=weights, k=1)[0] # 随机选择一个学生 + request.session['selected_student_id'] = selected_student.student_id # 存储被点名学生的ID到session中 + return JsonResponse({"message": "点名成功", "student_id": selected_student.student_id, "student_name": selected_student.name}) + except json.JSONDecodeError: + return JsonResponse({"error": "无效的 JSON 数据"}, status=400) + + # 返回错误信息,如果不是 POST 请求或没有触发点名 + return JsonResponse({"error": "只支持 POST 请求"}, status=405) + +@csrf_exempt def confirm_roll_call(request): - student_id = request.session.get('selected_student_id') - student = get_object_or_404(Student, student_id=student_id) - protection_awarded = False # 初始化保护权标志 - if request.method == 'POST': - # 增加学生的被点名次数 - student.called_count += 1 - - # 检查学生是否有“保护权” - if student.called_count % 3 == 0: - student.score += 2 - protection_awarded = True # 设置保护权标志 - print(f"Student {student.name} ({student.student_id})获得保护权,自动加2分") - else: - # 学生是否到课 - if 'attended' in request.POST: # 如果选择了到课 - student.score += 1 # 到课加1分 - student.attendance_count += 1 # 到课次数加1 - - # 处理是否准确重复问题 - if request.POST['question_repeat'] == 'accurate': - student.score += 0.5 # 重复问题准确,加0.5分 - else: - student.score -= 1 # 重复问题不准确,扣1分 - - # 处理回答问题的准确性(0-3分) - answer_accuracy = float(request.POST.get('answer_accuracy', 0)) - student.score += answer_accuracy # 根据回答准确性加分 + try: + # 从请求体中解析 JSON 数据 + data = json.loads(request.body) + student_id = data.get('student_id') + + # 确保 student_id 存在 + if not student_id: + return JsonResponse({"message": "未找到被点名学生ID"}, status=400) + + # 获取学生对象,若不存在则返回404 + student = get_object_or_404(Student, student_id=student_id) + protection_awarded = False # 初始化保护权标志 + + # 增加学生的被点名次数 + student.called_count += 1 + + # 检查学生是否有“保护权” + if student.called_count % 3 == 0: + student.score += 2 + protection_awarded = True # 设置保护权标志 + print(f"Student {student.name} ({student.student_id})获得保护权,自动加2分") else: - student.score -= 5 # 未到课扣5分 + # 学生是否到课 + attended = data.get('attended', 'false').lower() == 'true' # 转为布尔值 + if attended: # 如果选择了到课 + student.score += 1 # 到课加1分 + student.attendance_count += 1 # 到课次数加1 + + # 处理是否准确重复问题 + if data.get('question_repeat') == 'accurate': + student.score += 0.5 # 重复问题准确,加0.5分 + else: + student.score -= 1 # 重复问题不准确,扣1分 + + # 处理回答问题的准确性(0-3分) + answer_accuracy = float(data.get('answer_accuracy', 0)) + student.score += answer_accuracy # 根据回答准确性加分 + else: + student.score -= 5 # 未到课扣5分 - # 保存更新后的学生信息 - student.save() + # 保存更新后的学生信息 + student.save() - return JsonResponse({ - "message": "确认点名成功", - "student_id": student.student_id, - "student_name": student.name, - "current_score": student.score, - "protection_awarded": protection_awarded - }) + return JsonResponse({ + "message": "确认点名成功", + "student_id": student.student_id, + "student_name": student.name, + "current_score": student.score, + "protection_awarded": protection_awarded + }) - return render(request, 'confirm_roll_call.html', - {'student': student, 'protection_awarded': protection_awarded}) # 渲染确认点名页面 + except json.JSONDecodeError: + return JsonResponse({"message": "请求体解析错误"}, status=400) + # GET 请求时渲染页面 + return JsonResponse({"message": "不支持的请求方法"}, status=405) +@csrf_exempt def leaderboard(request): students = Student.objects.all().order_by('-score') # 按分数降序排列 leaderboard_data = [{"student_id": student.student_id, "name": student.name, "score": student.score} for student in students] return JsonResponse({"leaderboard": leaderboard_data}) # 返回排行榜数据 + +# 111 + + +@csrf_exempt # If you're testing without CSRF protection +def toLogin_view(request): + return JsonResponse({"message": "请提供用户名和密码进行登录"}) + +@csrf_exempt +def Login_view(request): + if request.method == 'POST': + try: + data = json.loads(request.body) + u = data.get("user", '') + p = data.get("pwd", '') + + if u and p: + c = StudentInfo.objects.filter(stu_name=u, stu_pwd=p).count() + if c >= 1: + return JsonResponse({"message": "登录成功!"}) + else: + return JsonResponse({"message": "账号密码错误!"}, status=400) + else: + return JsonResponse({"message": "请输入正确的账号和密码!"}, status=400) + except json.JSONDecodeError: + return JsonResponse({"message": "无效的JSON数据!"}, status=400) + return JsonResponse({"message": "仅支持POST请求"}, status=405) \ No newline at end of file diff --git a/pythonProject03/mysite10_7/test/__init__.py b/pythonProject03/mysite10_7/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pythonProject03/mysite10_7/test/__pycache__/__init__.cpython-37.pyc b/pythonProject03/mysite10_7/test/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..7c6c00d88e8ccd96bd30172063444da104270c07 GIT binary patch literal 166 zcmZ?b<>g`kg3KSR=^*+sh=2h`Aj1KOi&=m~3PUi1CZpd