Ejemplo n.º 1
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)
Ejemplo n.º 2
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'})
Ejemplo n.º 3
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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
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)
Ejemplo n.º 6
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)
Ejemplo n.º 7
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)
Ejemplo n.º 8
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)
Ejemplo n.º 9
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)
Ejemplo n.º 10
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)
Ejemplo n.º 11
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)
Ejemplo n.º 12
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)
Ejemplo n.º 13
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)
Ejemplo n.º 14
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)
Ejemplo n.º 15
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)