Пример #1
0
 def list(self, request, listing_pk=None):
     queryset = self.get_queryset().filter(listing=listing_pk).order_by('-edited_date')
     # it appears that because we override the queryset here, we must
     # manually invoke the pagination methods
     page = self.paginate_queryset(queryset)
     if page is not None:
         serializer = serializers.ReviewSerializer(page,
             context={'request': request}, many=True)
         return self.get_paginated_response(serializer.data)
     serializer = serializers.ReviewSerializer(queryset, many=True,
         context={'request': request})
     return Response(serializer.data)
Пример #2
0
    def create(self, request, listing_pk=None):
        """
        Create a new review
        """
        listing = model_access.get_listing_by_id(request.user.username,
                                                 listing_pk, True)

        serializer = serializers.ReviewSerializer(data=request.data,
                                                  context={
                                                      'request': request,
                                                      'listing': listing
                                                  },
                                                  partial=True)
        if not serializer.is_valid():
            logger.error('{0!s}'.format(serializer.errors),
                         extra={'request': request})
            raise errors.ValidationException('{0}'.format(serializer.errors))

        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)
Пример #3
0
    def update(self, request, pk=None, listing_pk=None):
        """
        Update an existing review
        """
        listing = model_access.get_listing_by_id(request.user.username,
                                                 listing_pk, True)
        review = model_access.get_review_by_id(pk)

        serializer = serializers.ReviewSerializer(review,
                                                  data=request.data,
                                                  context={
                                                      'request': request,
                                                      'listing': listing
                                                  },
                                                  partial=True)
        if not serializer.is_valid():
            logger.error('{0!s}'.format(serializer.errors),
                         extra={'request': request})
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
        serializer.save()
        return Response(serializer.data, status=status.HTTP_200_OK)
Пример #4
0
 def retrieve(self, request, pk=None, listing_pk=None):
     queryset = self.get_queryset().get(pk=pk, listing=listing_pk)
     serializer = serializers.ReviewSerializer(queryset,
                                               context={'request': request})
     return Response(serializer.data)