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.

363 lines
15 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from MySQLdb import IntegrityError
from django.shortcuts import render
# Create your views here.
# Create your views here.
from django.http import JsonResponse
from .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(request): # 教师个人信息修改
if request.method == "POST":
teacher_information = Teacher()
teacher_information.username = request.POST.get('username')
teacher_information.password = request.POST.get('password')
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')
teacher_information.save()
response = ResponseUtil.ok(teacher_information.to_dict(), "老师信息插入成功")
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 = {"code": 200, "message": "删除成功!", "data": info}
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']
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()
return JsonResponse({'code': 200, 'msg': 'success','data':data}, safe=False)
def delete(request):
tid = request.GET.get('tid')
try:
Teacher.objects.filter(tid = tid).delete()
return JsonResponse({'code': 200, 'msg': 'success'}, safe=False)
except:
return JsonResponse({'code': 200, 'msg': '删除失败'}, safe=False)
@csrf_exempt
def teaching_resources(request): # 课程程序
if request.method == 'POST':
cid = int(request.POST.get('cid'))
name = request.POST.get('name')
type = request.POST.get('type')
credit = request.POST.get('credit')
tid = int(request.POST.get('tid'))
teacher_name = request.POST.get('t_name')
# 定义空子字典查询条件
conditions = {}
if cid:
conditions.update({'cid': cid}) # 追加信息
if name:
conditions.update({'name': cid})
if type:
conditions.update({'type': cid})
if credit:
conditions.update({'credit': cid})
if tid:
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 = []
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":
# 获取第一个查询表的数据
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): # 学生信息查询
if request.method == 'POST':
name = request.POST.get('name')
sid = request.POST.get('sid')
sex = request.POST.get('sex')
grade = request.POST.get('grade')
class_name = request.POST.get('class_name')
major = request.POST.get('major')
# 定义空查询条件
conditions = Q()
if name:
conditions &= Q(name=name)
if sid:
conditions &= Q(sid=sid)
if sex:
conditions &= Q(sex=sex)
if grade:
conditions &= Q(grade=grade)
if class_name:
conditions &= Q(class_name=class_name)
if major:
conditions &= Q(major=major)
# 添加非空查询条件至基本查询集
query = Student.objects.filter(conditions)
data = query.values() # 将 QuerySet 对象转换为字典列表
data = list(data) # 转换为列表以便序列化为 JSON
print(query)
response = ResponseUtil.ok(data, "学生信息查询成功")
return JsonResponse(response)
elif request.method == "GET":
query = Student.objects.all()
data = query.values() # 将 QuerySet 对象转换为字典列表
data = list(data) # 转换为列表以便序列化为 JSON
response = ResponseUtil.ok(data, "学生信息查询成功")
return JsonResponse(response)
@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 and end_grade <= 100 and end_grade >= 0:
query = SC.objects.filter(sid = sid, cid =cid)
if query.exists(): # 如果学生存在,则修改学生成绩
query.update(middle_grade = middle_grade, end_grade= end_grade)
else: # 如果学生成绩不存在,则插入学生成绩信息
sc = SC()
sid = Student.objects.get(sid=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()
filtered = SC.objects.filter(sid = sid, cid =cid)
data = filtered.values()# 将 QuerySet 对象转换为字典列表
data = list(data) # 转换为列表以便序列化为 JSON
response = ResponseUtil.ok(data,"学生成绩录入成功")
return JsonResponse(response)
else:
response = ResponseUtil.error("成绩输入有误")
return JsonResponse(response)
elif request.method == "GET":
cid = request.GET.get('cid')
c_name = request.GET.get('c_name')
sid = request.GET.get('sid')
s_name = request.GET.get('s_name')
data = []
if c_name:
data.append(SC.objects.filter(cid__name=c_name))
elif s_name:
data.append(SC.objects.filter(sid__name=s_name))
elif sid and cid:
data.append(SC.objects.filter(sid=sid, cid=cid))
elif sid:
data.append(SC.objects.filter(sid=sid))
elif cid:
data.append(SC.objects.filter(cid=cid))
else:
data.append(SC.objects.all())
response = ResponseUtil.ok(list(data[0].values()), "成绩查询成功")
return JsonResponse(response)
elif request.method == "DELETE":
put = MultiPartParser(request.META, request, request.upload_handlers, request.encoding).parse()
sid = put[0]['sid']
print(sid)
try:
info = SC.objects.filter(sid=sid)
data = info.values() # 将 QuerySet 对象转换为字典列表
data = list(data) # 转换为列表以便序列化为 JSON
print(info)
SC.objects.filter(sid=sid).delete()
response = ResponseUtil.ok(data,"删除成功!")
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)
sid = put[0]['sid']
cid = put[0]['cid']
middle_grade = put[0]['middle_grade']
end_grade = put[0]['end_grade']
SC.objects.filter(sid=sid, cid=cid).update(middle_grade=middle_grade)
SC.objects.filter(sid=sid, cid=cid).update(end_grade=end_grade)
filtered = SC.objects.filter(sid=sid, cid=cid)
data = filtered.values() # 将 QuerySet 对象转换为字典列表
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 == '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=cid)
if name:
conditions &= Q(name=name)
if type:
conditions &= Q(type=type)
if credit:
conditions &= Q(credit=credit)
if 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()
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)