def put(self, request, pk, format=None): snippet = self.get_object(pk) serializer = QuestionSerializer(snippet, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def poll(request): if request.method == 'GET': questions = Question.objects.all() serializer = QuestionSerializer(questions, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': json_parser = JSONParser() data = json_parser.parse(request) serializer = QuestionSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def question_list(request): """ List all code snippets, or create a new snippet. """ if request.method == 'GET': questions = Question.objects.all() serializer = QuestionSerializer(questions, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = QuestionSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def post(self, request, pk, format='json'): poll = Poll.objects.get(id=pk) serializer = QuestionSerializer(data=request.data) if serializer.is_valid(): question = serializer.save(poll=poll) if question: return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def poll_details(request, pk): try: instance = Question.objects.get(id=pk) except Question.DoesNotExist as e: return JsonResponse({'error': 'Question objects is DoseNotExist'}, status=404) if request.method == 'GET': serializer = QuestionSerializer(instance=instance) return JsonResponse(serializer.data, status=200) if request.method == 'PUT': json_parser = JSONParser() data = json_parser.parse(request) serializer = QuestionSerializer(instance, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=200) return JsonResponse(serializer.errors, status=400) if request.method == 'DELETE': instance.delete() return JsonResponse({'messgae': 'Objects is delete'}, status=204)
def create(self, request, **kwargs): try: poll = Poll.objects.get(id=kwargs.get('poll_pk')) except ObjectDoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) request.data.update(poll=poll) serializer = QuestionSerializer(data=request.data) if serializer.is_valid(): inst = serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def update(self, request, pk=None, poll_pk=None): try: poll = Poll.objects.get(id=poll_pk) except ObjectDoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if not poll.questions.filter(id=pk).exists(): return Response("question id %s not associated with Poll %s " % (pk, poll_pk), status=status.HTTP_400_BAD_REQUEST) question = poll.questions.get(id=pk) request.data.update(poll=poll) serializer = QuestionSerializer(question, data=request.data) if serializer.is_valid(): inst = serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None): serializer = QuestionSerializer(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)