Example #1
0
 def post(self, request, slug, format=None):
     if request.POST.get('delete'):
         self.delete_post(slug)
         request
         redirect('post-list')
     else:
         cats = Category.objects.all()
         post_data = self.get_object(slug)
         post = PostSerializer(post_data,
                               data=request.POST,
                               context={
                                   'user_id': request.user.id,
                                   'request': request,
                                   'category':
                                   request.POST.getlist('category')
                               })
         if post.is_valid():
             post.save()
         return Response({
             'serializer': post,
             'category': cats,
             slug: slug,
             'post': post_data,
             'logged_in_user': request.user
         })
Example #2
0
 def get(self, request, slug, format=None):
     post_data = self.get_object(slug)
     if post_data.blog.author != request.user:
         return Http404
     post = PostSerializer(post_data,
                           many=False,
                           context={'request': request})
     return Response(post.data)
Example #3
0
 def get(self, request, slug):
     post = PostSerializer(self.get_object(slug, request),
                           many=False,
                           context={
                               'request': request,
                               'user_id': request.user.id
                           }).data
     return Response(post)
Example #4
0
    def get(self, request, format=None):
        post = PostSerializer()
        cats = Category.objects.all()

        return Response({
            'serializer': post,
            'category': cats,
            'logged_in_user': request.user
        })
Example #5
0
    def post(self, request, slug, format=None):
        post_data = self.get_object(slug)
        if post_data.blog.author != request.user:
            return Http404
        category_ids = request.POST.getlist('category')
        if not request.POST.getlist('category'):
            category_ids = [0]
        post = utils.update_post(post_data, request.data, category_ids)

        return Response(PostSerializer(post).data)
Example #6
0
 def post(self, request, format=None):
     post = PostSerializer(data=request.POST,
                           context={
                               'user_id': request.user.id,
                               'request': request,
                               'category': request.POST.getlist('category')
                           })
     if post.is_valid():
         post.save()
     return Response({'serializer': post})
Example #7
0
    def retrieve(self, request, pk):
        queryset = Post.objects.filter(blog=pk).order_by('-creation_date')

        if not queryset:
            return Response(status=status.HTTP_400_BAD_REQUEST)
        elif not request.user.is_authenticated or request.user.username != getattr(Blog.objects.get(pk=pk), 'owner').username:
            serializer = PostListSerializer(queryset, many=True)
            return Response(serializer.data, status=status.HTTP_200_OK)
        else:
            serializer = PostSerializer(queryset, many=True)
            return Response(serializer.data, status=status.HTTP_200_OK)
Example #8
0
    def retrieve(self, request, pk):
        """
        Returns a requested user
        :param request: HttpRequest
        :param pk: post primary key
        :return: Response
        """

        post = get_object_or_404(Post, pk=pk)
        self.check_object_permissions(request, post)
        serializer = PostSerializer(post)
        return Response(serializer.data)
Example #9
0
 def get(self, request, slug, format=None):
     cats = Category.objects.all()
     post_data = self.get_object(slug)
     post = PostSerializer(post_data,
                           many=False,
                           context={'request': request})
     return Response({
         'serializer': post,
         'category': cats,
         slug: slug,
         'post': post_data,
         'logged_in_user': request.user
     })
Example #10
0
 def update(self, request, pk):
     post = get_object_or_404(Post, pk=pk)
     self.check_object_permissions(request, post)
     request_copy = request.data.copy()
     request_copy['owner'] = post.owner.id
     request_copy['blog'] = post.blog.id
     serializer = WritePostSerializer(post, data=request_copy)
     if serializer.is_valid():
         updated_post = serializer.save()
         user_serializer = PostSerializer(updated_post)
         return Response(user_serializer.data)
     else:
         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #11
0
    def get(self, request, format=None):
        # categories = utils.get_all_visible_categories()
        cat_slug = request.GET.get('cat', None)
        sort = request.GET.get('sort', None)
        # auth = request.user.is_authenticated()
        posts = Post.objects.filter(is_published=True).order_by('-posted_at')
        if cat_slug:
            posts = posts.filter(category__slug=cat_slug)
        if sort:
            if sort == 'posted_at':
                sort = "-" + sort
            posts = posts.order_by(sort)

        posts = PostSerializer(posts, many=True).data
        return Response(posts)
Example #12
0
 def update(self, request, pk):
     """
     Updates a Post with the given data
     :param request: HttpRequest
     :param pk: Post primary key
     :return: Response
     """
     post = get_object_or_404(Post, pk=pk)
     self.check_object_permissions(request, post)
     serializer = PostSerializer(post, data=request.data)
     if serializer.is_valid():
         post = serializer.save()
         return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
     else:
         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #13
0
    def create(self, request):
        """
        Create a post
        :param request: HttpRequest
        :return: Response
        """

        # image=self.request.data.get('image')

        serializer = PostSerializer(data=request.data)
        if serializer.is_valid():
            post = serializer.save(owner=self.request.user)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #14
0
 def get(self, request, slug):
     categories = utils.get_all_visible_categories()
     auth = request.user.is_authenticated()
     post = PostSerializer(self.get_object(slug),
                           many=False,
                           context={
                               'request': request,
                               'user_id': request.user.id
                           }).data
     related_categories = self.get_object(slug).category.all()
     return Response({
         'post': post,
         'user': auth,
         'categories': categories,
         'logged_in_user': request.user,
         'related_cat': related_categories
     })
Example #15
0
 def get(self, request, format=None):
     post = PostSerializer(Post.objects.all(), many=True).data
     return Response(post)
def api_post(request):
    query = request.GET.get("q", "")
    posts = Post.objects.filter(Q(title__contains=query) | Q(text__contains=query))
    serializer = PostSerializer(posts, many=True)
    return Response(serializer.data)