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.
206 lines
8.2 KiB
206 lines
8.2 KiB
import json
|
|
|
|
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
|
|
from django.db.models import F, Subquery, Max
|
|
from django.shortcuts import render
|
|
|
|
# Create your views here.
|
|
from django.http import JsonResponse
|
|
|
|
from EduSystemServer.utils import *
|
|
from course.models import *
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from io import BytesIO
|
|
from django.http.multipartparser import MultiPartParser
|
|
from django.core import serializers
|
|
|
|
|
|
@csrf_exempt
|
|
def courses(request):
|
|
if request.method == "POST":
|
|
course = Course()
|
|
course.c_name = request.POST.get("c_name")
|
|
course.type = request.POST.get('type')
|
|
course.credit = request.POST.get("credit")
|
|
course.tid = request.POST.get('tid')
|
|
course.save()
|
|
response = {"code": 200, "message": "添加成功!", "data": course.to_dict()}
|
|
return JsonResponse(response)
|
|
elif request.method == "GET":
|
|
teacher_name = request.GET.get("tName")
|
|
course_name = request.GET.get("cName")
|
|
course_type = request.GET.get("cType")
|
|
current_page = request.GET.get("currentPage")
|
|
if not teacher_name == "" or not teacher_name is None:
|
|
filter_course = Course.objects.filter(tid__name__contains=teacher_name)
|
|
if not course_name == "" or not course_name is None:
|
|
filter_course = Course.objects.filter(tid__course__name=course_name)
|
|
if not course_type == "" or not course_type is None:
|
|
filter_course = Course.objects.filter(tid__course__type=course_type)
|
|
filter_course = filter_course.values("tid__course__name", "tid__name", "tid__course__type", "tid__course__credit",
|
|
"tid__title", "tid__education", "tid__dept", "cid").all()
|
|
paginator = Paginator(filter_course, 10)
|
|
try:
|
|
courses = paginator.page(current_page).object_list
|
|
except PageNotAnInteger:
|
|
courses = paginator.page(1)
|
|
except EmptyPage:
|
|
courses = paginator.page(paginator.num_pages).object_list
|
|
result = ResponseUtil.ok(json.loads(serializers.serialize("json", courses)), "success!")
|
|
result["pageTotal"] = paginator.count
|
|
result["pageNum"] = paginator.num_pages
|
|
return JsonResponse(result)
|
|
elif request.method == "DELETE":
|
|
delete = MultiPartParser(request.META, BytesIO(request.body), request.upload_handlers, request.encoding).parse()
|
|
cid = delete[0]['cid']
|
|
try:
|
|
info = Course.objects.filter(cid=cid).get().to_dict()
|
|
Course.objects.filter(cid=cid).delete()
|
|
response = {"code": 200, "message": "删除成功!", "data": info}
|
|
return JsonResponse(response)
|
|
except:
|
|
response = {"code": 200, "message": "删除失败,未找到课程信息!"}
|
|
return JsonResponse(response)
|
|
elif request.method == "PUT":
|
|
put = MultiPartParser(request.META, request, request.upload_handlers, request.encoding).parse()
|
|
cid = put[0]['cid']
|
|
c_name = put[0]['c_name']
|
|
c_type = put[0]['type']
|
|
credit = put[0]['credit']
|
|
tid = put[0]['tid']
|
|
Course.objects.filter(cid=cid).update(c_name=c_name, type=c_type, credit=credit, tid=tid)
|
|
data = Course.objects.filter(cid=cid)[0].to_dict()
|
|
return JsonResponse({'code': 200, 'msg': 'success', 'data': data}, safe=False)
|
|
|
|
|
|
def search_course(request):
|
|
"""
|
|
通过条件查询课程,并且分页显示
|
|
"""
|
|
if not request.method == "GET":
|
|
return JsonResponse(ResponseUtil.error("request method error!"))
|
|
teacher_name = request.GET.get("tName")
|
|
course_name = request.GET.get("cName")
|
|
course_type = request.GET.get("cType")
|
|
current_page = request.GET.get("currentPage")
|
|
filter_course = Course.objects
|
|
if not teacher_name == "" and teacher_name is not None:
|
|
filter_course = Course.objects.filter(tid__name__contains=teacher_name)
|
|
if not course_name == "" and course_name is not None:
|
|
filter_course = Course.objects.filter(tid__course__name__contains=course_name)
|
|
if not course_type == "" and course_type is not None:
|
|
filter_course = Course.objects.filter(tid__course__type__contains=course_type)
|
|
filter_course = filter_course.values("tid__course__name", "tid__name", "tid__course__type", "tid__course__credit",
|
|
"tid__title", "tid__education", "tid__dept", "cid").all()
|
|
paginator = Paginator(filter_course, 10)
|
|
try:
|
|
courses = paginator.page(current_page).object_list
|
|
except PageNotAnInteger:
|
|
courses = paginator.page(1).object_list
|
|
except EmptyPage:
|
|
courses = paginator.page(paginator.num_pages).object_list
|
|
courses_data = []
|
|
for course in courses:
|
|
courses_data.append(course)
|
|
result = ResponseUtil.ok(courses_data, "success!")
|
|
result["pageTotal"] = paginator.count
|
|
result["pageNum"] = paginator.num_pages
|
|
return JsonResponse(result)
|
|
|
|
@permission(allowed_roles=["student"])
|
|
def get_course_by_student_id(request):
|
|
"""
|
|
通过学生ID获取学生的选课
|
|
"""
|
|
if not request.method == "GET":
|
|
return JsonResponse(ResponseUtil.error("request method error!"))
|
|
s_username = request.jwt_payload.get("username")
|
|
student = Student.objects.filter(username=s_username).first()
|
|
sc_list = SC.objects.filter(sid=student.sid).values("sid__name", "cid__name", "cid__tid__name", "sid", "cid",
|
|
"cid__credit", "cid__tid__title", "cid__tid__dept", "cid__type").all()
|
|
data = []
|
|
for sc in sc_list:
|
|
data.append(sc)
|
|
result = ResponseUtil.ok(data)
|
|
return JsonResponse(result)
|
|
|
|
|
|
def delete_select_course(request):
|
|
"""
|
|
删除选课
|
|
"""
|
|
if not request.method == "GET":
|
|
return JsonResponse(ResponseUtil.error("request method error!"))
|
|
try:
|
|
sid = request.GET.get("sid")
|
|
cid = request.GET.get("cid")
|
|
sc = SC.objects.filter(cid=cid, sid=sid).first()
|
|
sc.delete()
|
|
return JsonResponse(ResponseUtil.ok(None, "退出选课成功!"))
|
|
except Exception as E:
|
|
return JsonResponse(ResponseUtil.error(E))
|
|
|
|
@csrf_exempt
|
|
@permission(allowed_roles=["teacher", "admin"])
|
|
def get_student_select_course(request):
|
|
"""
|
|
获取所有学生的所有选课
|
|
"""
|
|
if not request.method == "POST":
|
|
return JsonResponse(ResponseUtil.error("request method error!"))
|
|
current_page = request.GET.get("currentPage")
|
|
sname = request.POST.get("sname")
|
|
className = request.POST.get("className")
|
|
courseName = request.POST.get("courseName")
|
|
courseType = request.POST.get("courseType")
|
|
sc_list = SC.objects.all().filter(
|
|
sid__name__contains=sname,
|
|
sid__class_name__contains=className,
|
|
cid__name__contains=courseName,
|
|
cid__type__contains=courseType
|
|
).annotate(
|
|
sname=F("sid__name"),
|
|
className=F("sid__class_name"),
|
|
cname=F("cid__name"), ctype=F("cid__type"),
|
|
endGrade=F("end_grade"),
|
|
middleGrade=F("middle_grade")
|
|
).values(
|
|
"sname", "className", "cname",
|
|
"ctype", "endGrade", "middleGrade",
|
|
"sid", "cid"
|
|
).distinct()
|
|
paginator = Paginator(sc_list, 10)
|
|
try:
|
|
sc_list = paginator.page(current_page).object_list
|
|
except PageNotAnInteger:
|
|
sc_list = paginator.page(1)
|
|
except EmptyPage:
|
|
sc_list = paginator.page(paginator.num_pages).object_list
|
|
data = []
|
|
for student in sc_list:
|
|
data.append(student)
|
|
result = ResponseUtil.ok(data, "success!")
|
|
result["pageTotal"] = paginator.count
|
|
result["pageNum"] = paginator.num_pages
|
|
return JsonResponse(result)
|
|
|
|
@permission(allowed_roles=["teacher", "admin"])
|
|
@csrf_exempt
|
|
def edit_grade(request):
|
|
"""
|
|
成绩录入
|
|
"""
|
|
if not request.method == "POST":
|
|
return JsonResponse(ResponseUtil.error("request method error!"))
|
|
try:
|
|
sid = request.POST.get("sid")
|
|
cid = request.POST.get("cid")
|
|
sc = SC.objects.filter(sid=sid, cid=cid).first()
|
|
sc.middle_grade = request.POST.get("middleGrade")
|
|
sc.end_grade = request.POST.get("endGrade")
|
|
sc.save()
|
|
return JsonResponse(ResponseUtil.ok(None, "成绩修改成功!"))
|
|
except Exception as E:
|
|
return JsonResponse(ResponseUtil.error(str(E)))
|
|
|