Esempio n. 1
0
class TestCartManagement(EasyShopTestCase):
    """
    """
    def afterSetUp(self):
        """
        """
        super(TestCartManagement, self).afterSetUp()
        self.cm = ICartManagement(self.shop)

    def testCreateCart_1(self):
        """Create cart for anonymous.
        """
        self.logout()
        cart = self.cm.createCart()        
        
        self.assertEqual(cart.getId(), "123")
                
    def testCreateCart_2(self):
        """Create cart for member.
        """
        self.login("newmember")        
        cart = self.cm.createCart()        
        
        self.assertEqual(cart.getId(), "newmember")
                
    def testDeleteCart_1(self):
        """Without given id.
        """
        self.login("newmember")
        self.cm.createCart()        

        self.failUnless(self.shop.carts.get("newmember"))                
        self.cm.deleteCart()
        self.failIf(self.shop.carts.get("newmember"))
        
    def testDeleteCart_2(self):
        """With given id.
        """
        self.login("newmember")
        self.cm.createCart()

        self.failUnless(self.shop.carts.get("newmember"))                
        self.cm.deleteCart("newmember")
        self.failIf(self.shop.carts.get("newmember"))
        
    def testGetCart_1(self):
        """Cart for anonmyous. There is no cart yet.
        """
        self.logout()
        cart = self.cm.getCart()
        self.assertEqual(cart, None)
            
    def testGetCart_2(self):
        """Cart for anonymous. There is a cart
        """
        self.logout()
        self.cm.createCart()
        
        cart = self.cm.getCart()
        self.assertEqual(cart.getId(), "123")

    def testGetCart_3(self):
        """Cart for member. There is no anonymous cart. Carts are only created
        when items are added to it.
        """
        self.login("newmember")
        
        cart = self.cm.getCart()
        self.assertEqual(cart, None)

    def testGetCart_4(self):
        """Cart for member. There is an anonymous cart.
        """
        self.logout()
        
        # create cart for anonymous
        self.cm.createCart()
        
        cart = self.cm.getCart()
        self.assertEqual(cart.getId(), "123")

        self.login("newmember")

        # After login the anonymous cart should be moved to member cart
        cart = self.cm.getCart()
        self.assertEqual(cart.getId(), "newmember")
        
        # The cart for anonymous has to be deleted
        self.failIf(self.shop.carts.get("123"))
        
    def testGetCarts(self):
        """
        """
        # create cart for newmember
        self.login("newmember")            
        self.cm.createCart()

        self.setRoles(("Manager",))
        
        ids = [c.getId for c in self.cm.getCarts()]
        self.assertEqual(ids, ["newmember"])
        
    def testGetCartById(self):
        """
        """
        # create cart for newmember
        self.login("newmember")    
        self.cm.createCart()

        cart = self.cm.getCartById("newmember")
        self.assertEqual(cart.getId(), "newmember")
        
    def testGetCartByUID(self):
        """
        """
        # create cart for newmember
        self.login("newmember")    
        cart = self.cm.createCart()

        cart = self.cm.getCartByUID(cart.UID())
        
        self.assertEqual(cart.getId(), "newmember")

    def testHasCart(self):
        """
        """ 
        self.assertEqual(self.cm.hasCart(), False)

        self.login("newmember")    
        cart = self.cm.createCart()
        self.assertEqual(self.cm.hasCart(), True)
                    
    def test_getCartId(self):
        """
        """
        self.logout()
        cart_id = self.cm._getCartId()        
        self.assertEqual(cart_id, "123")
        
        self.login("newmember")
        cart_id = self.cm._getCartId()        
        self.assertEqual(cart_id, "newmember")
Esempio n. 2
0
    def getCarts(self):
        """
        """
        sort_on    = self.request.get("sort_on", "modified")
        sort_order = self.request.get("sort_order", "descending")

        shop = IShopManagement(self.context).getShop()
        cm = ICartManagement(shop)

        ttool = getToolByName(self.context, 'translation_service')
                
        result = []
        
        for cart in cm.getCarts():

            cart_object = cart.getObject()
            
            # items
            im = IItemManagement(cart_object)
            items = im.getItems()
            amount_of_items = len(items)
            
            if len(items) > 0:
                last_item = items[-1]
                modified = last_item.modified()
            else:
                modified = cart.modified
             
            # price
            price_float = IPrices(cart_object).getPriceGross()
            price = ICurrencyManagement(shop).priceToString(price_float)
                        
            # created
            created = ttool.ulocalized_time(cart.created, long_format=True)
            modified = ttool.ulocalized_time(modified, long_format=True)
            
            result.append({
                "id"              : cart.getId,
                "url"             : cart.getURL(),
                "created"         : created,
                "modified"        : modified,
                "amount_of_items" : amount_of_items,
                "price_float"     : price_float,             # for sorting reasons
                "price"           : price,
            })

        # There is no index for price, amount_of_items, modified (the
        # modification date of the cart is dependend on the items,
        # so default modification date is not working). So we have 
        # to order the result in another way
        
        # Yes. there is a index for id and created, but to differ makes the 
        # code more complicate than it gains speed, imo. In addition that this
        # is (just) a admin view.
        
        if sort_order == "descending":
            result.sort(lambda a, b: cmp(b[sort_on], a[sort_on]))
        else:
            result.sort(lambda a, b: cmp(a[sort_on], b[sort_on]))                
                
        return result