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)
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)
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)
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)
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)
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)
def get(self, request, pk): post = self.get_object(pk) serializer = PostSerializer(post) return Response(serializer.data, status=status.HTTP_200_OK)