from django.shortcuts import render from django.http import HttpResponse, JsonResponse from user.models import user from user.models import student import pandas as pd import random # Create your views here. def loginpage(request): return render(request, 'login.html') def Loginadmit(request): if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') print(username,password) if user.objects.filter(username=username).exists(): if user.objects.filter(username=username,password=password).exists(): request.session["info"]=username return render(request, 'firstpage.html') else: return HttpResponse("密码错误") else: return HttpResponse("用户名不存在") def Register(request): sign=0 if request.method == 'POST':#判断是否登录 username = request.POST.get('username') password = request.POST.get('password') qs = user.objects.values if user.objects.filter(username=username).exists(): return HttpResponse("用户名已存在") else: user.objects.create(username=username,password=password) return render(request, 'login.html',{'success':'注册成功'}) # 第一页 def firstpage(request): if request.session.get("info"):#判断是否登录 return render(request, 'firstpage.html') else: return render(request, 'login.html') #随机点名 def dianming(request): if request.session.get("info"):#判断是否登录 User=user.objects.filter(username=request.session.get("info")).first()#获取当前用户 studentlist=student.objects.filter(teather_id=User.id)#获取当前用户的学生表 if len(studentlist)==0:#判断学生表是否为空 return render(request, 'firstpage.html',{'form':'暂无学生'}) #权重随机点名算法 form=weights_random_choice(studentlist) #随机事件 random_number = random.randint(1, 11)#随机数 if random_number ==5:#触发事件双倍扣分 fair='zhongkou' return render(request,'firstpage.html',{'form':form.student_name,'sign':fair}) elif random_number ==6:#触发事件可以指定同学帮忙回答 fair='lucky' return render(request,'firstpage.html',{'form':form.student_name,'sign':fair}) else: return render(request,'firstpage.html',{'form':form.student_name,'sign':'none'}) else: return render(request, 'login.html') #权重随机点名算法 def weights_random_choice(studentlist): # 偏移所有分数 offset = max(1, abs(min(student.student_score for student in studentlist)))#计算最小值的绝对值 adjusted_points = [student.student_score + offset for student in studentlist]#添加偏移 # 计算逆向权重 inverse_weights = [1 / (point+1) for point in adjusted_points] # 归一化逆向权重 total_inverse_weight = sum(inverse_weights) normalized_inverse_weights = [w / total_inverse_weight for w in inverse_weights] # 选择一个学生 selected_student = random.choices(studentlist, weights=normalized_inverse_weights, k=1)[0] print(selected_student) return selected_student # 分数写入 def submitscore(request): teather=request.session.get("info")#获取当前用户 if teather:#判断是否登录 if request.method == 'POST': print(request.POST) User=user.objects.filter(username=teather).first()#获取当前用户的id STdname=request.POST.get('student_name','')#获取被点到的学生姓名 print(STdname) STudent=student.objects.filter(teather_id=User.id,student_name=STdname ).first()#获取被点到的学生 print(request.POST) #{'student_name': ['才需困'], 'daoda': ['1'], 'score1': ['1'], 'score2': ['0.5']}> daoda=request.POST.get('daoda','') score1=request.POST.get('score1','') score2=request.POST.get('score2','') if daoda=='1':#到课堂加1分 STudent.student_score+=1 elif daoda=='2':#没到扣1分 STudent.student_score-=1 elif daoda=='22':#没到扣2分(双倍扣分) STudent.student_score-=2 #重复问题 if score1=='1':#重复问题加0.5分 STudent.student_score+=0.5 elif score1=='2':#重复不了问题扣1分 STudent.student_score-=1 elif score1=='22':#重复不了问题扣2分(双倍扣分) STudent.student_score-=2 #回答问题 STudent.student_score+=float(score2) print(STudent.student_score) #写入数据库 STudent.save() return render(request, 'firstpage.html') else: return render(request, 'login.html') # 第二页 def secondpage(request): teather=request.session.get("info")#获取用户信息 if teather:#判断是否登录 User=user.objects.filter(username=teather).first()#获取用户 form=student.objects.filter(teather_id=User.id) return render(request, 'secondpage.html',{'form':form})#返回学生数据 else: return render(request, 'login.html') # 第三页 def thirdpage(request): teather=request.session.get("info")#获取用户session信息 if teather: User=user.objects.filter(username=teather).first()#获取当前用户的id form=student.objects.filter(teather_id=User.id).order_by('-student_score') return render(request, 'thirdpage.html',{"form":form}) else: return render(request, 'login.html') #上传Excel文件 def upload_excel(request): if request.session.get("info"): return render(request, 'upload.html') else: return render(request, 'login.html') #接收文件 def upload_file(request): teather=request.session.get("info")#获取session中的用户名 if teather: User=user.objects.filter(username=teather).first()#获取用户 if request.method == 'POST': excel_file = request.FILES.get('file')#获取excel表格文件 df=pd.read_excel(excel_file)#读取excel表格 for index, row in df.iterrows(): if student.objects.filter(student_xuehao=row['学号'],teather_id=User.id).exists():#判断是否已经存在当前老师的学生表中 continue student.objects.create(student_xuehao=row['学号'],student_name=row['姓名'],student_major=row['专业'],student_class=row['班级'],teather_id=User.id)#写入学生表 form=student.objects.filter(teather_id=User.id)#返回当前老师的学生表 return render(request, 'secondpage.html',{'form':form}) else: return render(request, 'login.html') #删除表格 def delete_excel(request): teather=request.session.get("info")#获取session中的用户名 if teather: User=user.objects.filter(username=teather).first()#获取用户 print("删除当前老师的所有学生") student.objects.filter(teather_id=User.id).delete() return render(request, 'secondpage.html') else: return render(request, 'login.html') #第四页 def fourthpage(request): teather=request.session.get("info")#获取当前用户 if teather:#判断是否登录 User=user.objects.filter(username=teather).first()#获取当前用户的id return render(request, 'fourthpage.html',{'name':User.username}) else: return render(request, 'login.html') #账号和密码修改 def xiugai(request): teather=request.session.get("info")#获取当前用户名 if teather:#判断是否登录 User=user.objects.filter(username=teather).first()#获取当前用户 if request.method == 'POST': username=request.POST.get('username','') password=request.POST.get('password','') print(username,password) if username=='' and password=='': return render(request, 'fourthpage.html',{'name':User.username}) else: if username!='': User.username=username if password!='': User.password=password User.save() return render(request, 'login.html') else: return render(request, 'login.html') #账户注销 def zhuxiao(request): teather=request.session.get("info")#获取当前用户名 if teather:#判断是否登录 User=user.objects.filter(username=teather).first()#获取当前用户 if request.method == 'POST': User.delete() return render(request, 'login.html') else: return render(request, 'login.html')