Exemplo n.º 1
0
 def test_buy_one_get_one_free_example(self):
     """Test as described in the docs
     /signals.html#an-example-of-pre-add-to-basket"""
     # make the product
     hammer = SimpleProduct(
         title="Hammer",
         price="4.99")
     hammer.save()
     # define the receiver
     def example_buy_one_get_one_free(sender,
                 instance,
                 item,
                 quantity,
                 **kwargs):
         # insert a hammer, with title 'buy one get one free' and a price
         # of 0.00
         if item.name == "Hammer":
             instance.add(hammer,
                     price="0.00",
                     description="Free Hammer! Buy One Get One Free",
                     locked=True,
                     silent=True)
     # connect it
     pre_add_to_basket.connect(example_buy_one_get_one_free,
         dispatch_uid='bogof')
     # our basket has no items
     self.assertEqual(0, self.basket.total_items())
     # add one hammer, quantity of one assumed by add method
     basketitem = self.basket.add(hammer)
     # now we have two basketitems
     self.assertEqual(2, self.basket.total_items())
     # The first is the free hammer
     self.assertEqual(
         self.basket.basketitem_set.all()[0].description,
         "Free Hammer! Buy One Get One Free")
     self.assertEqual(
         self.basket.basketitem_set.all()[0].price,
         Decimal("0.00"))
     # it's locked so the user can't mess with it
     self.assertEqual(
         self.basket.basketitem_set.all()[0].locked,
         True)
     # second is the paid for hammer
     self.assertEqual(
         self.basket.basketitem_set.all()[1].description,
         "Hammer")
     self.assertEqual(
         self.basket.basketitem_set.all()[1].price,
         Decimal("4.99"))
     self.assertEqual(
         self.basket.basketitem_set.all()[1].locked,
         False)
     # disconnect it
     pre_add_to_basket.disconnect('bogof')
Exemplo n.º 2
0
 def test_pre_add_to_basket_modified_product(self):
     # define a reciver that provides some overrides
     def modify_product(sender, instance, **kwargs):
         return {
             'price' : Decimal('14000.00'),
             'quantity': 20,
             'description' : 'w00t, w00t. Its da police.'
         }
     # connect it
     pre_add_to_basket.connect(modify_product, dispatch_uid='pre_test_mod')
     # add to basket
     basketitem = self.basket.add(self.item, quantity=1)
     # basket now has 20 items
     self.assertEqual(20, self.basket.total_items())
     # and costs
     self.assertEqual(Decimal('280000.00'), self.basket.total_price())
     # but original item is untouched at the database level
     self.assertEqual(Decimal("199.99"), self.item.price)
Exemplo n.º 3
0
 def test_pre_add_to_basket_kwargs(self):
     """Test pre_add_to_basket is sent"""
     # disconnect the post add to baskets 
     post_add_to_basket.disconnect(dispatch_uid='post_test_rec')
     # were using a global
     global test_dict
     # connect the signal
     pre_add_to_basket.connect(test_receiver, dispatch_uid='pre_test_rec')
     # test_dict is empty
     self.assertEqual(test_dict.keys(), [])
     # add a product to the basket
     basketitem = self.basket.add(self.item, quantity=2)
     # test_dict now has all the right keys
     self.assertEqual(test_dict['signal'], pre_add_to_basket)
     self.assertEqual(test_dict['sender'], Basket)
     self.assertEqual(test_dict['instance'], self.basket)
     self.assertEqual(test_dict['item'].title, self.item.title)
     self.assertEqual(test_dict['quantity'], 2)
Exemplo n.º 4
0
 def test_do_not_add(self):
     # make the products
     hammer = SimpleProduct(
         title="Hammer",
         price="4.99")
     hammer.save()
     # crude receiver
     def do_not_add(sender,instance, **kwargs):
         return {'do_not_add' : True}
     # connect it
     pre_add_to_basket.connect(do_not_add, dispatch_uid='do_not_add')
     # add to basket - None
     basketitem = self.basket.add(hammer)
     self.assertEqual(0, self.basket.total_items())
     # disconnect it
     pre_add_to_basket.disconnect(dispatch_uid='do_not_add')
     # add to basket now and it will go in
     basketitem = self.basket.add(hammer)
     self.assertEqual(1, self.basket.total_items())
Exemplo n.º 5
0
 def test_pre_add_to_basket_multiple_receivers(self):
     """Attach multiple receivers and make sure that there is no bork"""
     # define three listeners
     def modify_description(sender, instance, **kwargs):
         return {
             'description' : 'Ho ho ho and a bottle of rum'
         }
     
     def modify_quantity(sender, instance, **kwargs):
         return {
             'quantity' : 2
         }
     
     def modify_price(sender, instance, **kwargs):
         return {
             'price' : Decimal('0.01')
         }
     # connect them
     pre_add_to_basket.connect(modify_description, dispatch_uid='md')
     pre_add_to_basket.connect(modify_quantity, dispatch_uid='mq')
     pre_add_to_basket.connect(modify_price, dispatch_uid='mp')
     # no basketitems to start with
     self.assertEqual(0, BasketItem.objects.count())
     #  add to 1 to basket and we should end up with..
     basketitem = self.basket.add(self.item, quantity=1)
     # 2 items
     self.assertEqual(2, BasketItem.objects.all()[0].quantity)
     # costing 0.02
     self.assertEqual(self.basket.total_price(), Decimal('0.02'))
     # disconnect
     pre_add_to_basket.disconnect(dispatch_uid='md')
     pre_add_to_basket.disconnect(dispatch_uid='mq')
     pre_add_to_basket.disconnect(dispatch_uid='mp')
Exemplo n.º 6
0
    @property
    def total(self):
        try:
            for simplediscount in self.simplediscount_set.all():
                logger.debug('applying %s -%s%%' % (simplediscount.title,
                                        simplediscount.discount))
                self.price *= simplediscount.percentage
        except ValueError:
            pass
        return self.price


class SimpleDiscount(models.Model):
    title = models.CharField(max_length=255)
    simpleproducts = models.ManyToManyField(SimpleProduct)
    discount = models.PositiveSmallIntegerField()
    
    def __unicode__(self):
        return u'%s' % self.title
    
    @property
    def percentage(self):
        percentage = Decimal(100 - self.discount)
        percentage = percentage / 100
        return percentage


pre_add_to_basket.connect(simple_discount_description,
                sender=SimpleProduct,
                dispatch_uid="simple_discount_description")