update code

master
bettleChen 1 year ago
parent afade783a0
commit f3658ac3bc

@ -12,9 +12,9 @@ from EduSystemServer.utils import ResponseUtil
@csrf_exempt @csrf_exempt
def login(request): def login(request):
username = json.loads(request.body).get("username") username = request.POST.get("username")
password = json.loads(request.body).get("password") password = request.POST.get("password")
_type = json.loads(request.body).get("type") _type = request.POST.get("type")
if _type == "student": if _type == "student":
student = Student.objects.filter(username=username, student = Student.objects.filter(username=username,
password=password).first() password=password).first()
@ -35,7 +35,8 @@ def login(request):
result = ResponseUtil.error("username or password error!") result = ResponseUtil.error("username or password error!")
else: else:
result = ResponseUtil.error("type error!") result = ResponseUtil.error("type error!")
return JsonResponse(result) response = JsonResponse(result)
return response
@csrf_exempt @csrf_exempt
def get_user_info(request): def get_user_info(request):

@ -13,11 +13,6 @@ https://docs.djangoproject.com/en/2.2/ref/settings/
import os import os
CORS_ALLOW_ORIGIN_WHITELIST = [
"http://localhost:8080", # 允许访问的来源
"http://localhost:8000", # 允许访问的来源
# 可以继续添加其他允许的来源
]
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
@ -36,6 +31,8 @@ DEBUG = True
ALLOWED_HOSTS = ["*"] ALLOWED_HOSTS = ["*"]
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
'django.contrib.admin', 'django.contrib.admin',
@ -65,21 +62,6 @@ MIDDLEWARE = [
# 'API.middle.AuthMiddleware', # 'API.middle.AuthMiddleware',
] ]
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_HEADERS = (
'XMLHttpRequest',
'X_FILENAME',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
'Pragma',
)
ROOT_URLCONF = 'EduSystemServer.urls' ROOT_URLCONF = 'EduSystemServer.urls'
@ -154,3 +136,18 @@ USE_TZ = False
# https://docs.djangoproject.com/en/2.2/howto/static-files/ # https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/' STATIC_URL = '/static/'
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_HEADERS = (
'accept',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-requested-with',
'Cookie', # 添加Cookie到允许的头部
)

@ -7,4 +7,4 @@ class ResponseUtil:
@staticmethod @staticmethod
def error(message="error!"): def error(message="error!"):
return {"code": -1, "message": message} return {"code": -1, "message": str(message)}

@ -5,7 +5,8 @@ from .views import *
urlpatterns = [ urlpatterns = [
path("", studnets), path("", studnets),
path("search", search_student), path("search", search_student),
path("selectCourse", get_select_course_by_id), path("selectCourse", select_course),
path("add", add_student), path("add", add_student),
path("delete", del_student) path("delete", del_student),
path("getGrade", get_grade),
] ]

@ -5,6 +5,8 @@ from django.shortcuts import render
# Create your views here. # Create your views here.
from django.http import JsonResponse from django.http import JsonResponse
from course.models import SC, Course
from .models import Student from .models import Student
from EduSystemServer.utils import ResponseUtil from EduSystemServer.utils import ResponseUtil
@ -21,8 +23,7 @@ from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
def studnets(request): def studnets(request):
if request.method == "POST": if request.method == "POST":
try: try:
request_data = json.loads(request.body) request_data = request.POST
print(request_data)
student = Student.objects.filter(sid=request_data.get("sid")).first() student = Student.objects.filter(sid=request_data.get("sid")).first()
student.username = request_data.get("username") student.username = request_data.get("username")
student.password = request_data.get("password") student.password = request_data.get("password")
@ -123,7 +124,7 @@ def add_student(request):
if not request.method == "POST": if not request.method == "POST":
return JsonResponse(ResponseUtil.error("request method error!")) return JsonResponse(ResponseUtil.error("request method error!"))
try: try:
request_data = json.loads(request.body) request_data = request.POST
student = Student() student = Student()
student.username = request_data.get("username") student.username = request_data.get("username")
student.password = request_data.get("password") student.password = request_data.get("password")
@ -181,5 +182,40 @@ def del_student(request):
return JsonResponse(result) return JsonResponse(result)
def get_select_course_by_id(request): @csrf_exempt
pass def select_course(request):
if not request.method == "POST":
return JsonResponse(ResponseUtil.error("request method error!"))
try:
request_data = request.POST
cid = request_data.get("cid")
username = request_data.get("username")
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)))
def get_grade(request):
if not request.method == "GET":
return ResponseUtil.error("request method error!")
try:
username = request.GET.get("username")
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))

@ -4,4 +4,7 @@ from course.views import *
urlpatterns = [ urlpatterns = [
path("", courses), path("", courses),
path("getCourseById", get_course_by_student_id),
path("deleteSelectCourse", delete_select_course),
path("seacherCourse", search_course)
] ]

@ -1,11 +1,17 @@
import json
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.shortcuts import render from django.shortcuts import render
# Create your views here. # Create your views here.
from django.http import JsonResponse from django.http import JsonResponse
from EduSystemServer.utils import ResponseUtil
from course.models import * from course.models import *
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from io import BytesIO from io import BytesIO
from django.http.multipartparser import MultiPartParser from django.http.multipartparser import MultiPartParser
from django.core import serializers
@csrf_exempt @csrf_exempt
@ -20,32 +26,29 @@ def courses(request):
response = {"code": 200, "message": "添加成功!", "data": course.to_dict()} response = {"code": 200, "message": "添加成功!", "data": course.to_dict()}
return JsonResponse(response) return JsonResponse(response)
elif request.method == "GET": elif request.method == "GET":
cid = request.GET.get('cid') teacher_name = request.GET.get("tName")
c_name = request.GET.get('c_name') course_name = request.GET.get("cName")
c_type = request.GET.get('type') course_type = request.GET.get("cType")
tid = request.GET.get('tid') current_page = request.GET.get("currentPage")
data = [] if not teacher_name == "" or not teacher_name is None:
if cid: filter_course = Course.objects.filter(tid__name__contains=teacher_name)
filtered = Course.objects.filter(cid=cid) if not course_name == "" or not course_name is None:
for item in filtered: filter_course = Course.objects.filter(tid__course__name=course_name)
data.append(item.to_dict()) if not course_type == "" or not course_type is None:
elif c_name: filter_course = Course.objects.filter(tid__course__type=course_type)
filtered = Course.objects.filter(c_name=c_name) filter_course = filter_course.values("tid__course__name", "tid__name", "tid__course__type", "tid__course__credit",
for item in filtered: "tid__title", "tid__education", "tid__dept", "cid").all()
data.append(item.to_dict()) paginator = Paginator(filter_course, 10)
elif c_type: try:
filtered = Course.objects.filter(type=c_type) courses = paginator.page(current_page).object_list
for item in filtered: except PageNotAnInteger:
data.append(item.to_dict()) courses = paginator.page(1)
elif tid: except EmptyPage:
filtered = Course.objects.filter(tid=tid) courses = paginator.page(paginator.num_pages).object_list
for item in filtered: result = ResponseUtil.ok(json.loads(serializers.serialize("json", courses)), "success!")
data.append(item.to_dict()) result["pageTotal"] = paginator.count
else: result["pageNum"] = paginator.num_pages
all_objects = Course.objects.all() return JsonResponse(result)
for item in all_objects:
data.append(item.to_dict())
return JsonResponse({'code': 200, 'msg': 'success', 'data': data}, safe=False)
elif request.method == "DELETE": elif request.method == "DELETE":
delete = MultiPartParser(request.META, BytesIO(request.body), request.upload_handlers, request.encoding).parse() delete = MultiPartParser(request.META, BytesIO(request.body), request.upload_handlers, request.encoding).parse()
cid = delete[0]['cid'] cid = delete[0]['cid']
@ -67,3 +70,64 @@ def courses(request):
Course.objects.filter(cid=cid).update(c_name=c_name, type=c_type, credit=credit, tid=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() data = Course.objects.filter(cid=cid)[0].to_dict()
return JsonResponse({'code': 200, 'msg': 'success', 'data': data}, safe=False) 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)
def get_course_by_student_id(request):
if not request.method == "GET":
return JsonResponse(ResponseUtil.error("request method error!"))
s_username = request.GET.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))

Loading…
Cancel
Save