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.

136 lines
4.8 KiB

1 year ago
import json
from django.core import serializers
from django.shortcuts import render
# Create your views here.
1 year ago
from course.models import SC, Course
1 year ago
1 year ago
from EduSystemServer.utils import *
1 year ago
from .models import Student
from django.views.decorators.csrf import csrf_exempt
1 year ago
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
1 year ago
1 year ago
@csrf_exempt
1 year ago
@permission(allowed_roles=["admin", "teacher"])
1 year ago
def add_student(request):
"""
添加学生
"""
1 year ago
if not request.method == "POST":
return JsonResponse(ResponseUtil.error("request method error!"))
try:
1 year ago
request_data = request.POST
1 year ago
student = Student()
student.username = request_data.get("username")
student.password = request_data.get("password")
student.name = request_data.get("name")
student.sex = request_data.get("sex")
student.grade = request_data.get("grade")
student.class_name = request_data.get("class_name")
student.major = request_data.get("major")
student.save()
result = ResponseUtil.ok(student.to_dict(), "添加成功!")
except Exception as E:
result = ResponseUtil.error(E)
return JsonResponse(result)
@csrf_exempt
1 year ago
@permission(allowed_roles=["admin", "teacher"])
1 year ago
def search_student(request):
"""
根据表单对学生进行模糊查询以及将查询的数据进行分页
"""
1 year ago
currentPage = request.GET.get("currentPage")
pageSize = request.GET.get("pageSize")
1 year ago
request_data = request.POST
1 year ago
search_students = Student.objects.all()
if not request_data.get("sid") == "":
search_students =search_students.filter(sid=request_data.get("sid"))
if not request_data.get("name") == "":
search_students =search_students.filter(name__contains=request_data.get("name"))
if not request_data.get("sex") == "":
search_students =search_students.filter(sex=request_data.get("sex"))
if not request_data.get("class_name") == "":
search_students =search_students.filter(class_name__contains=request_data.get("class_name"))
if not request_data.get("major") == "":
search_students =search_students.filter(major__contains=request_data.get("major"))
paginator = Paginator(search_students, pageSize)
try:
students = paginator.page(currentPage).object_list
except PageNotAnInteger:
students = paginator.page(1)
except EmptyPage:
students = paginator.page(paginator.num_pages).object_list
result = ResponseUtil.ok(json.loads(serializers.serialize("json", students)), "success!")
result["pageTotal"] = paginator.count
result["pageNum"] = paginator.num_pages
return JsonResponse(result)
@csrf_exempt
1 year ago
@permission(allowed_roles=["admin", "teacher"])
1 year ago
def del_student(request):
"""
根据学生ID删除学生
"""
1 year ago
if not request.method == "GET":
return JsonResponse(ResponseUtil.error("request method error!"))
try:
sid = request.GET.get("sid")
student = Student.objects.filter(sid=sid).first()
student.delete()
result = ResponseUtil.ok(None, "删除成功!")
except Exception as E:
result = ResponseUtil.error(E)
return JsonResponse(result)
1 year ago
@csrf_exempt
1 year ago
@permission(allowed_roles=["admin", "student", "teacher"])
1 year ago
def select_course(request):
"""
选课
"""
1 year ago
if not request.method == "POST":
return JsonResponse(ResponseUtil.error("request method error!"))
try:
request_data = request.POST
cid = request_data.get("cid")
1 year ago
username = request.jwt_payload.get("username")
1 year ago
student = Student.objects.filter(username=username).first()
if SC.objects.filter(cid=cid, sid=student.sid).exists():
return JsonResponse(ResponseUtil.error("该课程已经选择!"))
sc = SC()
sc.sid = student
sc.cid = Course.objects.filter(cid=cid).first()
sc.middle_grade = 0
sc.end_grade = 0
sc.save()
return JsonResponse(ResponseUtil.ok(None, "选课成功!"))
except Exception as E:
print(E)
return JsonResponse(ResponseUtil.error(str(E)))
1 year ago
@permission(allowed_roles=["student"])
1 year ago
def get_grade(request):
1 year ago
"""
获取学生成绩
"""
1 year ago
if not request.method == "GET":
return ResponseUtil.error("request method error!")
try:
1 year ago
username = request.jwt_payload.get("username")
1 year ago
student = Student.objects.filter(username=username).first()
grade__all = SC.objects.filter(sid=student.sid).values("sid", "sid__name", "cid__name", "cid__type", "cid__credit",
"cid__tid__name", "middle_grade", "end_grade").all()
grades = []
for grade in grade__all:
grades.append(grade)
return JsonResponse(ResponseUtil.ok(grades))
except Exception as E:
return JsonResponse(ResponseUtil.error(E))