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.

109 lines
4.5 KiB

from django.shortcuts import render
from django.db.models import Q
# Create your views here.
from teacher.models import Teacher
from Student.models import Student
from course.models import SC, Course
from django.views.decorators.csrf import csrf_exempt
from django.http import QueryDict
from io import BytesIO
from django.http.multipartparser import MultiPartParser
import json
from django.http import JsonResponse
from EduSystemServer.utils import ResponseUtil
from .models import Admin
# 修改个人信息
@csrf_exempt
def admin_info(request):
if request.method == "POST":
try:
admin = Admin()
admin.username = request.POST.get("username")
admin.password = request.POST.get("password")
admin.save()
response = ResponseUtil.ok(admin.to_dict(), "添加成功!")
return JsonResponse(response)
except Exception as E:
return JsonResponse(ResponseUtil.error(E))
elif request.method == "GET":
username = request.GET.get("username")
try:
ad_info = Admin.objects.filter(username=username).values()
data = {
'管理员账号': ad_info[0]['username'],
'管理员密码': ad_info[0]['password']
}
response = ResponseUtil.ok(data, "查询成功!")
return JsonResponse(response)
except Exception as E:
return JsonResponse(ResponseUtil.error(E))
# 学生管理
@csrf_exempt
def student_manage(request):
if request.method == "GET":
student = Student.objects.all().values()
stu_list = []
for i in range(len(student)):
data = {
'学生编号': student[i]['sid'],
'用户账号': student[i]['username'],
'用户密码': student[i]['password'],
'姓名': student[i]['name'],
'性别': student[i]['sex'],
'年级': student[i]['grade'],
'班级': student[i]['class_name'],
'专业': student[i]['major'],
}
stu_list.append(data)
response = ResponseUtil.ok(stu_list, "学生信息查询成功!")
return JsonResponse(response)
elif request.method == "POST":
sid = request.GET.get('sid')
try:
stu_info = Student.objects.get(sid=sid)
username = request.POST.get("username") or stu_info.username
password = request.POST.get("password") or stu_info.password
name = request.POST.get("name") or stu_info.name
sex = request.POST.get("sex") or stu_info.sex
grade = request.POST.get("grade") or stu_info.grade
class_name = request.POST.get("class_name") or stu_info.class_name
major = request.POST.get("major") or stu_info.major
Student.objects.filter(sid=sid).update(username=username, password=password, name=name, sex=sex,
grade=grade, class_name=class_name, major=major)
except Exception as E:
return JsonResponse(ResponseUtil.error(E))
elif request.method == "DELETE":
delete = MultiPartParser(request.META, BytesIO(request.body), request.upload_handlers, request.encoding).parse()
sid = delete[0]['sid']
try:
info = Student.objects.filter(sid=sid).get().to_dict()
Student.objects.filter(sid=sid).delete()
response = ResponseUtil.ok(info, "删除成功!")
return JsonResponse(response)
except:
return JsonResponse(ResponseUtil.error("删除失败,未找到学生信息!"))
elif request.method == "PUT":
put = MultiPartParser(request.META, request, request.upload_handlers, request.encoding).parse()
sid = put[0]['sid']
username = put[0]['username']
name = put[0]['name']
sex = put[0]['sex']
grade = put[0]['grade']
class_name = put[0]['class_name']
major = put[0]['major']
if Student.objects.filter(sid=sid):
Student.objects.filter(sid=sid).update(username=username, name=name, sex=sex, grade=grade,
class_name=class_name,
major=major)
else:
return JsonResponse(ResponseUtil.error("没有查询到该同学!"))
data = Student.objects.filter(sid=sid)[0].to_dict()
return JsonResponse(ResponseUtil.ok(data, "修改成功!"))