|
|
|
import json
|
|
|
|
|
|
|
|
from django.core import serializers
|
|
|
|
from django.shortcuts import render
|
|
|
|
|
|
|
|
# Create your views here.
|
|
|
|
from django.http import JsonResponse
|
|
|
|
|
|
|
|
from course.models import SC, Course
|
|
|
|
from .models import Student
|
|
|
|
|
|
|
|
from EduSystemServer.utils import *
|
|
|
|
from Student.models import Student
|
|
|
|
from .models import Student
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from django.http import QueryDict
|
|
|
|
from io import BytesIO
|
|
|
|
from django.http.multipartparser import MultiPartParser
|
|
|
|
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def studnets(request):
|
|
|
|
if request.method == "POST":
|
|
|
|
try:
|
|
|
|
request_data = request.POST
|
|
|
|
student = Student.objects.filter(sid=request_data.get("sid")).first()
|
|
|
|
student.username = request_data.get("username")
|
|
|
|
student.password = request_data.get("password")
|
|
|
|
student.name = request_data.get("name")
|
|
|
|
student.sex = request_data.get("sex")
|
|
|
|
student.grade = request_data.get("grade")
|
|
|
|
student.class_name = request_data.get("class_name")
|
|
|
|
student.major = request_data.get("major")
|
|
|
|
student.save()
|
|
|
|
response = ResponseUtil.ok(student.to_dict(), "更新成功!")
|
|
|
|
return JsonResponse(response)
|
|
|
|
except Exception as E:
|
|
|
|
return JsonResponse(ResponseUtil.error(E))
|
|
|
|
elif request.method == "GET":
|
|
|
|
sid = request.GET.get('sid')
|
|
|
|
s_name = request.GET.get('s_name')
|
|
|
|
s_sex = request.GET.get('s_sex')
|
|
|
|
s_grade = request.GET.get('s_grade')
|
|
|
|
s_class_name = request.GET.get('s_class_name')
|
|
|
|
s_major = request.GET.get('s_major')
|
|
|
|
data = []
|
|
|
|
if sid:
|
|
|
|
filtered = Student.objects.filter(sid=sid)
|
|
|
|
for student in filtered:
|
|
|
|
data.append(student.to_dict())
|
|
|
|
elif s_name:
|
|
|
|
filtered = Student.objects.filter(s_name=s_name)
|
|
|
|
for student in filtered:
|
|
|
|
data.append(student.to_dict())
|
|
|
|
elif s_sex:
|
|
|
|
filtered = Student.objects.filter(s_sex=s_sex)
|
|
|
|
for student in filtered:
|
|
|
|
data.append(student.to_dict())
|
|
|
|
elif s_grade:
|
|
|
|
filtered = Student.objects.filter(s_grade=s_grade)
|
|
|
|
for student in filtered:
|
|
|
|
data.append(student.to_dict())
|
|
|
|
elif s_class_name:
|
|
|
|
filtered = Student.objects.filter(s_class_name=s_class_name)
|
|
|
|
for student in filtered:
|
|
|
|
data.append(student.to_dict())
|
|
|
|
elif s_major:
|
|
|
|
filtered = Student.objects.filter(s_major=s_major)
|
|
|
|
for student in filtered:
|
|
|
|
data.append(student.to_dict())
|
|
|
|
else:
|
|
|
|
all_objects = Student.objects.all()
|
|
|
|
for studnet in all_objects:
|
|
|
|
data.append(studnet.to_dict())
|
|
|
|
return JsonResponse({'code': 200, 'msg': 'success', 'data': data}, safe=False)
|
|
|
|
elif request.method == "DELETE":
|
|
|
|
parser = MultiPartParser(request.META, BytesIO(request.body), request.upload_handlers, request.encoding)
|
|
|
|
post_dict = parser.parse()
|
|
|
|
sid = post_dict[0]['sid']
|
|
|
|
try:
|
|
|
|
info = Student.objects.filter(sid=sid).get().to_dict()
|
|
|
|
Student.objects.filter(sid=sid).delete()
|
|
|
|
response = {"code": 200, "message": "删除成功!", "data": info}
|
|
|
|
return JsonResponse(response)
|
|
|
|
except:
|
|
|
|
response = {"code": 200, "message": "删除失败,未找到学生信息!"}
|
|
|
|
return JsonResponse(response)
|
|
|
|
elif request.method == "PUT":
|
|
|
|
put = MultiPartParser(request.META, request, request.upload_handlers, request.encoding).parse()
|
|
|
|
sid = put[0]['sid']
|
|
|
|
s_username = put[0]['s_username']
|
|
|
|
s_name = put[0]['s_name']
|
|
|
|
s_sex = put[0]['s_sex']
|
|
|
|
s_grade = put[0]['s_grade']
|
|
|
|
s_class_name = put[0]['s_class_name']
|
|
|
|
s_major = put[0]['s_major']
|
|
|
|
try:
|
|
|
|
Student.objects.filter(sid=sid).update(s_username=s_username, s_name=s_name, s_sex=s_sex, s_grade=s_grade,
|
|
|
|
s_class_name=s_class_name,
|
|
|
|
s_major=s_major)
|
|
|
|
except:
|
|
|
|
Student.objects.create(sid=sid, s_username=s_username, s_name=s_name, s_sex=s_sex, s_grade=s_grade,
|
|
|
|
s_class_name=s_class_name,
|
|
|
|
s_major=s_major)
|
|
|
|
data = Student.objects.filter(sid=sid)[0].to_dict()
|
|
|
|
return JsonResponse({'code': 200, 'msg': 'success', 'data': data}, safe=False)
|
|
|
|
student_list = Student.objects.all()
|
|
|
|
response_json = {"code": 200, "message": "success", "data": []}
|
|
|
|
for student in student_list:
|
|
|
|
response_json["data"].append(student.to_dict())
|
|
|
|
return JsonResponse(response_json)
|
|
|
|
elif request.method == 'DELETE':
|
|
|
|
params = QueryDict(request.body)
|
|
|
|
key = next(iter(params.keys())) # 获取键名
|
|
|
|
value = params.get(key) # 获取值
|
|
|
|
xx = str(value).split('-')[0]
|
|
|
|
print(xx.split('\r\n'))
|
|
|
|
return JsonResponse({'code': 200, 'msg': 'success'}, safe=False)
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
@teacher_required
|
|
|
|
@admin_required
|
|
|
|
def add_student(request):
|
|
|
|
if not request.method == "POST":
|
|
|
|
return JsonResponse(ResponseUtil.error("request method error!"))
|
|
|
|
try:
|
|
|
|
request_data = request.POST
|
|
|
|
student = Student()
|
|
|
|
student.username = request_data.get("username")
|
|
|
|
student.password = request_data.get("password")
|
|
|
|
student.name = request_data.get("name")
|
|
|
|
student.sex = request_data.get("sex")
|
|
|
|
student.grade = request_data.get("grade")
|
|
|
|
student.class_name = request_data.get("class_name")
|
|
|
|
student.major = request_data.get("major")
|
|
|
|
student.save()
|
|
|
|
result = ResponseUtil.ok(student.to_dict(), "添加成功!")
|
|
|
|
except Exception as E:
|
|
|
|
result = ResponseUtil.error(E)
|
|
|
|
return JsonResponse(result)
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
@teacher_required
|
|
|
|
@admin_required
|
|
|
|
def search_student(request):
|
|
|
|
currentPage = request.GET.get("currentPage")
|
|
|
|
pageSize = request.GET.get("pageSize")
|
|
|
|
request_data = request.POST
|
|
|
|
search_students = Student.objects.all()
|
|
|
|
if not request_data.get("sid") == "":
|
|
|
|
search_students =search_students.filter(sid=request_data.get("sid"))
|
|
|
|
if not request_data.get("name") == "":
|
|
|
|
search_students =search_students.filter(name__contains=request_data.get("name"))
|
|
|
|
if not request_data.get("sex") == "":
|
|
|
|
search_students =search_students.filter(sex=request_data.get("sex"))
|
|
|
|
if not request_data.get("class_name") == "":
|
|
|
|
search_students =search_students.filter(class_name__contains=request_data.get("class_name"))
|
|
|
|
if not request_data.get("major") == "":
|
|
|
|
search_students =search_students.filter(major__contains=request_data.get("major"))
|
|
|
|
paginator = Paginator(search_students, pageSize)
|
|
|
|
try:
|
|
|
|
students = paginator.page(currentPage).object_list
|
|
|
|
except PageNotAnInteger:
|
|
|
|
students = paginator.page(1)
|
|
|
|
except EmptyPage:
|
|
|
|
students = paginator.page(paginator.num_pages).object_list
|
|
|
|
result = ResponseUtil.ok(json.loads(serializers.serialize("json", students)), "success!")
|
|
|
|
result["pageTotal"] = paginator.count
|
|
|
|
result["pageNum"] = paginator.num_pages
|
|
|
|
return JsonResponse(result)
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
@teacher_required
|
|
|
|
@admin_required
|
|
|
|
def del_student(request):
|
|
|
|
if not request.method == "GET":
|
|
|
|
return JsonResponse(ResponseUtil.error("request method error!"))
|
|
|
|
try:
|
|
|
|
sid = request.GET.get("sid")
|
|
|
|
student = Student.objects.filter(sid=sid).first()
|
|
|
|
student.delete()
|
|
|
|
result = ResponseUtil.ok(None, "删除成功!")
|
|
|
|
except Exception as E:
|
|
|
|
result = ResponseUtil.error(E)
|
|
|
|
return JsonResponse(result)
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
@student_required
|
|
|
|
def select_course(request):
|
|
|
|
if not request.method == "POST":
|
|
|
|
return JsonResponse(ResponseUtil.error("request method error!"))
|
|
|
|
try:
|
|
|
|
request_data = request.POST
|
|
|
|
cid = request_data.get("cid")
|
|
|
|
username = request_data.get("username")
|
|
|
|
student = Student.objects.filter(username=username).first()
|
|
|
|
if SC.objects.filter(cid=cid, sid=student.sid).exists():
|
|
|
|
return JsonResponse(ResponseUtil.error("该课程已经选择!"))
|
|
|
|
sc = SC()
|
|
|
|
sc.sid = student
|
|
|
|
sc.cid = Course.objects.filter(cid=cid).first()
|
|
|
|
sc.middle_grade = 0
|
|
|
|
sc.end_grade = 0
|
|
|
|
sc.save()
|
|
|
|
return JsonResponse(ResponseUtil.ok(None, "选课成功!"))
|
|
|
|
except Exception as E:
|
|
|
|
print(E)
|
|
|
|
return JsonResponse(ResponseUtil.error(str(E)))
|
|
|
|
|
|
|
|
|
|
|
|
@student_required
|
|
|
|
def get_grade(request):
|
|
|
|
"""
|
|
|
|
获取学生成绩
|
|
|
|
"""
|
|
|
|
if not request.method == "GET":
|
|
|
|
return ResponseUtil.error("request method error!")
|
|
|
|
try:
|
|
|
|
username = request.jwt_payload.get("username")
|
|
|
|
student = Student.objects.filter(username=username).first()
|
|
|
|
grade__all = SC.objects.filter(sid=student.sid).values("sid", "sid__name", "cid__name", "cid__type", "cid__credit",
|
|
|
|
"cid__tid__name", "middle_grade", "end_grade").all()
|
|
|
|
grades = []
|
|
|
|
for grade in grade__all:
|
|
|
|
grades.append(grade)
|
|
|
|
return JsonResponse(ResponseUtil.ok(grades))
|
|
|
|
except Exception as E:
|
|
|
|
return JsonResponse(ResponseUtil.error(E))
|