def createStudent(request): # product = Product.objects.get(id=pk) serializer = StudentSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data)
def put(self, request, pk, format=None): student = self.get_object(pk) serializer = StudentSerializer(student, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_gpa_decimals(self): matric_no = "1234567" data = [{ "matricNo": matric_no, "givenNames": "Philip J", "surname": "Fry", "academicPlan": "F100-2208", "gradYear": "19-20", "finalAward3": 1.200 }] serializer = StudentSerializer(data=data, many=True) if serializer.is_valid(): serializer.save() student = Student.objects.get(matricNo=matric_no) student.save() self.assertEqual(str(student.finalAward1), '1.2') self.assertEqual(str(student.finalAward2), '1.20') self.assertEqual(str(student.finalAward3), '1.200') student.finalAward1 = 1.23 student.save() self.decimal_assertion() student.finalAward1 = 1.232 student.save() self.decimal_assertion() final_award3_dec = str(student.finalAward3).partition('.')[-1] self.assertEqual(len(final_award3_dec), 3)
def updateStudent(request,pk): student = Student_model.objects.get(id=pk) serializer = StudentSerializer(instance=student,data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data)
def post(self, request): print(self.request.user) serializer = StudentSerializer(data=request.data) if serializer.is_valid(): print(self.request.user) serializer.save(user=self.request.user) return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response({'status': 'error'})
def student_list(request): if request.method == 'GET': students = Student.objects.all() serializer = StudentSerializer(students, many=True) return JsonResponse(serializer.data, safe=False) if request.method == 'POST': data = JSONParser().parse(request) serializer = StudentSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def post(self, request, *args, **kwargs): json_data = request.body stream = io.BytesIO(json_data) pythondata = JSONParser().parse(stream) serializer = StudentSerializer(data=pythondata) if serializer.is_valid(): serializer.save() res = {"msg": "Data Created"} json_data = JSONRenderer().render(res) return HttpResponse(json_data, content_type="application/json") json_data = JSONRenderer().render(serializer.errors) return HttpResponse(json_data, content_type="application/json")
def post(self, request, *args, **kwargs): data = request.data serializer = StudentSerializer(data=data, many=True) if serializer.is_valid(): try: serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) except IntegrityError: return Response("Data already exists", status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, *args, **kwargs): json_data = request.body stream = io.BytesIO(json_data) pythondata = JSONParser().parse(stream) id = pythondata.get('id') stu = Student.objects.get(id=id) serializer = StudentSerializer(stu, data=pythondata, partial=True) if serializer.is_valid(): serializer.save() res = {"msg": "Partial Data Created"} json_data = JSONRenderer().render(res) return HttpResponse(json_data, content_type="application/json") json_data = JSONRenderer().render(serializer.errors) return HttpResponse(json_data, content_type="application/json")
def post(self, request): serializer = StudentSerializer(data=request.data) if serializer.is_valid(): if request.user.is_student(): if request.user.student.id == serializer.validated_data['id']: serializer.save() return Response(serializer.data) else: return Response(status=status.HTTP_401_UNAUTHORIZED) else: return Response(status=status.HTTP_404_NOT_FOUND) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def student_api(request): if request.method == "GET": json_data = request.body stream = io.BytesIO(json_data) pythondata = JSONParser().parse(stream) id = pythondata.get('id', None) if id is not None: stu = Student.objects.get(id=id) serializer = StudentSerializer(stu) json_data = JSONRenderer().render(serializer.data) return HttpResponse(json_data, content_type="application/json") stu = Student.objects.all() serializer = StudentSerializer(stu, many=True) json_data = JSONRenderer().render(serializer.data) return HttpResponse(json_data, content_type="application/json") if request.method == "POST": json_data = request.body stream = io.BytesIO(json_data) pythondata = JSONParser().parse(stream) serializer = StudentSerializer(data=pythondata) if serializer.is_valid(): serializer.save() res = {"msg": "Data Created"} json_data = JSONRenderer().render(res) return HttpResponse(json_data, content_type="application/json") json_data = JSONRenderer().render(serializer.errors) return HttpResponse(json_data, content_type="application/json") if request.method == "PUT": json_data = request.body stream = io.BytesIO(json_data) pythondata = JSONParser().parse(stream) id = pythondata.get('id') stu = Student.objects.get(id=id) serializer = StudentSerializer(stu, data=pythondata, partial=True) if serializer.is_valid(): serializer.save() res = {"msg": "Partial Data Created"} json_data = JSONRenderer().render(res) return HttpResponse(json_data, content_type="application/json") json_data = JSONRenderer().render(serializer.errors) return HttpResponse(json_data, content_type="application/json")
def test_student_data_stored(self): data = [{ "matricNo": "1234567", "givenNames": "Philip J", "surname": "Fry", "academicPlan": "F100-2208", "gradYear": "19-20", "finalAward1": 0.0, "finalAward2": 0.00, "finalAward3": 0.000 }, { "matricNo": "7654321", "givenNames": "Hermes", "surname": "Conrad", "academicPlan": "F100-2208", "gradYear": "19-20", "finalAward1": 0.0, "finalAward2": 0.00, "finalAward3": 0.000 }] serializer = StudentSerializer(data=data, many=True) if serializer.is_valid(): serializer.save() student1 = Student.objects.filter( matricNo="1234567", givenNames="Philip J", surname="Fry", academicPlan="F100-2208", gradYear=GraduationYear.objects.get(gradYear="19-20"), finalAward1=0.0, finalAward2=0.00, finalAward3=0.000).exists() student2 = Student.objects.filter( matricNo="7654321", givenNames="Hermes", surname="Conrad", academicPlan="F100-2208", gradYear=GraduationYear.objects.get(gradYear="19-20"), finalAward1=0.0, finalAward2=0.00, finalAward3=0.000).exists() self.assertTrue(student1) self.assertTrue(student2)
def student_detail(request, pk): try: student = Student.objects.get(pk=pk) except Student.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = StudentSerializer(student) return JsonResponse(serializer.data) if request.method == 'POST': data = JSONParser().parse(request) serializer = StudentSerializer(student, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400) if request.method == 'DELETE': student.delete() return HttpResponse(status=204)
def studentApi(request, id=0): if request.method == 'GET': students = Students.objects.all() student_serializer = StudentSerializer(students, many=True) return JsonResponse(student_serializer.data, safe=False) elif request.method == 'POST': student_data = JSONParser().parse(request) student_serializer = StudentSerializer(data=student_data) if student_serializer.is_valid(): student_serializer.save() return JsonResponse("Added Successfully!", safe=False) return JsonResponse("Failed to add", safe=False) elif request.method == 'PUT': student_data = JSONParser().parse(request) student = Students.objects.get(StudentId=student_data['StudentId']) student_serializer = StudentSerializer(student, data=student_data) if student_serializer.is_valid(): student_serializer.save() return JsonResponse("Updated Successfully!", safe=False) return JsonResponse("Failed to update", safe=False) elif request.method == 'DELETE': student = Students.objects.get(StudentId=id) student.delete() return JsonResponse("Deleted Successfully!", safe=False)
def create(self, request, *args, **kwargs): serializer = StudentSerializer(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)
def post(self, request): lserializer = StudentSerializer(data=request.DATA.get('student')) if lserializer.is_valid(): lserializer.save() return Response(lserializer.data, status=status.HTTP_201_CREATED) return Response(lserializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create(self, request): serializer = StudentSerializer(data=request.data, context={'request': request}) serializer.is_valid(raise_exception=True) serializer.save(user=request.user) return Response(serializer.data, status=status.HTTP_201_CREATED)