Esempio n. 1
0
    def detail(self, request, *args, **kwargs):
        post = self.load_post()
        if not post:
            return self.resource_not_found()

        comment = self.get_comment(post)
        if not comment:
            return self.resource_not_found()

        return self.response(data=comment_to_dict(request, comment))
Esempio n. 2
0
    def list(self, request, *args, **kwargs):
        post = self.load_post()
        if not post:
            return self.resource_not_found()

        queryset = Comment.objects.filter(post=post)
        object_list, pagination = self.paginate_objects(request, queryset)

        data = {
            'pagination': pagination,
            'data': [
                comment_to_dict(request, comment) for comment in object_list
            ]
        }

        return self.response(data=data)
Esempio n. 3
0
    def create(self, request, *args, **kwargs):
        post = self.load_post()
        if not post:
            return self.resource_not_found()

        form = CommentForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.user = request.user
            comment.save()
            return self.response(
                data=comment_to_dict(request, comment),
                status_code=status.HTTP_201_CREATED
            )
        else:
            return self.response(
                data={'error': form.errors},
                status_code=status.HTTP_400_BAD_REQUEST
            )
Esempio n. 4
0
    def partial_update(self, request, *args, **kwargs):
        post = self.load_post()
        if not post:
            return self.resource_not_found()

        comment = self.get_comment(post)
        if not comment:
            return self.resource_not_found()

        data = model_to_dict(comment)
        data.update(request.PATCH.dict())
        form = CommentForm(data=data, files=request.FILES, instance=comment)
        if form.is_valid():
            comment = form.save()
            return self.response(
                data=comment_to_dict(request, comment),
                status_code=status.HTTP_200_OK
            )
        else:
            return self.response(
                data={'error': form.errors},
                status_code=status.HTTP_400_BAD_REQUEST
            )