Further improvement of teacher functions

-Query student information
lrs-branch
lrs 1 year ago
parent fd5e26b71a
commit 1fa138dd24

@ -16,6 +16,9 @@ class Course(models.Model):
return {"cid": self.cid, "c_name": self.name, "type": self.type, "credit": self.credit, return {"cid": self.cid, "c_name": self.name, "type": self.type, "credit": self.credit,
"tid": self.tid} "tid": self.tid}
def tid_dict(self):
return {"cid": self.cid, "c_name": self.name, "type": self.type, "credit": self.credit,
"tid": self.tid.tid}
class Meta: class Meta:
db_table = "course" db_table = "course"
verbose_name = "课程" verbose_name = "课程"

@ -14,5 +14,6 @@ urlpatterns = [
path('delete/',delete), path('delete/',delete),
path('student/',student_performance_management), path('student/',student_performance_management),
path('query/',query_student_information), path('query/',query_student_information),
path('course/',teaching_resources) path('course/',teaching_resources),
path('selection/',course_selection)
] ]

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

Loading…
Cancel
Save