available_products now refers to the user’s product limits as well as enabling conditions
This commit is contained in:
parent
f7289c2101
commit
8f233c7943
3 changed files with 38 additions and 4 deletions
|
@ -21,6 +21,4 @@ class CategoryController(object):
|
||||||
products=products,
|
products=products,
|
||||||
)
|
)
|
||||||
|
|
||||||
print available
|
|
||||||
|
|
||||||
return set(i.category for i in available)
|
return set(i.category for i in available)
|
||||||
|
|
|
@ -32,16 +32,23 @@ class ProductController(object):
|
||||||
out = [
|
out = [
|
||||||
product
|
product
|
||||||
for product in all_products
|
for product in all_products
|
||||||
|
if cls(product).user_can_add_within_limit(user, 1, past_carts=True)
|
||||||
if cls(product).can_add_with_enabling_conditions(user, 0)
|
if cls(product).can_add_with_enabling_conditions(user, 0)
|
||||||
]
|
]
|
||||||
out.sort(key=lambda product: product.order)
|
out.sort(key=lambda product: product.order)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def user_can_add_within_limit(self, user, quantity):
|
def user_can_add_within_limit(self, user, quantity, past_carts=False):
|
||||||
''' Return true if the user is able to add _quantity_ to their count of
|
''' Return true if the user is able to add _quantity_ to their count of
|
||||||
this Product without exceeding _limit_per_user_.'''
|
this Product without exceeding _limit_per_user_.'''
|
||||||
|
|
||||||
carts = rego.Cart.objects.filter(user=user)
|
carts = rego.Cart.objects.filter(
|
||||||
|
user=user,
|
||||||
|
released=False,
|
||||||
|
)
|
||||||
|
if past_carts:
|
||||||
|
carts = carts.filter(active=False)
|
||||||
|
|
||||||
items = rego.ProductItem.objects.filter(
|
items = rego.ProductItem.objects.filter(
|
||||||
cart__in=carts,
|
cart__in=carts,
|
||||||
)
|
)
|
||||||
|
|
|
@ -9,6 +9,7 @@ from django.test import TestCase
|
||||||
|
|
||||||
from registrasion import models as rego
|
from registrasion import models as rego
|
||||||
from registrasion.controllers.cart import CartController
|
from registrasion.controllers.cart import CartController
|
||||||
|
from registrasion.controllers.product import ProductController
|
||||||
|
|
||||||
from patch_datetime import SetTimeMixin
|
from patch_datetime import SetTimeMixin
|
||||||
|
|
||||||
|
@ -288,3 +289,31 @@ class BasicCartTests(RegistrationCartTestCase):
|
||||||
|
|
||||||
with self.assertRaises(ValidationError):
|
with self.assertRaises(ValidationError):
|
||||||
current_cart.set_quantity(self.PROD_4, 1)
|
current_cart.set_quantity(self.PROD_4, 1)
|
||||||
|
|
||||||
|
def __available_products_test(self, item, quantity):
|
||||||
|
self.set_limits()
|
||||||
|
|
||||||
|
get_prods = lambda: ProductController.available_products(
|
||||||
|
self.USER_1,
|
||||||
|
products=[self.PROD_2, self.PROD_3, self.PROD_4],
|
||||||
|
)
|
||||||
|
|
||||||
|
current_cart = CartController.for_user(self.USER_1)
|
||||||
|
prods = get_prods()
|
||||||
|
self.assertTrue(item in prods)
|
||||||
|
current_cart.add_to_cart(item, quantity)
|
||||||
|
self.assertTrue(item in prods)
|
||||||
|
|
||||||
|
current_cart.cart.active = False
|
||||||
|
current_cart.cart.save()
|
||||||
|
|
||||||
|
current_cart = CartController.for_user(self.USER_1)
|
||||||
|
|
||||||
|
prods = get_prods()
|
||||||
|
self.assertTrue(item not in prods)
|
||||||
|
|
||||||
|
def test_available_products_respects_category_limits(self):
|
||||||
|
self.__available_products_test(self.PROD_3, 10)
|
||||||
|
|
||||||
|
def test_available_products_respects_product_limits(self):
|
||||||
|
self.__available_products_test(self.PROD_4, 6)
|
||||||
|
|
Loading…
Reference in a new issue