Пример #1
0
    def test_validate_invalid_ip_address(self):
        self.data['ip_address'] = '127,0,0,1'
        serializer = ReviewSerializer(data=self.data)

        self.assertIsInstance(serializer, serializers.ModelSerializer)
        self.assertFalse(serializer.is_valid())
        self.assertEqual(len(serializer.errors.get('ip_address')), 1)
Пример #2
0
    def post(self, request, place_id, format=None):
        """
        Creates a Review
        """

        try:
            place = Place.objects.get(id=place_id)
        except ObjectDoesNotExist:
            raise NotFound(detail="Place not found")

        data = JSONParser().parse(request)

        if 'user_id' not in data:
            raise ParseError(detail="Missing user_id", code=400)

        try:
            user = User.objects.get(id=data['user_id'])
        except ObjectDoesNotExist:
            raise NotFound(detail="User not found")

        if 'text' not in data:
            raise ParseError(detail="Missing text", code=400)

        data["place"] = place.id
        data["user"] = user.id

        serializer = ReviewSerializer(data=data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #3
0
def users_review_detail(request, pk):
    if request.user.is_anonymous:
        return Response(status=status.HTTP_401_UNAUTHORIZED)
    else:
        try:
            review = Review.objects.for_user(request.user).get(id=pk)
        except Review.DoesNotExist as e:
            return Response({'error': f'{e}'},
                            status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = ReviewSerializer(review)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = ReviewSerializer(instance=review, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors,
                        status=status.HTTP_500_INTERNAL_SERVER_ERROR)

    elif request.method == 'DELETE':
        review.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
    def post(self, request):
        data = json.loads(request.body)
        serializer = ReviewSerializer(data=data)
        if not serializer.is_valid():
            return JsonResponse({'errors': serializer.errors},
                                status=status_codes.HTTP_400_BAD_REQUEST)
        clean_data = serializer.validated_data

        business = User.objects.filter(
            username=clean_data['business_username'],
            is_business=True).first()
        if not business:
            return JsonResponse({'errors': 'Business ID Provided is invalid'},
                                status=status_codes.HTTP_403_FORBIDDEN)

        user = User.objects.get(id=request.user.id)
        if not user:
            return JsonResponse(
                {'error': 'user not found'},
                status=status_codes.HTTP_500_INTERNAL_SERVER_ERROR)

        model = BusinessReviews(
            user_id=request.user.id,
            user_name=user.name,
            business_username=clean_data['business_username'],
            review_text=clean_data['review_text'],
            stars=clean_data['stars'])
        model.save()
        return JsonResponse({'data': model.to_dict()},
                            status=status_codes.HTTP_200_OK)
Пример #5
0
def review_detail(request, pk):
    try:
        review = Review.objects.get(id=pk)
    except Review.DoesNotExist as e:
        return JsonResponse({'error': str(e)})
    if request.method == 'GET':
        serializer = ReviewSerializer(review)
        if request.user == review.created_by:
            return JsonResponse(serializer.data)
        else:
            JsonResponse({'error': 'No access'})
    elif request.method == 'PUT':
        body = json.loads(request.body)
        serializer = ReviewSerializer(instance=review, data=body)
        if serializer.is_valid():
            if request.user == review.created_by:
                serializer.save()
                return JsonResponse(serializer.data)
            else:
                JsonResponse({'error': 'No access'})
        return JsonResponse(serializer.errors)
    elif request.method == 'DELETE':
        if request.user == review.created_by:
            review.delete()
            return JsonResponse({})
        else:
            JsonResponse({'error': 'No access'})
    return JsonResponse({'error': 'bad request'})
Пример #6
0
    def test_validate_beyond_five_rating(self):
        self.data['rating'] = 6
        serializer = ReviewSerializer(data=self.data)

        self.assertIsInstance(serializer, serializers.ModelSerializer)
        self.assertFalse(serializer.is_valid())
        self.assertEqual(len(serializer.errors.get('rating')), 1)
Пример #7
0
    def test_validate_long_summary(self):
        self.data['summary'] = ''.join(['a' for i in range(10001)])
        serializer = ReviewSerializer(data=self.data)

        self.assertIsInstance(serializer, serializers.ModelSerializer)
        self.assertFalse(serializer.is_valid())
        self.assertEqual(len(serializer.errors.get('summary')), 1)
Пример #8
0
    def test_validate_long_title(self):
        self.data[
            'title'] = 'This is a very very very very very very very very very long title'
        serializer = ReviewSerializer(data=self.data)

        self.assertIsInstance(serializer, serializers.ModelSerializer)
        self.assertFalse(serializer.is_valid())
        self.assertEqual(len(serializer.errors.get('title')), 1)
Пример #9
0
 def post(self, request, pk):
     request.data['movie_id']=pk
     serializer = ReviewSerializer(data=request.data)
     if serializer.is_valid():
         # serializer.movie_id = pk
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Пример #10
0
 def post(self, request, *args, **kwargs):
     data = request.data
     data['ip_address'] = request.META.get('REMOTE_ADDR')
     serializer = ReviewSerializer(data=data)
     if serializer.is_valid():
         serializer.save(user=request.user)
         return Response(serializer.data, status.HTTP_201_CREATED)
     return Response(serializer.errors, status.HTTP_400_BAD_REQUEST)
Пример #11
0
 def create(self, request):
     serializer = ReviewSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save(card_id=request.data['card'])
         card = Card.objects.get(pk=request.data['card'])
         card.review(request.data['answer_quality'])
         card.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #12
0
 def post(self, request):
     request.data["reviewer"] = request.user.id
     ip = get_client_ip(request)[0]
     request.data["ip"] = self.convert_ip(ip)
     serializer = ReviewSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response({'error': serializer.errors},
                     status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Пример #13
0
 def put(self, request, review_id):
     review = self.get_object(review_id)
     request.data["reviewer"] = request.user.id
     ip = get_client_ip(request)[0]
     request.data["ip"] = self.convert_ip(ip)
     serializer = ReviewSerializer(instance=review, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_200_OK)
     return Response({'error': serializer.errors},
                     status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Пример #14
0
def review_list(request):
  if request.method == "GET":
    reviews = Review.objects.all()
    ser = ReviewSerializer(reviews, many=True)
    return JsonResponse(ser.data, safe=False)
  elif request.method == "POST":
    data = JSONParser().parse(request)
    ser = ReviewSerializer(data=data)
    if ser.is_valid():
      ser.save()
      return JsonResponse(ser.data, status=201)
    return JsonResponse(ser.errors, status=400)
Пример #15
0
def createReview(request, pk):
    if request.user.is_anonymous == False:

        serializer = ReviewSerializer(data=request.data)
        if serializer.is_valid():
            product = Product.objects.get(id=pk)
            serializer.save(user=request.user, product=product)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors,
                        status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    else:
        return Response(status=status.HTTP_401_UNAUTHORIZED)
Пример #16
0
    def post(self, request):
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[0]
        else:
            ip = request.META.get('REMOTE_ADDR')

        serializer = ReviewSerializer(data=request.data)
        if serializer.is_valid():
            serializer.validated_data["ip_address"] = ip
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors)
Пример #17
0
    def post(self, request):
        ip_address = request.META.get('HTTP_X_FORWARDED_FOR')
        if ip_address:
            ip = ip_address.split(',')[0]
        else:
            ip = request.META.get('REMOTE_ADDR')

        serializer = ReviewSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            serializer.validated_data['client_ip'] = ip
            serializer.save()
            return Response(status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #18
0
    def test_deserialize_data(self):
        serializer = ReviewSerializer(data=self.data)

        self.assertIsInstance(serializer, serializers.ModelSerializer)
        self.assertTrue(serializer.is_valid())

        validated_data = serializer.validated_data

        self.assertEqual(validated_data.get('title'), 'My review')
        self.assertEqual(validated_data.get('summary'),
                         'This is my first review.')
        self.assertEqual(validated_data.get('rating'), 1)
        self.assertEqual(validated_data.get('ip_address'), '127.0.0.1')
        self.assertEqual(validated_data.get('company'), 'Some Company')
        self.assertEqual(validated_data.get('reviewer'), 'Some Reviewer')
Пример #19
0
    def put(self, request, review_id, format=None):
        """ Updates a City """

        try:
            review = Review.objects.get(id=review_id)
        except ObjectDoesNotExist:
            raise NotFound(detail="Review not found")

        data = JSONParser().parse(request)

        # data["state"] = city.state.id
        serializer = ReviewSerializer(review, data=data, partial=True)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #20
0
    def post(self, request):
        review_serializer = ReviewSerializer(data=request.data)
        if review_serializer.is_valid():
            review_serializer.save()
            beer_id = request.data.get('beer_id')
            beer = Beer.objects.get(beer_id=beer_id)
            avg_rate = Review.objects.filter(beer_id=beer_id).aggregate(
                Avg('rate'))
            avg_value = round(avg_rate['rate__avg'], 2)
            beer.total_rate = str(avg_value)
            beer.save()
            result = {'result': 'success'}
            result.update(review_serializer.data)
            result.update({'total_rate': beer.total_rate})

            return Response(result, status=status.HTTP_201_CREATED)
        else:
            return Response(review_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Пример #21
0
 def post(self, request, *args, **kwargs):
     '''
     creating new reviews
     '''
     try:
         business = Business.objects.get(id=self.kwargs['id'])
         data = {
             'review': request.data['review'],
             'business': business.id,
             'reviewed_by': request.user.id
         }
         serializer = ReviewSerializer(data=data)
         if serializer.is_valid():
             serializer.save()
             return Response(serializer.data,
                             status=status.HTTP_201_CREATED)
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
     except Business.DoesNotExist:
         return Response(status=status.HTTP_404_NOT_FOUND)
    def post(self, request):
        data = json.loads(request.body)
        serializer = ReviewSerializer(data=data)
        if not serializer.is_valid():
            return JsonResponse({'errors': serializer.errors},
                                status=status_codes.HTTP_400_BAD_REQUEST)
        clean_data = serializer.validated_data

        # TODO : re-add this when business system is done
        # business = Business.objects.filter(
        #     id=clean_data['business_id']).first()
        # if not business:
        #     return JsonResponse({'errors': 'Business ID Provided is invalid'},
        #                         status=status_codes.HTTP_403_FORBIDDEN)

        model = BusinessReviews(user_id=request.user.id,
                                business_id=clean_data['business_id'],
                                review_text=clean_data['review_text'],
                                stars=clean_data['stars'])
        model.save()
        return JsonResponse({'data': model.to_dict()},
                            status=status_codes.HTTP_200_OK)
Пример #23
0
 def test_validation(self):
     self.review_data['review'] = 6
     serializer = ReviewSerializer(data=self.review_data)
     self.assertFalse(serializer.is_valid())