diff --git a/EduSystemServer/Eduadmin/urls.py b/EduSystemServer/Eduadmin/urls.py index f7d9603..1051814 100644 --- a/EduSystemServer/Eduadmin/urls.py +++ b/EduSystemServer/Eduadmin/urls.py @@ -10,5 +10,6 @@ from django.urls import path from .views import * urlpatterns = [ - path("teachers/", teacher_manage), + path("teacher/", teacher_manage), + path('course/',course_selection) ] \ No newline at end of file diff --git a/EduSystemServer/Eduadmin/views.py b/EduSystemServer/Eduadmin/views.py index f427141..28349f1 100644 --- a/EduSystemServer/Eduadmin/views.py +++ b/EduSystemServer/Eduadmin/views.py @@ -1,7 +1,7 @@ from django.shortcuts import render # Create your views here. -from django.http import JsonResponse +from django.http import JsonResponse, HttpResponseBadRequest, HttpResponse from teacher.models import Teacher from django.views.decorators.csrf import csrf_exempt from io import BytesIO @@ -12,7 +12,8 @@ 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 ast import literal_eval +from MySQLdb import IntegrityError from EduSystemServer.utils import ResponseUtil @csrf_exempt def teacher_manage(request): # 教师个人信息修改 @@ -26,17 +27,21 @@ def teacher_manage(request): # 教师个人信息修改 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) + try: + 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) + except: + response = ResponseUtil.error("老师信息插入未成功,用户名重复!") + return JsonResponse(response) else: response = ResponseUtil.error("老师信息插入未成功,存在空值,请你仔细检测并上传") return JsonResponse(response) @@ -68,18 +73,38 @@ def teacher_manage(request): # 教师个人信息修改 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) parser = MultiPartParser(request.META, BytesIO(request.body), request.upload_handlers, request.encoding) posdict = parser.parse() - print(posdict) - tid = int(posdict[0]['tid']) - print(tid) + # print(posdict) + tids = posdict[0]['tids'] + # print(tids) + tids = literal_eval(tids) + # print(tids) try: - info = Teacher.objects.filter(tid=tid).get().to_dict() - Teacher.objects.filter(tid=tid).delete() - response = ResponseUtil.ok(info, "老师信息删除成功") - return JsonResponse(response) + info = Teacher.objects.filter(tid__in=tids).values() + data = list(info) + if len(data) > 0: + Teacher.objects.filter(tid__in=tids).delete() + response = ResponseUtil.ok(data, "老师信息删除成功") + return JsonResponse(response) + else: + response = ResponseUtil.error("老师未找到") + return JsonResponse(response) except: - response = ResponseUtil.error("删除失败,未找到老师信息!") + response = ResponseUtil.ok("未查询到老师信息,删除失败") return JsonResponse(response) elif request.method == 'PUT': put = MultiPartParser(request.META, request, request.upload_handlers, request.encoding).parse() @@ -100,3 +125,92 @@ def teacher_manage(request): # 教师个人信息修改 else: response = ResponseUtil.error("修改信息有误!") return JsonResponse(response) + +@csrf_exempt +def course_selection(request): # 选课管理 + if request.method == 'POST': + # 假设需要将 tid 为 1 的老师赋值给某个课程的 tid 字段 + teacher_id = request.POST.get('tid') + name = request.POST.get('name') + type = request.POST.get('type') + credit = request.POST.get('credit') + if name and type and credit: + try: + teacher_id = int(teacher_id) + course = Course() + course.name = name + course.type = type + course.credit = int(credit) + course.tid = Teacher.objects.get(tid=teacher_id) # 将 Teacher 对象赋给 Course.tid 字段 + course.save() + response = ResponseUtil.ok(course.tid_dict(), "课程信息插入成功") + return JsonResponse(response) + except ObjectDoesNotExist: + response = ResponseUtil.error("找不到 id 为 " + str(teacher_id)+" 的老师") + return JsonResponse(response) + except ValueError: + response = ResponseUtil.error("插入数据失败: tid_id 列不能为 null") + return JsonResponse(response) + if request.method == 'GET': + cid = request.GET.get('cid') + name = request.GET.get('name') + type = request.GET.get('type') + credit = request.GET.get('credit') + tid = request.GET.get('tid') + # 定义空查询条件 + conditions = Q() + if cid: + conditions &= Q(cid=int(cid)) + if name: + conditions &= Q(name=name) + if type: + conditions &= Q(type=type) + if credit: + conditions &= Q(credit=credit) + if tid: + tid = int(tid) + conditions &= Q(tid=tid) + # 添加非空查询条件至基本查询集 + query = Course.objects.filter(conditions) + data = query.values() # 将 QuerySet 对象转换为字典列表 + data = list(data) # 转换为列表以便序列化为 JSON + print(query) + response = ResponseUtil.ok(data, "课程信息查询成功") + return JsonResponse(response) + if request.method == 'DELETE': + parser = MultiPartParser(request.META, BytesIO(request.body), request.upload_handlers, request.encoding) + posdict = parser.parse() + cids = posdict[0]['cids'] + cids = literal_eval(cids) + print(cids) + try: + info = Course.objects.filter(cid__in=cids).values() + data = list(info) + if len(data)>0: + Course.objects.filter(cid__in=cids).delete() + response = ResponseUtil.ok(data, "课程信息删除成功") + return JsonResponse(response) + else: + response = ResponseUtil.error("课程未找到") + return JsonResponse(response) + except: + response = ResponseUtil.ok("未查询到课程,删除失败") + return JsonResponse(response) + if request.method == 'PUT': + put = MultiPartParser(request.META, request, request.upload_handlers, request.encoding).parse() + # request.PUT = put[0] + print(put) + cid = put[0]['cid'] + name = put[0]['name'] + type = put[0]['type'] + credit = put[0]['credit'] + tid = put[0]['tid'] + if cid and name and type and credit and tid: + Course.objects.filter(cid=cid).update(name=name, type=type, credit=credit) + Course.objects.filter(cid=cid).update(tid=Teacher.objects.get(tid=tid)) + data = Course.objects.filter(cid=cid)[0].tid_dict() + response = ResponseUtil.ok(data, "课程信息修改成功") + return JsonResponse(response) + else: + response = ResponseUtil.error("课程信息修改失败,信息不全") + return JsonResponse(response) \ No newline at end of file diff --git a/EduSystemServer/teacher/views.py b/EduSystemServer/teacher/views.py index 5441a2f..06b953b 100644 --- a/EduSystemServer/teacher/views.py +++ b/EduSystemServer/teacher/views.py @@ -287,22 +287,27 @@ def student_performance_management(request): # 学生成绩管理 def course_selection(request): # 选课管理 if request.method == 'POST': # 假设需要将 tid 为 1 的老师赋值给某个课程的 tid 字段 - teacher_id = int(request.POST.get('tid')) - try: - course = Course() - course.name = request.POST.get('name') - course.type = request.POST.get('type') - course.credit = int(request.POST.get('credit')) - course.tid = Teacher.objects.get(tid=teacher_id) # 将 Teacher 对象赋给 Course.tid 字段 - course.save() - response = ResponseUtil.ok(course.tid_dict(), "课程信息插入成功") - return JsonResponse(response) - except ObjectDoesNotExist: - response = ResponseUtil.error("找不到 id 为 " + str(teacher_id)+" 的老师") - return JsonResponse(response) - except IntegrityError: - response = ResponseUtil.error("插入数据失败:tid_id 列不能为 null") - return JsonResponse(response) + teacher_id = request.POST.get('tid') + name = request.POST.get('name') + type = request.POST.get('type') + credit = request.POST.get('credit') + if name and type and credit: + try: + teacher_id = int(teacher_id) + course = Course() + course.name = name + course.type = type + course.credit = int(credit) + course.tid = Teacher.objects.get(tid=teacher_id) # 将 Teacher 对象赋给 Course.tid 字段 + course.save() + response = ResponseUtil.ok(course.tid_dict(), "课程信息插入成功") + return JsonResponse(response) + except ObjectDoesNotExist: + response = ResponseUtil.error("找不到 id 为 " + str(teacher_id)+" 的老师") + return JsonResponse(response) + except ValueError: + response = ResponseUtil.error("插入数据失败:tid_id 列不能为 null") + return JsonResponse(response) if request.method == 'GET': cid = request.GET.get('cid') name = request.GET.get('name')