Adds ‘available_categories’ as something that actually works

This commit is contained in:
Christopher Neugebauer 2016-04-02 11:56:03 +11:00
parent bdd3714f47
commit f7289c2101
3 changed files with 50 additions and 1 deletions

View file

@ -0,0 +1,26 @@
from .product import ProductController
from registrasion import models as rego
class AllProducts(object):
pass
class CategoryController(object):
@classmethod
def available_categories(cls, user, products=AllProducts):
''' Returns the categories available to the user. Specify `products` if
you want to restrict to just the categories that hold the specified
products, otherwise it'll do all. '''
if products is AllProducts:
products = rego.Product.objects.all()
available = ProductController.available_products(
user,
products=products,
)
print available
return set(i.category for i in available)

View file

@ -1,4 +1,5 @@
from registrasion import models as rego
from registrasion.controllers.category import CategoryController
from collections import namedtuple
from django import template
@ -12,7 +13,7 @@ ProductAndQuantity = namedtuple("ProductAndQuantity", ["product", "quantity"])
@register.assignment_tag(takes_context=True)
def available_categories(context):
''' Returns all of the available product categories '''
return rego.Category.objects.all()
return CategoryController.available_categories(context.request.user)
@register.assignment_tag(takes_context=True)

View file

@ -3,6 +3,7 @@ import pytz
from django.core.exceptions import ValidationError
from registrasion import models as rego
from registrasion.controllers.category import CategoryController
from registrasion.controllers.cart import CartController
from registrasion.controllers.product import ProductController
@ -271,3 +272,24 @@ class EnablingConditionTestCases(RegistrationCartTestCase):
with self.assertRaises(ValidationError):
cart_2.set_quantity(self.PROD_1, 1)
def test_available_categories(self):
self.add_product_enabling_condition_on_category(mandatory=False)
cart_1 = CartController.for_user(self.USER_1)
cats = CategoryController.available_categories(
self.USER_1,
)
self.assertFalse(self.CAT_1 in cats)
self.assertTrue(self.CAT_2 in cats)
cart_1.add_to_cart(self.PROD_3, 1)
cats = CategoryController.available_categories(
self.USER_1,
)
self.assertTrue(self.CAT_1 in cats)
self.assertTrue(self.CAT_2 in cats)