|
|
|
from django.shortcuts import render
|
|
|
|
|
|
|
|
# Create your views here.
|
|
|
|
from django.http import JsonResponse
|
|
|
|
from course.models import *
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from io import BytesIO
|
|
|
|
from django.http.multipartparser import MultiPartParser
|
|
|
|
|
|
|
|
|
|
|
|
@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":
|
|
|
|
cid = request.GET.get('cid')
|
|
|
|
c_name = request.GET.get('c_name')
|
|
|
|
c_type = request.GET.get('type')
|
|
|
|
tid = request.GET.get('tid')
|
|
|
|
data = []
|
|
|
|
if cid:
|
|
|
|
filtered = Course.objects.filter(cid=cid)
|
|
|
|
for item in filtered:
|
|
|
|
data.append(item.to_dict())
|
|
|
|
elif c_name:
|
|
|
|
filtered = Course.objects.filter(c_name=c_name)
|
|
|
|
for item in filtered:
|
|
|
|
data.append(item.to_dict())
|
|
|
|
elif c_type:
|
|
|
|
filtered = Course.objects.filter(type=c_type)
|
|
|
|
for item in filtered:
|
|
|
|
data.append(item.to_dict())
|
|
|
|
elif tid:
|
|
|
|
filtered = Course.objects.filter(tid=tid)
|
|
|
|
for item in filtered:
|
|
|
|
data.append(item.to_dict())
|
|
|
|
else:
|
|
|
|
all_objects = Course.objects.all()
|
|
|
|
for item in all_objects:
|
|
|
|
data.append(item.to_dict())
|
|
|
|
return JsonResponse({'code': 200, 'msg': 'success', 'data': data}, safe=False)
|
|
|
|
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)
|