From f7289c21019b87efa069beffba201701f9179c1f Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Sat, 2 Apr 2016 11:56:03 +1100 Subject: [PATCH] =?UTF-8?q?Adds=20=E2=80=98available=5Fcategories=E2=80=99?= =?UTF-8?q?=20as=20something=20that=20actually=20works?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- registrasion/controllers/category.py | 26 +++++++++++++++++++ .../templatetags/registrasion_tags.py | 3 ++- registrasion/tests/test_enabling_condition.py | 22 ++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 registrasion/controllers/category.py diff --git a/registrasion/controllers/category.py b/registrasion/controllers/category.py new file mode 100644 index 00000000..53fe2872 --- /dev/null +++ b/registrasion/controllers/category.py @@ -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) diff --git a/registrasion/templatetags/registrasion_tags.py b/registrasion/templatetags/registrasion_tags.py index ac9fa0ca..6de13e74 100644 --- a/registrasion/templatetags/registrasion_tags.py +++ b/registrasion/templatetags/registrasion_tags.py @@ -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) diff --git a/registrasion/tests/test_enabling_condition.py b/registrasion/tests/test_enabling_condition.py index 2861e8db..aeec4708 100644 --- a/registrasion/tests/test_enabling_condition.py +++ b/registrasion/tests/test_enabling_condition.py @@ -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)