From b2c194e7072cca6f9372c722a401d99bd0e2b538 Mon Sep 17 00:00:00 2001 From: Yontennorbu <1052126954@qq.com> Date: Mon, 3 Jun 2019 19:23:56 +0800 Subject: [PATCH] getcourseAPI --- course/serializers.py | 8 ++++++++ course/urls/student.py | 3 +++ course/views/student.py | 17 ++++++++++++++++- oj/urls.py | 1 + user/urls/admin.py | 6 +++--- 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/course/serializers.py b/course/serializers.py index e69de29..231806e 100644 --- a/course/serializers.py +++ b/course/serializers.py @@ -0,0 +1,8 @@ +from rest_framework import serializers +from course.models import Course + +class CourseSerializer(serializers.ModelSerializer): + class meta: + model = Course + fields = ('id','name', 'start_time', 'end_time', + 'description') \ No newline at end of file diff --git a/course/urls/student.py b/course/urls/student.py index 11d65f2..a334623 100644 --- a/course/urls/student.py +++ b/course/urls/student.py @@ -1,4 +1,7 @@ from django.conf.urls import url +from django.urls import path +from course.views.student import GetCourseAPI urlpatterns = [ + url(r"student-personal-information/?$", GetCourseAPI.as_view(), name="student-personal-information"), ] \ No newline at end of file diff --git a/course/views/student.py b/course/views/student.py index 2b3ae7e..02618b3 100644 --- a/course/views/student.py +++ b/course/views/student.py @@ -1 +1,16 @@ -from utils.api import APIView \ No newline at end of file +from utils.api import APIView +from course.models import Course +from course.serializers import CourseSerializer +from user.models import Student +from django.http import HttpResponse, JsonResponse + +class GetCourseAPI(APIView): + def get(self, request): + + user_id = int(request.GET.get("user_id")) + # Get the stu object first and then stu.courses.all() to get all the courses. Serilizer converts the package to json and returns it to the front end. + student = Student.objects.filter(id=user_id) + courses = student.courses.all() + #before there is successed + serializer = CourseSerializer(courses, many=True) + return JsonResponse(serializer.data) # [{}{}{}] \ No newline at end of file diff --git a/oj/urls.py b/oj/urls.py index 49c7b80..e175e17 100644 --- a/oj/urls.py +++ b/oj/urls.py @@ -39,4 +39,5 @@ url(r"^teacher/submission/statistics/", include("submission.urls.statistics")), url(r"^distribution/", include("submission.urls.statistics")), + url(r"^api/student/course", include("course.urls.student")), ] diff --git a/user/urls/admin.py b/user/urls/admin.py index cb7be11..8d415a5 100644 --- a/user/urls/admin.py +++ b/user/urls/admin.py @@ -16,7 +16,7 @@ url(r'student_get/?$', GetStudentAPI.as_view(), name="student_get"), url(r'student_update/?$', UpdateStudentAPI.as_view(), name="student_update"), - url(r'teacher_create/?$', CreateTeacherAPI.as_view(), name="teacher_create"), - url(r'teacher_get/?$', GetTeacherAPI.as_view(), name="teacher_get"), - url(r'teacher_update/?$', UpdateTeacherAPI.as_view(), name="teacher_update"), + #url(r'teacher_create/?$', CreateTeacherAPI.as_view(), name="teacher_create"), + #url(r'teacher_get/?$', GetTeacherAPI.as_view(), name="teacher_get"), + #url(r'teacher_update/?$', UpdateTeacherAPI.as_view(), name="teacher_update"), ]