|
|
|
@ -1,3 +1,4 @@
|
|
|
|
|
from MySQLdb import IntegrityError
|
|
|
|
|
from django.shortcuts import render
|
|
|
|
|
|
|
|
|
|
# Create your views here.
|
|
|
|
@ -12,13 +13,14 @@ from django.http import QueryDict
|
|
|
|
|
from course.models import Course, SC
|
|
|
|
|
from Student.models import Student
|
|
|
|
|
from django.db.models import Q
|
|
|
|
|
import json
|
|
|
|
|
from django.db.models import Subquery, OuterRef
|
|
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
|
|
|
|
|
|
|
|
from EduSystemServer.utils import ResponseUtil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
|
def teacher(request):
|
|
|
|
|
def teacher(request): # 教师个人信息修改
|
|
|
|
|
if request.method == "POST":
|
|
|
|
|
teacher_information = Teacher()
|
|
|
|
|
teacher_information.username = request.POST.get('username')
|
|
|
|
@ -38,22 +40,23 @@ def teacher(request):
|
|
|
|
|
title = request.GET.get('title')
|
|
|
|
|
education = request.GET.get('education')
|
|
|
|
|
dept = request.GET.get('dept')
|
|
|
|
|
data = []
|
|
|
|
|
# 定义空查询条件
|
|
|
|
|
conditions = Q()
|
|
|
|
|
if name:
|
|
|
|
|
data.append(Teacher.objects.filter(name=name))
|
|
|
|
|
elif sex:
|
|
|
|
|
data.append(Teacher.objects.filter(sex=sex))
|
|
|
|
|
elif title:
|
|
|
|
|
data.append(Teacher.objects.filter(title=title))
|
|
|
|
|
elif education:
|
|
|
|
|
data.append(Teacher.objects.filter(education=education))
|
|
|
|
|
elif dept:
|
|
|
|
|
data.append(Teacher.objects.filter(dept=dept))
|
|
|
|
|
else:
|
|
|
|
|
data.append(Teacher.objects.all())
|
|
|
|
|
data_x = []
|
|
|
|
|
for teacher_x in data[0]:
|
|
|
|
|
data_x.append(teacher_x.to_dict())
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
@ -95,44 +98,77 @@ def delete(request):
|
|
|
|
|
return JsonResponse({'code': 200, 'msg': '删除失败'}, safe=False)
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
|
def teaching_resources(request):
|
|
|
|
|
def teaching_resources(request): # 课程程序
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
cid = request.POST.get('cid')
|
|
|
|
|
cid = int(request.POST.get('cid'))
|
|
|
|
|
name = request.POST.get('name')
|
|
|
|
|
type = request.POST.get('type')
|
|
|
|
|
credit = request.POST.get('credit')
|
|
|
|
|
tid = request.POST.get('tid')
|
|
|
|
|
t_name = request.POST.get('t_name')
|
|
|
|
|
# 定义空查询条件
|
|
|
|
|
conditions = Q()
|
|
|
|
|
t_conditions = Q()
|
|
|
|
|
tid = int(request.POST.get('tid'))
|
|
|
|
|
teacher_name = request.POST.get('t_name')
|
|
|
|
|
# 定义空子字典查询条件
|
|
|
|
|
conditions = {}
|
|
|
|
|
if cid:
|
|
|
|
|
conditions &= Q(cid=cid)
|
|
|
|
|
conditions.update({'cid': cid}) # 追加信息
|
|
|
|
|
if name:
|
|
|
|
|
conditions &= Q(name=name)
|
|
|
|
|
conditions.update({'name': cid})
|
|
|
|
|
if type:
|
|
|
|
|
conditions &= Q(type=type)
|
|
|
|
|
conditions.update({'type': cid})
|
|
|
|
|
if credit:
|
|
|
|
|
conditions &= Q(credit=credit)
|
|
|
|
|
conditions.update({'credit': cid})
|
|
|
|
|
if tid:
|
|
|
|
|
t_conditions &= Q(tid=tid)
|
|
|
|
|
if t_name:
|
|
|
|
|
t_conditions &= Q(t_name=t_name)
|
|
|
|
|
# 添加非空查询条件至基本查询集
|
|
|
|
|
query1 = Course.objects.filter(conditions)
|
|
|
|
|
query2 = Teacher.objects.filter(t_conditions)
|
|
|
|
|
conditions.update({'tid': cid})
|
|
|
|
|
if teacher_name:
|
|
|
|
|
conditions.update({'teacher_name': teacher_name})
|
|
|
|
|
def check_subdict_exists(dictionary, subdict): # 判断子字典是否存在字典
|
|
|
|
|
for key, value in subdict.items():
|
|
|
|
|
if key not in dictionary or dictionary[key] != value:
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
# 获取第一个查询表的数据
|
|
|
|
|
table1_result = Teacher.objects.values('tid', 'name', 'title', 'dept')
|
|
|
|
|
# 获取第二个查询表的数据
|
|
|
|
|
table2_result = Course.objects.values('tid', 'cid', 'name', 'type', 'credit')
|
|
|
|
|
# 构建子查询,获取第一个查询表的结果并注入到第二个查询表中
|
|
|
|
|
table2_result = table2_result.annotate(teacher_name=Subquery(
|
|
|
|
|
table1_result.filter(tid=OuterRef('tid')).values('name')[:1]
|
|
|
|
|
), teacher_title=Subquery(
|
|
|
|
|
table1_result.filter(tid=OuterRef('tid')).values('title')[:1]
|
|
|
|
|
), teacher_dept=Subquery(
|
|
|
|
|
table1_result.filter(tid=OuterRef('tid')).values('dept')[:1]
|
|
|
|
|
))
|
|
|
|
|
# 合并两个查询表的结果
|
|
|
|
|
merged_result =list(table2_result)
|
|
|
|
|
combined_data = []
|
|
|
|
|
for item in query2:
|
|
|
|
|
matching_items = query1.filter(tid=item.tid).values()
|
|
|
|
|
combined_data.extend(matching_items)
|
|
|
|
|
print(combined_data)
|
|
|
|
|
response = ResponseUtil.ok("课程信息查询成功")
|
|
|
|
|
print(conditions)
|
|
|
|
|
# 输出合并结果
|
|
|
|
|
for item in merged_result:
|
|
|
|
|
# 判断子字典是否存在于字典中
|
|
|
|
|
print(item)
|
|
|
|
|
if check_subdict_exists(item, conditions):
|
|
|
|
|
combined_data.append(item)
|
|
|
|
|
response = ResponseUtil.ok(combined_data, "课程信息查询成功")
|
|
|
|
|
return JsonResponse(response)
|
|
|
|
|
|
|
|
|
|
elif request.method == "GET":
|
|
|
|
|
pass
|
|
|
|
|
# 获取第一个查询表的数据
|
|
|
|
|
table1_result = Teacher.objects.values('tid', 'name', 'title', 'dept')
|
|
|
|
|
# 获取第二个查询表的数据
|
|
|
|
|
table2_result = Course.objects.values('tid', 'cid', 'name', 'type', 'credit')
|
|
|
|
|
# 构建子查询,获取第一个查询表的结果并注入到第二个查询表中
|
|
|
|
|
table2_result = table2_result.annotate(teacher_name=Subquery(
|
|
|
|
|
table1_result.filter(tid=OuterRef('tid')).values('name')[:1]
|
|
|
|
|
), teacher_title=Subquery(
|
|
|
|
|
table1_result.filter(tid=OuterRef('tid')).values('title')[:1]
|
|
|
|
|
), teacher_dept=Subquery(
|
|
|
|
|
table1_result.filter(tid=OuterRef('tid')).values('dept')[:1]
|
|
|
|
|
))
|
|
|
|
|
data = list(table2_result) # 转换为列表以便序列化为 JSON
|
|
|
|
|
response = ResponseUtil.ok(data, "课程信息查询成功")
|
|
|
|
|
return JsonResponse(response)
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
|
def query_student_information(request):
|
|
|
|
|
def query_student_information(request): # 学生信息查询
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
name = request.POST.get('name')
|
|
|
|
|
sid = request.POST.get('sid')
|
|
|
|
@ -246,3 +282,56 @@ def student_performance_management(request): # 学生成绩管理
|
|
|
|
|
data = list(data) # 转换为列表以便序列化为 JSON
|
|
|
|
|
response = ResponseUtil.ok(data, "学生成绩修改成功")
|
|
|
|
|
return JsonResponse(response)
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
|
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)
|
|
|
|
|
if request.method == 'DELETE':
|
|
|
|
|
parser = MultiPartParser(request.META, BytesIO(request.body), request.upload_handlers, request.encoding)
|
|
|
|
|
posdict = parser.parse()
|
|
|
|
|
print(posdict)
|
|
|
|
|
cid = int(posdict[0]['cid'])
|
|
|
|
|
print(cid)
|
|
|
|
|
try:
|
|
|
|
|
data = Course.objects.filter(cid=cid).get()
|
|
|
|
|
Course.objects.filter(cid=cid).delete()
|
|
|
|
|
response = ResponseUtil.ok(data.tid_dict, "删除课程成功!")
|
|
|
|
|
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)
|
|
|
|
|