Exemplo n.º 1
0
    def test_validate_rating(self):
        """
        Test to validate that rating should be in range of 1-5
        """

        self.data['rating'] = 6

        with self.assertRaises(ValidationError):
            serializer = ReviewSerializer(data=self.data)
            serializer.is_valid(raise_exception=True)
Exemplo n.º 2
0
    def test_without_rating(self):
        """
        Test trying to create a review without rating
        """

        del self.data['rating']

        with self.assertRaises(ValidationError):
            serializer = ReviewSerializer(data=self.data)
            serializer.is_valid(raise_exception=True)
Exemplo n.º 3
0
    def test_change_replied_at(self):
        """
        Shouldn't be able to change the replied_at date if is set
        """

        replied_at = datetime.datetime(2015, 11, 8, 3, 30, 0, tzinfo=utc)
        review = ReviewFactory(reply='Replied', replied_at=replied_at)

        serializer = ReviewSerializer(review, data={'rating': 2}, partial=True)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        review = Review.objects.get(id=review.id)
        self.assertEqual(review.replied_at, replied_at)
Exemplo n.º 4
0
    def test_create(self):
        """
        Test create a review with the serializer
        """

        serializer = ReviewSerializer(data=self.data)
        serializer.context = self.context
        serializer.is_valid(raise_exception=True)
        serializer.save()

        self.assertTrue(Review.objects.filter(**self.data).exists())

        # Check the permissions
        perms = ('reviews.report_review', 'reviews.reply_review',
                 'reviews.read_review')
        self.activity.organizer.user.has_perms(perms)
Exemplo n.º 5
0
    def test_validate_calendar_initial_date(self):
        """
        Now() should be grater than calendar.initial_date
        """

        # Set the initial_date 10 days ahead
        self.order.calendar.initial_date = (now() + timedelta(days=10)).date()
        self.order.calendar.save()

        with self.assertRaisesMessage(
                ValidationError,
                "{'non_field_errors': ['La orden no cumple con los "
                "requerimientos para crear un review']}"):
            serializer = ReviewSerializer(data=self.data)
            serializer.context = self.context
            serializer.is_valid(raise_exception=True)
Exemplo n.º 6
0
 def post(self, request):
     data = JSONParser().parse(request)
     serializer = ReviewSerializer(data=data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return JsonResponse(serializer.errors, status=400)
Exemplo n.º 7
0
    def test_validate_reply(self):
        """
        Test can't change reply
        """

        # Arrangement
        review = Review.objects.create(rating=5,
                                       activity=self.activity,
                                       author=self.author,
                                       reply='Replied')
        data = {'reply': 'Replied updated'}

        with self.assertRaisesMessage(
                ValidationError,
                "{'reply': ['No se puede cambiar la respuesta']}"):
            serializer = ReviewSerializer(review, data=data, partial=True)
            serializer.is_valid(raise_exception=True)
Exemplo n.º 8
0
 def create(self, request):
     if request.user.is_authenticated:
         serializer = ReviewSerializer(data=request.data, context={'request': request})
         if serializer.is_valid():
             serializer.save()
             return Response(serializer.data, status=status.HTTP_201_CREATED)
         else:
             return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
     else:
         return Response(status=status.HTTP_401_UNAUTHORIZED)
Exemplo n.º 9
0
    def update(self, request, pk, *args, **kwargs):
        data = request.data
        if (request.data.get('transaction_id') == None):
            transaction = Transaction.objects.get(pk=pk)
            transaction.user = data.get("user", transaction.user)
        else:
            transaction = Transaction.objects.get(
                pk=request.data['transaction_id'])
            request.data['user'] = request.user.id
            RevSerializer = ReviewSerializer(data=request.data)
            RevSerializer.is_valid(raise_exception=True)
            RevSerializer.save()
            transaction_item_current = Transaction_item.objects.get(
                id=request.data['transaction_item_id'])
            transaction_item_current.review = Review.objects.get(
                id=RevSerializer.data["id"])
            transaction_item_current.save()

        transaction.created_at = data.get("created_at", transaction.created_at)
        transaction.totalAmount = data.get("totalAmount",
                                           transaction.totalAmount)
        transaction.quantity = data.get("quantity", transaction.quantity)
        transaction.payment_method = data.get("payment_method",
                                              transaction.payment_method)
        transaction.status = data.get("status", transaction.status)
        transaction.address = data.get("address", transaction.address)
        transaction.contact_number = data.get("contact_number",
                                              transaction.contact_number)
        # transaction.items = data.get("items",transaction.items)
        transaction.order_status = data.get("order_status",
                                            transaction.order_status)
        transaction.tracking_number = data.get("tracking_number",
                                               transaction.tracking_number)
        transaction.save()
        if (request.data.get('transaction_id') == None):
            context = self.get_serializer(transaction).data
        else:
            context = self.get_serializer(
                Transaction.objects.get(
                    id=request.data['transaction_id'])).data
        return Response(context, status=status.HTTP_201_CREATED)
Exemplo n.º 10
0
    def create(self, request, *args, **kwargs):
        data = request.data
        data['author'] = request.user.id

        serializer = ReviewSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            request.user.profile.set_points('REVIEW')

            return Response({'message': 'Review criado com sucesso', **(serializer.data)})
        else:
            return Response({'error': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 11
0
def review_list(self, request):
    if request.method == 'GET':
        queryset = Review.objects.all()
        serializer = ReviewSerializer(queryset, many=True)
        return JsonResponse(serializer.data, safe=False)
    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = ReviewSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)
Exemplo n.º 12
0
def review_create(request, movie_pk):
    if request.user.user_rank.filter(id=movie_pk).exists():
        content = '리뷰는 하나만 작성가능합니다.'
        return Response(content, status=status.HTTP_400_BAD_REQUEST)
    movie = get_object_or_404(Movie, pk=movie_pk)
    serializer = ReviewSerializer(data=request.data)

    UserRank.objects.create(user=request.user,
                            movie=movie,
                            rank=request.data['rank'])
    if serializer.is_valid(raise_exception=True):
        serializer.save(user=request.user, movie=movie)
        return Response(serializer.data)
Exemplo n.º 13
0
    def test_reply(self):
        """
        Test the reply
        """

        # Arrangement
        review = Review.objects.create(rating=5,
                                       activity=self.activity,
                                       author=self.author)
        data = {'reply': 'Replied'}
        replied_at = datetime.datetime(2015, 11, 8, 3, 30, 0, tzinfo=utc)

        with patch('reviews.serializers.now') as mock_now:
            mock_now.return_value = replied_at
            serializer = ReviewSerializer(review, data=data, partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        review = Review.objects.get(id=review.id)
        self.assertEqual(review.reply, 'Replied')
        self.assertEqual(review.replied_at, replied_at)
        self.assertEqual(serializer.data['replied_at'],
                         replied_at.isoformat()[:-6] + 'Z')
Exemplo n.º 14
0
    def create(self, request):
        """
            Function: Create a review
            HTTP Verb: POST
            Endpoint: /reviews

            """
        review_serializer = ReviewSerializer(data=request.data)

        if (review_serializer.is_valid()):
            review_serializer.save()

            response = Response([review_serializer.data],
                                status=status.HTTP_201_CREATED)
        else:
            response = Response(review_serializer.errors,
                                status=status.HTTP_400_BAD_REQUEST)

        return response
Exemplo n.º 15
0
def review_detail(request, id):
    try:
        reviewdetails = Review.objects.get(id=id)
    except ReviewDoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = ReviewSerializer(reviewdetails)
        return JsonResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = ReviewSerializer(companybytype, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        review.delete()
        return HttpResponse(status=204)