diff --git a/EduSystemServer/EduSystemServer/urls.py b/EduSystemServer/EduSystemServer/urls.py index ef87b34..c881251 100644 --- a/EduSystemServer/EduSystemServer/urls.py +++ b/EduSystemServer/EduSystemServer/urls.py @@ -21,4 +21,5 @@ urlpatterns = [ path('Eduadmin/', admin.site.urls), path('api/', include(("API.urls", "api"), namespace="api")), path('student/', include(("Student.urls", "student"), namespace="student")), + path('course/', include(("course.urls", "course"), namespace="course")) ] diff --git a/EduSystemServer/Student/views.py b/EduSystemServer/Student/views.py index f4d5456..e177196 100644 --- a/EduSystemServer/Student/views.py +++ b/EduSystemServer/Student/views.py @@ -74,16 +74,19 @@ def studnets(request): elif request.method == "PUT": put = MultiPartParser(request.META, request, request.upload_handlers, request.encoding).parse() sid = put[0]['sid'] + s_username = put[0]['s_username'] s_name = put[0]['s_name'] s_sex = put[0]['s_sex'] s_grade = put[0]['s_grade'] s_class_name = put[0]['s_class_name'] s_major = put[0]['s_major'] - Student.objects.filter(sid=sid).update(s_name=s_name) - Student.objects.filter(sid=sid).update(s_sex=s_sex) - Student.objects.filter(sid=sid).update(s_grade=s_grade) - Student.objects.filter(sid=sid).update(s_class_name=s_class_name) - Student.objects.filter(sid=sid).update(s_major=s_major) + try: + Student.objects.filter(sid=sid).update(s_username=s_username, s_name=s_name, s_sex=s_sex, s_grade=s_grade, + s_class_name=s_class_name, + s_major=s_major) + except: + Student.objects.create(sid=sid, s_username=s_username, s_name=s_name, s_sex=s_sex, s_grade=s_grade, + s_class_name=s_class_name, + s_major=s_major) data = Student.objects.filter(sid=sid)[0].to_dict() return JsonResponse({'code': 200, 'msg': 'success', 'data': data}, safe=False) - diff --git a/EduSystemServer/course/models.py b/EduSystemServer/course/models.py index b2fda46..c9d7d8d 100644 --- a/EduSystemServer/course/models.py +++ b/EduSystemServer/course/models.py @@ -12,6 +12,10 @@ class Course(models.Model): credit = models.IntegerField(verbose_name="课程学分", name="credit") tid = models.ForeignKey(Teacher, to_field="tid", on_delete=models.CASCADE, name="tid") + def to_dict(self): + return {"cid": self.cid, "c_name": self.c_name, "type": self.type, "credit": self.credit, + "tid": self.tid} + class Meta: db_table = "course" verbose_name = "课程" @@ -24,6 +28,9 @@ class SC(models.Model): middle_grade = models.IntegerField(name="middle_grade") end_grade = models.IntegerField(name="end_grade") + def to_dict(self): + return {"sid": self.sid, "cid": self.cid, "middle_grade": self.middle_grade, "end_grade": self.end_grade} + class Meta: db_table = "sc" verbose_name = "管理员" diff --git a/EduSystemServer/course/urls.py b/EduSystemServer/course/urls.py new file mode 100644 index 0000000..74f5f91 --- /dev/null +++ b/EduSystemServer/course/urls.py @@ -0,0 +1,7 @@ +from django.urls import path + +from course.views import * + +urlpatterns = [ + path("", courses), +] \ No newline at end of file diff --git a/EduSystemServer/course/views.py b/EduSystemServer/course/views.py index 91ea44a..e72d5c1 100644 --- a/EduSystemServer/course/views.py +++ b/EduSystemServer/course/views.py @@ -1,3 +1,69 @@ 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)