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.
216 lines
9.1 KiB
216 lines
9.1 KiB
from django.shortcuts import render
|
|
|
|
# Create your views here.
|
|
from django.http import JsonResponse, HttpResponseBadRequest, HttpResponse
|
|
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 ast import literal_eval
|
|
from MySQLdb import IntegrityError
|
|
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):
|
|
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)
|
|
|
|
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)
|
|
parser = MultiPartParser(request.META, BytesIO(request.body), request.upload_handlers, request.encoding)
|
|
posdict = parser.parse()
|
|
# print(posdict)
|
|
tids = posdict[0]['tids']
|
|
# print(tids)
|
|
tids = literal_eval(tids)
|
|
# print(tids)
|
|
try:
|
|
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.ok("未查询到老师信息,删除失败")
|
|
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)
|
|
|
|
@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) |