Replaces CategoryController.attach_user_remainders with user_remainders
This commit is contained in:
parent
941caa30d9
commit
c6fdfa496e
3 changed files with 10 additions and 23 deletions
|
@ -240,12 +240,11 @@ class CartController(object):
|
||||||
by_cat[product.category].append((product, quantity))
|
by_cat[product.category].append((product, quantity))
|
||||||
|
|
||||||
# Pre-annotate categories
|
# Pre-annotate categories
|
||||||
r = CategoryController.attach_user_remainders(self.cart.user, by_cat)
|
remainders = CategoryController.user_remainders(self.cart.user)
|
||||||
with_remainders = dict((cat, cat) for cat in r)
|
|
||||||
|
|
||||||
# Test each category limit here
|
# Test each category limit here
|
||||||
for category in by_cat:
|
for category in by_cat:
|
||||||
limit = with_remainders[category].remainder
|
limit = remainders[category.id]
|
||||||
|
|
||||||
# Get the amount so far in the cart
|
# Get the amount so far in the cart
|
||||||
to_add = sum(i[1] for i in by_cat[category])
|
to_add = sum(i[1] for i in by_cat[category])
|
||||||
|
|
|
@ -39,17 +39,16 @@ class CategoryController(object):
|
||||||
return set(i.category for i in available)
|
return set(i.category for i in available)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def attach_user_remainders(cls, user, categories):
|
def user_remainders(cls, user):
|
||||||
'''
|
'''
|
||||||
|
|
||||||
Return:
|
Return:
|
||||||
queryset(inventory.Product): A queryset containing items from
|
Mapping[int->int]: A dictionary that maps the category ID to the
|
||||||
``categories``, with an extra attribute -- remainder = the amount
|
user's remainder for that category.
|
||||||
of items from this category that is remaining.
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
ids = [category.id for category in categories]
|
categories = inventory.Category.objects.all()
|
||||||
categories = inventory.Category.objects.filter(id__in=ids)
|
|
||||||
|
|
||||||
cart_filter = (
|
cart_filter = (
|
||||||
Q(product__productitem__cart__user=user) &
|
Q(product__productitem__cart__user=user) &
|
||||||
|
@ -73,12 +72,4 @@ class CategoryController(object):
|
||||||
|
|
||||||
categories = categories.annotate(remainder=remainder)
|
categories = categories.annotate(remainder=remainder)
|
||||||
|
|
||||||
return categories
|
return dict((cat.id, cat.remainder) for cat in categories)
|
||||||
|
|
||||||
def user_quantity_remaining(self, user):
|
|
||||||
''' Returns the quantity of this product that the user add in the
|
|
||||||
current cart. '''
|
|
||||||
|
|
||||||
with_remainders = self.attach_user_remainders(user, [self.category])
|
|
||||||
|
|
||||||
return with_remainders[0].remainder
|
|
||||||
|
|
|
@ -34,16 +34,13 @@ class ProductController(object):
|
||||||
if products is not None:
|
if products is not None:
|
||||||
all_products = set(itertools.chain(all_products, products))
|
all_products = set(itertools.chain(all_products, products))
|
||||||
|
|
||||||
categories = set(product.category for product in all_products)
|
category_remainders = CategoryController.user_remainders(user)
|
||||||
r = CategoryController.attach_user_remainders(user, categories)
|
|
||||||
cat_quants = dict((c, c) for c in r)
|
|
||||||
|
|
||||||
product_remainders = ProductController.user_remainders(user)
|
product_remainders = ProductController.user_remainders(user)
|
||||||
|
|
||||||
passed_limits = set(
|
passed_limits = set(
|
||||||
product
|
product
|
||||||
for product in all_products
|
for product in all_products
|
||||||
if cat_quants[product.category].remainder > 0
|
if category_remainders[product.category.id] > 0
|
||||||
if product_remainders[product.id] > 0
|
if product_remainders[product.id] > 0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue