示例#1
0
    def test_04_subclasses_product_filters(self):
        class DiscountBaseSubclass(DiscountBase):
            pass

        filt = {'unit_price__gt': 10}
        DiscountBase.register_product_filter(filt)
        self.assertNotEqual(DiscountBase.product_filters,
                            DiscountBaseSubclass.product_filters)
示例#2
0
    def test_04_subclasses_product_filters(self):
        class DiscountBaseSubclass(DiscountBase):
            pass

        filt = {'unit_price__gt': 10}
        DiscountBase.register_product_filter(filt)
        self.assertNotEqual(DiscountBase.product_filters,
                            DiscountBaseSubclass.product_filters)
示例#3
0
    def test_03_register_product_filter_with_callable(self):
        def filt(discount, qs):
            return(qs.filter(name=discount.name))

        DiscountBase.register_product_filter(filt)
        self.assertEquals(len(self.discount.eligible_products()), 0)

        product_discount = create_product(name=self.discount.name)
        self.assertIn(product_discount, self.discount.eligible_products())
示例#4
0
    def test_03_register_product_filter_with_callable(self):
        def filt(discount, qs):
            return (qs.filter(name=discount.name))

        DiscountBase.register_product_filter(filt)
        self.assertEquals(len(self.discount.eligible_products()), 0)

        product_discount = create_product(name=self.discount.name)
        self.discount = DiscountBase.objects.get(pk=self.discount.pk)
        self.assertIn(product_discount, self.discount.eligible_products())
示例#5
0
 def test_02_register_product_filter_with_dict(self):
     filt = {'unit_price__gt': 10}
     DiscountBase.register_product_filter(filt)
     self.assertNotIn(self.product_1, self.discount.eligible_products())
     self.assertIn(self.product_2, self.discount.eligible_products())
示例#6
0
                amount,
            )

    class Meta:
        verbose_name = _('Bulk discount')
        verbose_name_plural = _('Bulk discounts')


# filter function
def category_product_filter(discount, queryset):
    """
    Allow discount type to be filtered by category.
    """
    if not discount.categories.count():
        return queryset
    ids = [c.id for c in discount.categories.all()]
    return queryset.filter(models.Q(Book___categories__id__in=ids))


DiscountBase.register_product_filter(category_product_filter)

#add categories field to BulkDiscount
DiscountBase.add_to_class(
    'categories',
    models.ManyToManyField(
        Category,
        verbose_name=_('Categories'),
        blank=True,
        null=True,
        help_text=_('Limit discount to selected categories')))
示例#7
0
 def test_02_register_product_filter_with_dict(self):
     filt = {'unit_price__gt': 10}
     DiscountBase.register_product_filter(filt)
     self.assertNotIn(self.product_1, self.discount.eligible_products())
     self.assertIn(self.product_2, self.discount.eligible_products())
示例#8
0
            to_append = (self.get_name(), amount)
            cart_item.extra_price_fields.append(to_append)

    class Meta:
        verbose_name = _('Bulk discount')
        verbose_name_plural = _('Bulk discounts')


# filter function
def category_product_filter(discount, queryset):
    """
    Allow discount type to be filtered by category.
    """
    if not discount.categories.count():
        return queryset
    ids = [c.id for c in discount.categories.all()]
    return queryset.filter(models.Q( Book___categories__id__in=ids))

DiscountBase.register_product_filter(category_product_filter)

#add categories field to BulkDiscount
DiscountBase.add_to_class('categories', 
        models.ManyToManyField(Category, 
            verbose_name=_('Categories'),
            blank=True,
            null=True,
            help_text=_('Limit discount to selected categories')
            ))