|
|
|
from django.shortcuts import render
|
|
|
|
|
|
|
|
# Create your views here.
|
|
|
|
from django.http import JsonResponse
|
|
|
|
from Student.models import Student
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from io import BytesIO
|
|
|
|
from django.http.multipartparser import MultiPartParser
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def studnets(request):
|
|
|
|
if request.method == "POST":
|
|
|
|
student = Student()
|
|
|
|
student.s_username = request.POST.get("s_username")
|
|
|
|
student.s_password = request.POST.get("s_password")
|
|
|
|
student.s_name = request.POST.get("s_name") or ''
|
|
|
|
student.s_sex = request.POST.get("s_sex") or ''
|
|
|
|
student.s_grade = request.POST.get("s_grade") or ''
|
|
|
|
student.s_class_name = request.POST.get("s_class_name") or ''
|
|
|
|
student.s_major = request.POST.get("s_major") or ''
|
|
|
|
student.save()
|
|
|
|
response = {"code": 200, "message": "添加成功!", "data": student.to_dict()}
|
|
|
|
return JsonResponse(response)
|
|
|
|
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)
|