_filtered_discounts is now called _filtered_clauses, and it no longer cares about specific products or categories
This commit is contained in:
parent
b3491cab8e
commit
162a1f23dd
1 changed files with 24 additions and 29 deletions
|
@ -50,7 +50,22 @@ class DiscountController(object):
|
||||||
categories and products. The discounts also list the available quantity
|
categories and products. The discounts also list the available quantity
|
||||||
for this user, not including products that are pending purchase. '''
|
for this user, not including products that are pending purchase. '''
|
||||||
|
|
||||||
filtered_clauses = cls._filtered_discounts(user, categories, products)
|
filtered_clauses = cls._filtered_clauses(user, categories, products)
|
||||||
|
|
||||||
|
# clauses that match provided categories
|
||||||
|
categories = set(categories)
|
||||||
|
# clauses that match provided products
|
||||||
|
products = set(products)
|
||||||
|
# clauses that match categories for provided products
|
||||||
|
product_categories = set(product.category for product in products)
|
||||||
|
# (Not relevant: clauses that match products in provided categories)
|
||||||
|
all_categories = categories | product_categories
|
||||||
|
|
||||||
|
filtered_clauses = (
|
||||||
|
clause for clause in filtered_clauses
|
||||||
|
if hasattr(clause, 'product') and clause.product in products or
|
||||||
|
hasattr(clause, 'category') and clause.category in all_categories
|
||||||
|
)
|
||||||
|
|
||||||
discounts = []
|
discounts = []
|
||||||
|
|
||||||
|
@ -84,7 +99,7 @@ class DiscountController(object):
|
||||||
return discounts
|
return discounts
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _filtered_discounts(cls, user, categories, products):
|
def _filtered_clauses(cls, user):
|
||||||
'''
|
'''
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
|
@ -98,37 +113,17 @@ class DiscountController(object):
|
||||||
i for i in types if issubclass(i, conditions.DiscountBase)
|
i for i in types if issubclass(i, conditions.DiscountBase)
|
||||||
]
|
]
|
||||||
|
|
||||||
# discounts that match provided categories
|
product_clauses = conditions.DiscountForProduct.objects.all()
|
||||||
category_discounts = conditions.DiscountForCategory.objects.filter(
|
product_clauses = product_clauses.select_related(
|
||||||
category__in=categories
|
|
||||||
)
|
|
||||||
# discounts that match provided products
|
|
||||||
product_discounts = conditions.DiscountForProduct.objects.filter(
|
|
||||||
product__in=products
|
|
||||||
)
|
|
||||||
# discounts that match categories for provided products
|
|
||||||
product_category_discounts = conditions.DiscountForCategory.objects
|
|
||||||
product_category_discounts = product_category_discounts.filter(
|
|
||||||
category__in=(product.category for product in products)
|
|
||||||
)
|
|
||||||
# (Not relevant: discounts that match products in provided categories)
|
|
||||||
|
|
||||||
product_discounts = product_discounts.select_related(
|
|
||||||
"product",
|
"product",
|
||||||
"product__category",
|
"product__category",
|
||||||
)
|
)
|
||||||
|
category_clauses = conditions.DiscountForCategory.objects.all()
|
||||||
all_category_discounts = (
|
category_clauses = category_clauses.select_related(
|
||||||
category_discounts | product_category_discounts
|
|
||||||
)
|
|
||||||
all_category_discounts = all_category_discounts.select_related(
|
|
||||||
"category",
|
"category",
|
||||||
)
|
)
|
||||||
|
|
||||||
valid_discounts = conditions.DiscountBase.objects.filter(
|
valid_discounts = conditions.DiscountBase.objects.all()
|
||||||
Q(discountforproduct__in=product_discounts) |
|
|
||||||
Q(discountforcategory__in=all_category_discounts)
|
|
||||||
)
|
|
||||||
|
|
||||||
all_subsets = []
|
all_subsets = []
|
||||||
|
|
||||||
|
@ -145,8 +140,8 @@ class DiscountController(object):
|
||||||
from_filter = dict((i.id, i) for i in filtered_discounts)
|
from_filter = dict((i.id, i) for i in filtered_discounts)
|
||||||
|
|
||||||
clause_sets = (
|
clause_sets = (
|
||||||
product_discounts.filter(discount__in=filtered_discounts),
|
product_clauses.filter(discount__in=filtered_discounts),
|
||||||
all_category_discounts.filter(discount__in=filtered_discounts),
|
category_clauses.filter(discount__in=filtered_discounts),
|
||||||
)
|
)
|
||||||
|
|
||||||
clause_sets = (
|
clause_sets = (
|
||||||
|
|
Loading…
Reference in a new issue