コード例 #1
0
 def put(self, request):
     queryset = Question.objects.get(id = request.data['id'])
     serializer = QuestionSerializer(queryset, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_200_OK)
     return Response(serializer.errors, status=status.HTTP_404_NOT_FOUND)
コード例 #2
0
 def post(self, request):
     serializer = QuestionSerializer(data=request.data)
     try:
         if serializer.is_valid():
             serializer.save()
             return Response(serializer.data, status=status.HTTP_201_CREATED)
     except Exception as e:
         print(e)
         return Response(serializer.errors, status=status.HTTP_404_NOT_FOUND)
コード例 #3
0
ファイル: api.py プロジェクト: IrSent/nodeads_ta
def question_list_or_post(request):
    """List all Questions or create a new one."""
    if request.method == 'GET':
        questions = Question.objects.all()
        serializer = QuestionSerializer(questions, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = QuestionSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(user=Profile.objects.get(user=request.user))
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
コード例 #4
0
ファイル: api.py プロジェクト: IrSent/nodeads_ta
def question_list_or_post(request):
    """List all Questions or create a new one."""
    if request.method == 'GET':
        questions = Question.objects.all()
        serializer = QuestionSerializer(questions, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = QuestionSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(user=Profile.objects.get(user=request.user))
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
コード例 #5
0
ファイル: api.py プロジェクト: IrSent/nodeads_ta
def question_get_edit_delete(request, pk):
    """Get, Edit or Delete specific Question if you are an owner."""
    try:
        question = Question.objects.get(pk=pk)
    except Question.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = QuestionSerializer(question)
        return Response(serializer.data)

    if request.method == 'PUT':
        serializer = QuestionSerializer(question, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    if request.method == 'DELETE':
        question.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
コード例 #6
0
ファイル: api.py プロジェクト: IrSent/nodeads_ta
def question_get_edit_delete(request, pk):
    """Get, Edit or Delete specific Question if you are an owner."""
    try:
        question = Question.objects.get(pk=pk)
    except Question.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = QuestionSerializer(question)
        return Response(serializer.data)

    if request.method == 'PUT':
        serializer = QuestionSerializer(question, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    if request.method == 'DELETE':
        question.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
コード例 #7
0
ファイル: api.py プロジェクト: hamzatekin/trivia_app
 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)