Пример #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
         })
Пример #2
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})
Пример #3
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)
Пример #4
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)