def put(self, request, pk): item = get_object_or_404(Course, pk=pk) serializer = CourseSerializer(item, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def course_detail(request, pk): # find course by pk (id) try: course = Course.objects.get(pk=pk) except Course.DoesNotExist: return JsonResponse({'message': 'The course does not exist'}, status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': course_serializer = CourseSerializer(course) return JsonResponse(course_serializer.data) elif request.method == 'PUT': course_data = JSONParser().parse(request) course_serializer = CourseSerializer(course, data=course_data) if course_serializer.is_valid(): course_serializer.save() return JsonResponse(course_serializer.data) return JsonResponse(course_serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': course.delete() return JsonResponse({'message': 'Course was deleted successfully!'}, status=status.HTTP_204_NO_CONTENT)
def course_detail(request, cID): #Retrieve, update or delete a code snippet. try: course = Course.objects.using('course').get(courseID=cID) except Course.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = CourseSerializer(course) return JSONResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = CourseSerializer(course, data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data) return JSONResponse(serializer.errors, status=400) elif request.method == 'DELETE': delete_enroll(cID=cID) print "test" course.delete(using='course') return HttpResponse(status=204)
def put(self, request, pk, format=None): course = self.get_object(pk) serializer = CourseSerializer(course, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, req): course = CourseSerializer(data=req.data) if course.is_valid(): course.save() return Response(status=status.HTTP_201_CREATED) else: return Response(status=status.HTTP_400, data=course.errors)
def course_list(request): if request.method == 'GET': courses = Course.objects.all() courseNumber = request.query_params.get('courseNumber', None) if courseNumber is not None: courses = courses.filter(courseNumber=courseNumber) courses_serializer = CourseSerializer(courses, many=True) return JsonResponse(courses_serializer.data, safe=False) # 'safe=False' for objects serialization elif request.method == 'POST': course_data = JSONParser().parse(request) course_serializer = CourseSerializer(data=course_data) if course_serializer.is_valid(): course_serializer.save() return JsonResponse(course_serializer.data, status=status.HTTP_201_CREATED) return JsonResponse(course_serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': count = Course.objects.all().delete() return JsonResponse( { 'message': '{} courses were deleted successfully!'.format( count[0]) }, status=status.HTTP_204_NO_CONTENT)
def put(self, req, pk): course_obj = get_object_or_404(Course, id=pk) serialized = CourseSerializer(instance=course_obj, data=req.data, partial=True) if serialized.is_valid(): serialized.save() return Response(status=status.HTTP_200_OK) else: return Response(status=status.HTTP_400_BAD_REQUEST, data=serialized.errors)
def handle(self, *args, **options): succ = 0 fail = 0 file_name = options.get('file_name') if not options.get('file_name'): print "No Filename Specified (--file_name <FILE_NAME>)" return with open(COURSE_DATA_DIR + file_name[0], 'r') as f: course_data = [parse_course_data(data) for data in f.readlines()] print "Ingesting {0} courses".format(len(course_data)) for record_data in course_data: serializer = CourseSerializer(data=record_data) if serializer.is_valid(): # print serializer.validated_data course = serializer.save() succ += 1 print "course created: {0}".format(course.name) else: fail += 1 print "Course data invalid: {0}".format(serializer.errors) print "Ingestion complete" print "{0} courses processed".format(str(succ)) if fail: print "Failed to ingest {0} courses".format(str(fail))
def course_list(request): #List all code courses, or create a new course. if request.method == 'GET': courses = Course.objects.using('course').all() serializer = CourseSerializer(courses, many=True) print serializer.data return JSONResponse(serializer.data) elif request.method == 'POST': data = JSONParser().parse(request) serializer = CourseSerializer(data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status=201) return JSONResponse(serializer.errors, status=400)
def put(self, request, course_id=None): if not course_id: return Response('no course_id supplied', status=400) course = self.queryset.get(course_id=course_id) serializer = CourseSerializer(data=request.data.copy(), instance=course) serializer.is_valid(raise_exception=True) course = serializer.save() return Response(serializer.data)
def post(self, request): course_data = request.data.copy() print course_data if not course_data.get('course_id'): course_data['course_id'] = randint(10000, 99999) serializer = CourseSerializer(data=course_data) serializer.is_valid(raise_exception=True) course = serializer.save() return Response(serializer.data)
def course_list(request): # GET list of courses, POST a new course, DELETE all courses if request.method == 'GET': courses = Course.objects.all() title = request.GET.get('title', None) if title is not None: courses = courses.filter(title__icontains=title) courses_serializer = CourseSerializer(courses, many=True) return JsonResponse(courses_serializer.data, safe=False) # 'safe=False' for objects serialization elif request.method == 'POST': course_data = JSONParser().parse(request) course_serializer = CourseSerializer(data=course_data) if course_serializer.is_valid(): course_serializer.save() return JsonResponse(course_serializer.data, status=status.HTTP_201_CREATED) return JsonResponse(course_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_serializer_create_method_works(self): data = { "name": "Chinese language school", "description": "Learn how to speak chinese in a month or less", "logo": "Some logo", "contact": "FACEBOOK", "category": "Language", "branches": [{ "latitude": "103.5", "longtitude": "158.6", "address": "Bakery street 27" }] } serializer = CourseSerializer(data=data) self.assertTrue(serializer.is_valid()) serializer.save() exists = Course.objects.get(id=1) self.assertIsNotNone(exists)
def post(self, request): serializer = CourseSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)