Ejemplo n.º 1
0
    def get(self, request, pk, pk_alt, format=None):

        #pk = self.kwargs.get('pk')
        orderitems = self.request.user.enterprise.orderitem.filter(order=pk,
                                                                   id=pk_alt)
        serializer = OrderItemSerializer(orderitems, many=True)
        return Response(serializer.data)
Ejemplo n.º 2
0
    def items(self, request):
        """
        Get shopping cart items.
        """
        cart_obj = self.get_cart(request.user)
        if not cart_obj:
            return self.create_response(data=[])

        queryset = self.get_cart_items(cart_obj)

        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = OrderItemSerializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = OrderItemSerializer(queryset, many=True)
        return self.create_response(data=serializer.data)
Ejemplo n.º 3
0
    def test_option_exist(self):
        """test if option sort exists"""

        # right
        serializer = OrderItemSerializer(
            data={
                'product':
                self.product2.pk,
                'choices': [{
                    'option_group_id': self.option_group1.sort,
                    'choosed_option_id': self.option1.sort
                }, {
                    'option_group_id': self.option_group2.sort,
                    'choosed_option_id': self.option3.sort
                }]
            })
        self.assertTrue(serializer.is_valid())

        # wrong option group sort
        serializer = OrderItemSerializer(
            data={
                'product':
                self.product2.pk,
                'choices': [
                    {
                        'option_group_id': self.option_group1.sort,
                        'choosed_option_id': 32
                    },  # wrong
                    {
                        'option_group_id': self.option_group2.sort,
                        'choosed_option_id': self.option3.sort
                    }
                ]
            })
        self.assertFalse(serializer.is_valid())
Ejemplo n.º 4
0
    def test_duplicate_choice(self):
        """test if there are any duplicate choices"""

        # right
        serializer = OrderItemSerializer(
            data={
                'product':
                self.product2.pk,
                'choices': [{
                    'option_group_id': self.option_group1.sort,
                    'choosed_option_id': self.option1.sort
                }, {
                    'option_group_id': self.option_group2.sort,
                    'choosed_option_id': self.option3.sort
                }]
            })
        self.assertTrue(serializer.is_valid())

        # wrong -- duplicate choices
        serializer = OrderItemSerializer(
            data={
                'product':
                self.product2.pk,
                'choices': [{
                    'option_group_id': self.option_group1.sort,
                    'choosed_option_id': self.option1.sort
                }, {
                    'option_group_id': self.option_group1.sort,
                    'choosed_option_id': self.option1.sort
                }]
            })
        self.assertFalse(serializer.is_valid())
Ejemplo n.º 5
0
    def test_addon_exist(self):
        """test for addons sort exist"""

        # right
        serializer = OrderItemSerializer(
            data={
                'product':
                self.product2.pk,
                'add_ons_sorts': [self.addon1.sort, self.addon2.sort],
                'choices': [{
                    'option_group_id': self.option_group1.sort,
                    'choosed_option_id': self.option1.sort
                }, {
                    'option_group_id': self.option_group2.sort,
                    'choosed_option_id': self.option3.sort
                }]
            })
        self.assertTrue(serializer.is_valid())

        # wrong addons sorts
        serializer = OrderItemSerializer(
            data={
                'product':
                self.product2.pk,
                'add_ons_sorts': [123, 898],
                'choices': [{
                    'option_group_id': self.option_group1.sort,
                    'choosed_option_id': self.option1.sort
                }, {
                    'option_group_id': self.option_group2.sort,
                    'choosed_option_id': self.option3.sort
                }]
            })
        self.assertFalse(serializer.is_valid())
Ejemplo n.º 6
0
    def list(self, request, *args, **kwargs):
        customer = request.user
        self.check_object_permissions(self.request, customer)
        order, created = Order.objects.get_or_create(customer=customer,
                                                     complete=False)
        items = order.order_items
        serializer = OrderItemSerializer(items, many=True)

        resp = {
            "total_items": order.get_cart_items,
            "total_amount": order.get_cart_total,
            "products": serializer.data,
        }

        return Response(resp)
Ejemplo n.º 7
0
    def test_product_exists(self):
        """test if product really exists"""

        # right
        serializer = OrderItemSerializer(data={'product': self.product.pk})
        self.assertTrue(serializer.is_valid())

        # non existing pk for a product
        serializer = OrderItemSerializer(data={'product': 12345})
        self.assertFalse(serializer.is_valid())
Ejemplo n.º 8
0
    def test_product_available(self):
        """test if a product is marked as not available"""

        # change it temporally
        self.product.is_available = False
        self.product.save()

        # wrong because it is not available
        serializer = OrderItemSerializer(data={'product': self.product.pk})
        self.assertFalse(serializer.is_valid())

        # revert it back
        self.product.is_available = True
        self.product.save()

        # wrong because it is not available
        serializer = OrderItemSerializer(data={'product': self.product.pk})
        self.assertTrue(serializer.is_valid())
Ejemplo n.º 9
0
    def update_cart(self, request):
        request_data = self.get_serializer(data=request.data)
        request_data.is_valid(raise_exception=True)

        request_action = request_data.data["action"]
        product_id = request_data.data["productId"]
        customer = request.user
        self.check_object_permissions(self.request, customer)

        product = Product.objects.get(id=product_id)
        order, created = Order.objects.get_or_create(customer=customer,
                                                     complete=False)

        order_item, created = OrderItem.objects.get_or_create(order=order,
                                                              product=product)

        if request_action == "add":
            order_item.quantity += 1
        elif request_action == "remove":
            order_item.quantity -= 1

        order_item.save()

        if order_item.quantity <= 0:
            order_item.delete()

        items = order.order_items
        serializer = OrderItemSerializer(items, many=True)

        resp = {
            "total_items": order.get_cart_items,
            "total_amount": order.get_cart_total,
            "products": serializer.data,
        }

        return Response(data=resp, status=200)
Ejemplo n.º 10
0
    def test_rely_choosed(self):
        """test if a rely_on choosed or not"""

        # add rely on for test option group2
        RelyOn.objects.create(option_group=self.option_group2,
                              choosed_option_group=self.option_group1,
                              option=self.option1)

        # right and rely_on requirement for option_group2 is fulfilled
        serializer = OrderItemSerializer(
            data={
                'product':
                self.product2.pk,
                'choices': [{
                    'option_group_id': self.option_group1.sort,
                    'choosed_option_id': self.option1.sort
                }, {
                    'option_group_id': self.option_group2.sort,
                    'choosed_option_id': self.option3.sort
                }]
            })
        self.assertTrue(serializer.is_valid())

        # wrong because rely_on requirement {option_group2: option1} is not fulfilled
        serializer = OrderItemSerializer(
            data={
                'product':
                self.product2.pk,
                'choices': [
                    {
                        'option_group_id': self.option_group1.sort,
                        'choosed_option_id': self.option2.sort
                    },  # not 1
                    {
                        'option_group_id': self.option_group2.sort,
                        'choosed_option_id': self.option3.sort
                    }
                ]
            })
        self.assertFalse(serializer.is_valid())
Ejemplo n.º 11
0
    def test_missing_choice_for_option_group(self):
        """test if any choices missing for an option
        group in the product"""

        # right and all choices are choosed
        serializer = OrderItemSerializer(
            data={
                'product':
                self.product2.pk,
                'choices': [{
                    'option_group_id': self.option_group1.sort,
                    'choosed_option_id': self.option1.sort
                }, {
                    'option_group_id': self.option_group2.sort,
                    'choosed_option_id': self.option3.sort
                }]
            })
        self.assertTrue(serializer.is_valid())

        # missing a choice (option group2 not choosed)
        serializer = OrderItemSerializer(
            data={
                'product':
                self.product2.pk,
                'choices': [{
                    'option_group_id': self.option_group1.sort,
                    'choosed_option_id': self.option1.sort
                }]
            })
        self.assertFalse(serializer.is_valid())

        # add rely on for test option group2
        RelyOn.objects.create(option_group=self.option_group2,
                              choosed_option_group=self.option_group1,
                              option=self.option1)

        # wrong because it is not choosed and has a rely on {option_group1: option1}
        # which is choosed, which makes it required
        serializer = OrderItemSerializer(
            data={
                'product':
                self.product2.pk,
                'choices': [{
                    'option_group_id': self.option_group1.sort,
                    'choosed_option_id': self.option1.sort
                }]
            })
        self.assertFalse(serializer.is_valid())

        # right
        # missing a choice but as option group2 relys on choosing {option_group1: option1}
        # which is no choosed, which is makes it unable to be choosed,
        # no error is thrown
        serializer = OrderItemSerializer(
            data={
                'product':
                self.product2.pk,
                'choices': [{
                    'option_group_id': self.option_group1.sort,
                    'choosed_option_id': self.option2.sort
                }]
            })
        self.assertTrue(serializer.is_valid())