from django.shortcuts import render # Create your views here. from django.http import HttpRequest from django.http import JsonResponse from EduSystemServer.utils import ResponseUtil from .models import Student from course.models import SC, Course from django.views.decorators.csrf import csrf_exempt from django.http import QueryDict from io import BytesIO from django.http.multipartparser import MultiPartParser import json @csrf_exempt def studnets(request): if request.method == "POST": try: request_data = json.loads(request.body) print(request_data) 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') name = request.GET.get('name') sex = request.GET.get('sex') grade = request.GET.get('grade') class_name = request.GET.get('class_name') major = request.GET.get('major') data = [] if sid: filtered = Student.objects.filter(sid=sid) for student in filtered: data.append(student.to_dict()) elif name: filtered = Student.objects.filter(name=name) for student in filtered: data.append(student.to_dict()) elif sex: filtered = Student.objects.filter(sex=sex) for student in filtered: data.append(student.to_dict()) elif grade: filtered = Student.objects.filter(grade=grade) for student in filtered: data.append(student.to_dict()) elif class_name: filtered = Student.objects.filter(class_name=class_name) for student in filtered: data.append(student.to_dict()) elif major: filtered = Student.objects.filter(major=major) for student in filtered: data.append(student.to_dict()) else: all_objects = Student.objects.all() for student in all_objects: data.append(student.to_dict()) response = ResponseUtil.ok(data, "查询成功!") return JsonResponse(response) 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 = ResponseUtil.ok(info, "删除成功!") return JsonResponse(response) except: return JsonResponse(ResponseUtil.error("删除失败,未找到学生信息!")) elif request.method == "PUT": put = MultiPartParser(request.META, request, request.upload_handlers, request.encoding).parse() sid = put[0]['sid'] username = put[0]['username'] name = put[0]['name'] sex = put[0]['sex'] grade = put[0]['grade'] class_name = put[0]['class_name'] major = put[0]['major'] if Student.objects.filter(sid=sid): Student.objects.filter(sid=sid).update(username=username, name=name, sex=sex, grade=grade, class_name=class_name, major=major) else: return JsonResponse(ResponseUtil.error("没有查询到该同学!")) data = Student.objects.filter(sid=sid)[0].to_dict() return JsonResponse(ResponseUtil.ok(data, "修改成功!")) def search_grade(request): if request.method == "GET": sid = request.GET.get('sid') cid = SC.objects.filter(sid_id=sid).values() data_list = [] for i in range(len(cid)): c_name = Course.objects.filter(cid=cid[i]['cid_id']).values('name')[0]['name'] middle_grade = cid[i]['middle_grade'] end_grade = cid[i]['end_grade'] data = { 'c_name': c_name, '平时成绩': middle_grade, '最终成绩': end_grade } data_list.append(data) return JsonResponse(ResponseUtil.ok(data_list, "成绩查询成功!")) def get_select_course_by_id(request): pass