예제 #1
0
 def put(self, request, pk):
     post = self.get_object(pk)
     serializer = PostSerializer(instance=post, 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_500_INTERNAL_SERVER_ERROR)
예제 #2
0
 def own(self, request, pk=None):
     queryset = self.get_queryset()
     queryset = queryset.filter(author=request.user)
     serializer = PostSerializer(queryset,
                                 many=True,
                                 context={'request': request})
     return Response(serializer.data, status=status.HTTP_200_OK)
예제 #3
0
def post_detail(request, pk):
    """
    Retrieve, update or delete user
    You can only change fields 'like' 
    :param request: request
    :param pk: Post's id in db
    :return: Response
    """
    try:
        snippet = Post.objects.get(pk=pk)
    except User.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = PostSerializer(snippet)
        return Response(serializer.data)

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

    elif request.method == 'DELETE':
        snippet.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
예제 #4
0
 def search(self, request, pk=None):
     q = request.query_params.get('q')
     queryset = self.get_queryset()
     queryset = queryset.filter(
         Q(title__icontains=q) | Q(text__icontains=q))
     serializer = PostSerializer(queryset,
                                 many=True,
                                 context={'request': request})
     return Response(serializer.data, status=status.HTTP_200_OK)
예제 #5
0
def post_list(request, num):
    """
	List all of the available comments (for now). Will later only show friends'
	"""
    if request.method == 'GET':
        posts = _get_friend_posts(num)[:5]
        serializer = PostSerializer(posts, many=True)
        return JsonResponse(serializer.data, status=200, safe=False)

    else:
        return JsonResponse({}, status=404)
예제 #6
0
def posts_list(request):
    queryset = Post.objects.all()
    serializer = PostSerializer(queryset, many=True, context={'request': request})
    return Response(data=serializer.data, status=status.HTTP_200_OK)
예제 #7
0
 def get(self, request, pk):
     post = self.get_object(pk)
     serializer = PostSerializer(post)
     return Response(serializer.data, status=status.HTTP_200_OK)