def test_cart_to_dict(self):
     expect_cart = {'total_cost': 0, 'total_count': 0,
                    'raw_cart': {
                        'owner': 'hello_world', 'status': 'NEW', u'id': 1, 'closed': False, 'timestamp': 0
                    },
                    'cart_items': [], 'uuid': 'hello_world'}
     self.assertEqual(get_cart_dict(self.cart),expect_cart)
Exemplo n.º 2
0
def add_product_to_cart(request, product_id):
    data = {}
    try:
        product = Product.objects.get(pk=product_id)
    except ObjectDoesNotExist:
        data = {"status": "Fail"}
    else:
        data = {"status": "OK"}
        cart = get_cart(request)
        cart.add_product(product)
        cart.save()

    data.update(get_cart_dict(request))
    return JsonResponse(data)
Exemplo n.º 3
0
def update_cart(request, product_id, qty):
    data = {}
    try:
        product = Product.objects.get(pk=product_id)
    except ObjectDoesNotExist:
        data = {"status": "Fail"}
    else:
        data = {"status": "OK"}
        cart = get_cart(request)
        item, _ = CartItem.objects.get_or_create(cart=cart, product=product)

        item.quantity = qty
        item.save()
    data.update(get_cart_dict(request))
    return JsonResponse(data)
 def test_two_items_append(self):
     self.cart.add_products(self.product, self.product2)
     self.cart.add_products(self.product, self.product2)
     self.cart.save()
     self.assertEqual(self.cart.total_count(),4) # four total items
     self.assertEqual(self.cart.items.count(),2) #two unique items
     items = set(self.cart.items.all())
     pks = set(item.pk for item in items)
     self.assertEqual(pks.intersection([self.product.pk,self.product2.pk]),pks)
     for item in items:
         self.assertEqual(item.quantity,2)
         self.assertEqual(item.cart,self.cart)
     expected_cart = {'total_cost': 9.5, 'total_count': 4,
                      'raw_cart': {
                          'owner': 'hello_world', 'status': 'NEW', u'id': 1, 'closed': False, 'timestamp': 0
                      },
                      'cart_items': [
                          {'product': {
                              'background_image': u'', 'title_color': u'#000000', 'product_name': u'test1',
                              'product_cost': 2.25, 'product_description': u'', 'product_ingredients': u'',
                              u'id': 1
                             }, u'id': 1,
                              'cart': {
                                  'owner': 'hello_world', 'status': 'NEW', u'id': 1, 'closed': False,
                                  'timestamp': 0},
                              'quantity': 2
                          },
                          {'product': {
                              'background_image': u'', 'title_color': u'#000000', 'product_name': u'test2',
                              'product_cost': 2.5, 'product_description': u'', 'product_ingredients': u'', u'id': 2
                              },
                              u'id': 2, 'cart': {
                                 'owner': 'hello_world', 'status': 'NEW', u'id': 1, 'closed': False, 'timestamp': 0
                              }, 'quantity': 2
                          }
                      ], 'uuid': 'hello_world'}
     self.assertEqual(get_cart_dict(self.cart),expected_cart)
Exemplo n.º 5
0
def get_cart_details(request):
    return JsonResponse(get_cart_dict(request))