Exemplo n.º 1
0
    def add_to_cart(self, product, quantity=1):
        """
        Add a new product into the Shopping Cart.
        If the product is already in the cart then
        update the quantity.
        If not then create a new CartItem and set
        appropriate value for its variable and save.
        This method does nothing when 'quantity' <  1
        """
        added = False
        if quantity >= 0:
            if CartService.contains_item(self.id, product.id):
                print("Product Already in Cart")
                # ci = CartItem.objects.get(product=product)
                # ci = CartItem.objects.get(cart=self, product=product)
                ci = self.cartitem_set.get(product=product)
                added = self.update_quantity(ci.id, quantity)

            # check if this product is already in the cart.
            # if yes, then check if 'quantity' is not greater than
            # that we have in stock.
            # if it is lower then update the quantity in stock

            else:
                # create and save a new cart item
                if (quantity <= (product.quantity - product.sell_quantity)):
                    item = CartItem()
                    item.set_product(product)
                    # this might throw an exception
                    item.set_quantity(quantity)
                    item.set_cart(self)
                    item.save()
                    added = True
        return added
Exemplo n.º 2
0
 def contain_item(self, item_id):
     """
     Deprecated : this method is slow on query.
     Use the CartService.contains_items() from 
     cart.cart_service instead
     """
     """
     flag = False
     try:
         start_time = datetime.datetime.now()
         prod = Product.objects.get(pk=int(item_id))
         item = self.cartitem_set.get(product=prod)
         if item is not None:
             flag = True
         end_time = datetime.datetime.now()
         elapsed_time = end_time - start_time
         print("Cart : contain_item() processing time : {0} ms".format(elapsed_time.microseconds / 1000))
     except ObjectDoesNotExist as e:
         flag = False
     """
     return CartService.contains_item(self.id, item_id)
Exemplo n.º 3
0
    def update_cart(self, item_id, quantity):
        """
        item_id : ID of the Product to be updated
        quantity:  new quantity of the item .
        precondition : quantity >= 0;
        update_cart() updates the quantity value of a CartItem.
        If quantity == 0 then that item will be removed from the cart.
        if quantity < 0 this method returns false.
        On success this method returns True.

        Update :11.03.2018 : this method returns an object which
        contains more infos on the result.
        """
        response = {}
        if (quantity >= 0):
            if (CartService.contains_item(self.id, item_id)):
                prod = Product.objects.get(pk=int(item_id))
                item = self.cartitem_set.get(product=prod)
                # item = self.get_item(item_id)
                # item_quantity = item.get_quantity()
                if quantity > 0:
                    in_stock = prod.quantity - prod.sell_quantity
                    if (quantity <= in_stock):
                        item.set_quantity(quantity)
                        item.save()
                        response['updated'] = True
                        response['quantity'] = quantity
                    else:
                        response['updated'] = False
                        response['quantity_error'] = True
                        response['quantity'] = item.quantity

                else:
                    response['updated'] = self.remove_from_cart(item.id)
                    response['quantity'] = 0
        return response