Replaces ProductController.attach_user_remainders with ProductController.user_remainders

This commit is contained in:
Christopher Neugebauer 2016-04-30 20:30:21 +10:00
parent 6956c78b0d
commit 941caa30d9
2 changed files with 9 additions and 22 deletions

View file

@ -217,16 +217,14 @@ class CartController(object):
errors = [] errors = []
# Pre-annotate products # Pre-annotate products
products = [p for (p, q) in product_quantities] remainders = ProductController.user_remainders(self.cart.user)
r = ProductController.attach_user_remainders(self.cart.user, products)
with_remainders = dict((p, p) for p in r)
# Test each product limit here # Test each product limit here
for product, quantity in product_quantities: for product, quantity in product_quantities:
if quantity < 0: if quantity < 0:
errors.append((product, "Value must be zero or greater.")) errors.append((product, "Value must be zero or greater."))
limit = with_remainders[product].remainder limit = remainders[product.id]
if quantity > limit: if quantity > limit:
errors.append(( errors.append((

View file

@ -38,14 +38,13 @@ class ProductController(object):
r = CategoryController.attach_user_remainders(user, categories) r = CategoryController.attach_user_remainders(user, categories)
cat_quants = dict((c, c) for c in r) cat_quants = dict((c, c) for c in r)
r = ProductController.attach_user_remainders(user, all_products) product_remainders = ProductController.user_remainders(user)
prod_quants = dict((p, p) for p in r)
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 cat_quants[product.category].remainder > 0
if prod_quants[product].remainder > 0 if product_remainders[product.id] > 0
) )
failed_and_messages = FlagController.test_flags( failed_and_messages = FlagController.test_flags(
@ -59,17 +58,15 @@ class ProductController(object):
return out return out
@classmethod @classmethod
def attach_user_remainders(cls, user, products): def user_remainders(cls, user):
''' '''
Return: Return:
queryset(inventory.Product): A queryset containing items from Mapping[int->int]: A dictionary that maps the product ID to the
``product``, with an extra attribute -- remainder = the amount of user's remainder for that product.
this item that is remaining.
''' '''
ids = [product.id for product in products] products = inventory.Product.objects.all()
products = inventory.Product.objects.filter(id__in=ids)
cart_filter = ( cart_filter = (
Q(productitem__cart__user=user) & Q(productitem__cart__user=user) &
@ -93,12 +90,4 @@ class ProductController(object):
products = products.annotate(remainder=remainder) products = products.annotate(remainder=remainder)
return products return dict((product.id, product.remainder) for product in products)
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.product])
return with_remainders[0].remainder