from django.shortcuts import render # Create your views here. from django.http import JsonResponse from teacher.models import Teacher from django.views.decorators.csrf import csrf_exempt from io import BytesIO from django.http.multipartparser import MultiPartParser from django.http import QueryDict from course.models import Course, SC from Student.models import Student from django.db.models import Q from django.db.models import Subquery, OuterRef from django.core.exceptions import ObjectDoesNotExist from EduSystemServer.utils import ResponseUtil @csrf_exempt def teacher_manage(request): # 教师个人信息修改 if request.method == "POST": username = request.POST.get('username') password = request.POST.get('password') name = request.POST.get('name') sex = request.POST.get('sex') title = request.POST.get('title') education = request.POST.get('education') dept = request.POST.get('dept') if (username and password and name and sex and title and education and dept): teacher = Teacher() teacher.username = username teacher.password = password teacher.name = name teacher.sex = sex teacher.title = title teacher.education = education teacher.dept = dept teacher.save() response = ResponseUtil.ok(teacher.to_dict(), "老师信息插入成功") return JsonResponse(response) else: response = ResponseUtil.error("老师信息插入未成功,存在空值,请你仔细检测并上传") return JsonResponse(response) elif request.method == "GET": 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') # 定义空查询条件 conditions = Q() if name: conditions &= Q(name=name) if sex: conditions &= Q(sex=sex) if title: conditions &= Q(title=title) if education: conditions &= Q(education=education) if dept: conditions &= Q(dept=dept) # 添加非空查询条件至基本查询集 query = Teacher.objects.filter(conditions) data = query.values() # 将 QuerySet 对象转换为字典列表 data = list(data) # 转换为列表以便序列化为 JSON print(query) response = ResponseUtil.ok(data, "老师信息查询成功") return JsonResponse(response) elif request.method == "DELETE": parser = MultiPartParser(request.META, BytesIO(request.body), request.upload_handlers, request.encoding) posdict = parser.parse() print(posdict) tid = int(posdict[0]['tid']) print(tid) try: info = Teacher.objects.filter(tid=tid).get().to_dict() Teacher.objects.filter(tid=tid).delete() response = ResponseUtil.ok(info, "老师信息删除成功") return JsonResponse(response) except: response = ResponseUtil.error("删除失败,未找到老师信息!") 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'] name = put[0]['name'] sex = put[0]['sex'] title = put[0]['title'] education = put[0]['education'] dept = put[0]['dept'] if name and sex and title and education and dept: Teacher.objects.filter(tid=tid).update(name=name,sex=sex,title=title) Teacher.objects.filter(tid=tid).update(education=education,dept=dept) data = Teacher.objects.filter(tid=tid)[0].to_dict() response = ResponseUtil.ok(data, "修改老师信息成功!") return JsonResponse(response) else: response = ResponseUtil.error("修改信息有误!") return JsonResponse(response)