Adds available_products as a method on ProductController

This commit is contained in:
Christopher Neugebauer 2016-03-26 13:22:47 +11:00
parent c41a9cadff
commit 45aa83f854
2 changed files with 103 additions and 0 deletions

View file

@ -1,3 +1,5 @@
import itertools
from django.db.models import Q from django.db.models import Q
from registrasion import models as rego from registrasion import models as rego
@ -9,6 +11,29 @@ class ProductController(object):
def __init__(self, product): def __init__(self, product):
self.product = product self.product = product
@classmethod
def available_products(cls, user, category=None, products=None):
''' Returns a list of all of the products that are available per
enabling conditions from the given categories.
TODO: refactor so that all conditions are tested here and
can_add_with_enabling_conditions calls this method. '''
if category is None and products is None:
raise ValueError("You must provide products or a category")
if category is not None:
all_products = rego.Product.objects.filter(category=category)
else:
all_products = []
if products is not None:
all_products = itertools.chain(all_products, products)
return [
product
for product in all_products
if cls(product).can_add_with_enabling_conditions(user, 0)
]
def user_can_add_within_limit(self, user, quantity): def user_can_add_within_limit(self, user, quantity):
''' 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_.'''

View file

@ -4,6 +4,7 @@ from django.core.exceptions import ValidationError
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 test_cart import RegistrationCartTestCase from test_cart import RegistrationCartTestCase
@ -155,3 +156,80 @@ class EnablingConditionTestCases(RegistrationCartTestCase):
cart_1.add_to_cart(self.PROD_1, 1) cart_1.add_to_cart(self.PROD_1, 1)
cart_1.add_to_cart(self.PROD_3, 1) # Meets the category condition cart_1.add_to_cart(self.PROD_3, 1) # Meets the category condition
cart_1.add_to_cart(self.PROD_1, 1) cart_1.add_to_cart(self.PROD_1, 1)
def test_available_products_works_with_no_conditions_set(self):
prods = ProductController.available_products(
self.USER_1,
category=self.CAT_1,
)
self.assertTrue(self.PROD_1 in prods)
self.assertTrue(self.PROD_2 in prods)
prods = ProductController.available_products(
self.USER_1,
category=self.CAT_2,
)
self.assertTrue(self.PROD_3 in prods)
self.assertTrue(self.PROD_4 in prods)
prods = ProductController.available_products(
self.USER_1,
products=[self.PROD_1, self.PROD_2, self.PROD_3, self.PROD_4],
)
self.assertTrue(self.PROD_1 in prods)
self.assertTrue(self.PROD_2 in prods)
self.assertTrue(self.PROD_3 in prods)
self.assertTrue(self.PROD_4 in prods)
def test_available_products_on_category_works_when_condition_not_met(self):
self.add_product_enabling_condition(mandatory=False)
prods = ProductController.available_products(
self.USER_1,
category=self.CAT_1,
)
self.assertTrue(self.PROD_1 not in prods)
self.assertTrue(self.PROD_2 in prods)
def test_available_products_on_category_works_when_condition_is_met(self):
self.add_product_enabling_condition(mandatory=False)
cart_1 = CartController.for_user(self.USER_1)
cart_1.add_to_cart(self.PROD_2, 1)
prods = ProductController.available_products(
self.USER_1,
category=self.CAT_1,
)
self.assertTrue(self.PROD_1 in prods)
self.assertTrue(self.PROD_2 in prods)
def test_available_products_on_products_works_when_condition_not_met(self):
self.add_product_enabling_condition(mandatory=False)
prods = ProductController.available_products(
self.USER_1,
products=[self.PROD_1, self.PROD_2],
)
self.assertTrue(self.PROD_1 not in prods)
self.assertTrue(self.PROD_2 in prods)
def test_available_products_on_products_works_when_condition_is_met(self):
self.add_product_enabling_condition(mandatory=False)
cart_1 = CartController.for_user(self.USER_1)
cart_1.add_to_cart(self.PROD_2, 1)
prods = ProductController.available_products(
self.USER_1,
products=[self.PROD_1, self.PROD_2],
)
self.assertTrue(self.PROD_1 in prods)
self.assertTrue(self.PROD_2 in prods)