You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
4.9 KiB

from django.shortcuts import render
# Create your views here.
1 year ago
# Create your views here.
from django.http import JsonResponse
from .models import Teacher
from django.views.decorators.csrf import csrf_exempt
1 year ago
from io import BytesIO
from django.http.multipartparser import MultiPartParser
1 year ago
from django.http import QueryDict
1 year ago
from course.models import Course,SC
from Student.models import Student
1 year ago
import json
1 year ago
@csrf_exempt
def teacher(request):
if request.method == "POST":
teacher_information = Teacher()
1 year ago
teacher_information.name = request.POST.get('name')
teacher_information.sex = request.POST.get('sex')
teacher_information.title = request.POST.get('title')
teacher_information.education = request.POST.get('education')
teacher_information.dept = request.POST.get('dept')
1 year ago
teacher_information.save()
1 year ago
print(teacher_information.name)
1 year ago
return JsonResponse({'code': 200, 'msg': 'success',"data": teacher_information.to_dict()}, safe=False)
elif request.method == "GET":
1 year ago
name = request.GET.get('name')
sex = request.GET.get('sex')
title = request.GET.get('title')
education = request.GET.get('education')
dept = request.GET.get('dept')
1 year ago
data = []
1 year ago
if name:
filtered = Teacher.objects.filter(name=name)
1 year ago
for teacher_x in filtered:
data.append(teacher_x.to_dict())
1 year ago
elif sex:
filtered = Teacher.objects.filter(sex=sex)
1 year ago
for teacher_x in filtered:
data.append(teacher_x.to_dict())
1 year ago
elif title:
filtered = Teacher.objects.filter(title=title)
1 year ago
for teacher_x in filtered:
data.append(teacher_x.to_dict())
1 year ago
elif education:
filtered = Teacher.objects.filter(education=education)
1 year ago
for teacher_x in filtered:
data.append(teacher_x.to_dict())
1 year ago
elif dept:
filtered = Teacher.objects.filter(dept=dept)
1 year ago
for teacher_x in filtered:
data.append(teacher_x.to_dict())
else:
all_objects = Teacher.objects.all()
for teacher_x in all_objects:
data.append(teacher_x.to_dict())
1 year ago
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)
1 year ago
posdict = parser.parse()
print(posdict)
tid = int(posdict[0]['tid'])
1 year ago
print(tid)
try:
info = Teacher.objects.filter(tid=tid).get().to_dict()
Teacher.objects.filter(tid=tid).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()
# request.PUT = put[0]
print(put)
tid = put[0]['tid']
1 year ago
name = put[0]['name']
sex = put[0]['sex']
title = put[0]['title']
education = put[0]['education']
dept = put[0]['dept']
Teacher.objects.filter(tid=tid).update(name=name)
Teacher.objects.filter(tid=tid).update(sex=sex)
Teacher.objects.filter(tid=tid).update(title=title)
Teacher.objects.filter(tid=tid).update(education=education)
Teacher.objects.filter(tid=tid).update(dept=dept)
1 year ago
data = Teacher.objects.filter(tid=tid)[0].to_dict()
return JsonResponse({'code': 200, 'msg': 'success','data':data}, safe=False)
def delete(request):
tid = request.GET.get('tid')
1 year ago
try:
Teacher.objects.filter(tid = tid).delete()
return JsonResponse({'code': 200, 'msg': 'success'}, safe=False)
except:
return JsonResponse({'code': 200, 'msg': '删除失败'}, safe=False)
1 year ago
1 year ago
@csrf_exempt
def teaching_resources(request):
pass
@csrf_exempt
def student_performance_management(request):
if request.method == "POST":
sid = request.POST.get('sid')
cid = request.POST.get('cid')
middle_grade = float(request.POST.get('middle_grade'))
end_grade = float(request.POST.get('end_grade'))
if middle_grade <= 100 and middle_grade >= 0:
sc = SC()
sid = Student.objects.get(sid=sid)
print(sid)
cid = Course.objects.get(cid=cid)
sc.sid = sid
sc.cid = cid
sc.middle_grade = middle_grade
sc.end_grade=end_grade
sc.save()
return JsonResponse({'code': 200, 'msg': 'success'}, safe=False)
else:
return JsonResponse({'code': 200, 'msg': '成绩输入有误'}, safe=False)