From 4928fad4151b943da3bc63ae62ca10c79b5aa8f2 Mon Sep 17 00:00:00 2001 From: Chris Dodge Date: Thu, 17 Jul 2014 00:43:00 -0400 Subject: [PATCH] add ability to inspect a cart for the existence of a particular type of item, e.g. PaidCourseRegistration. Only show the 'Shopping Cart' button if there's a PaidCourseRegistration in it. --- .../shoppingcart/context_processor.py | 5 ++++- lms/djangoapps/shoppingcart/models.py | 18 ++++++++++++++---- .../shoppingcart/tests/test_models.py | 15 +++++++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/lms/djangoapps/shoppingcart/context_processor.py b/lms/djangoapps/shoppingcart/context_processor.py index 95beb106d3..540ab6a2f3 100644 --- a/lms/djangoapps/shoppingcart/context_processor.py +++ b/lms/djangoapps/shoppingcart/context_processor.py @@ -19,5 +19,8 @@ def user_has_cart_context_processor(request): request.user.is_authenticated() and # user is logged in and settings.FEATURES.get('ENABLE_PAID_COURSE_REGISTRATION') and # settings enable paid course reg and settings.FEATURES.get('ENABLE_SHOPPING_CART') and # settings enable shopping cart and - shoppingcart.models.Order.user_cart_has_items(request.user) # user's cart has items + shoppingcart.models.Order.user_cart_has_items( + request.user, + shoppingcart.models.PaidCourseRegistration + ) # user's cart has PaidCourseRegistrations )} diff --git a/lms/djangoapps/shoppingcart/models.py b/lms/djangoapps/shoppingcart/models.py index 406257fd01..6839300575 100644 --- a/lms/djangoapps/shoppingcart/models.py +++ b/lms/djangoapps/shoppingcart/models.py @@ -87,15 +87,17 @@ class Order(models.Model): return cart_order @classmethod - def user_cart_has_items(cls, user): + def user_cart_has_items(cls, user, item_type=None): """ Returns true if the user (anonymous user ok) has a cart with items in it. (Which means it should be displayed. + If a item_type is passed in, then we check to see if the cart has at least one of + those types of OrderItems """ if not user.is_authenticated(): return False cart = cls.get_cart_for_user(user) - return cart.has_items() + return cart.has_items(item_type) @property def total_cost(self): @@ -105,11 +107,19 @@ class Order(models.Model): """ return sum(i.line_cost for i in self.orderitem_set.filter(status=self.status)) # pylint: disable=E1101 - def has_items(self): + def has_items(self, item_type=None): """ Does the cart have any items in it? + If an item_type is passed in then we check to see if there are any items of that class type """ - return self.orderitem_set.exists() # pylint: disable=E1101 + if not item_type: + return self.orderitem_set.exists() # pylint: disable=E1101 + else: + items = self.orderitem_set.all().select_subclasses() + for item in items: + if isinstance(item, item_type): + return True + return False def clear(self): """ diff --git a/lms/djangoapps/shoppingcart/tests/test_models.py b/lms/djangoapps/shoppingcart/tests/test_models.py index 5f4a911071..cdaeda1826 100644 --- a/lms/djangoapps/shoppingcart/tests/test_models.py +++ b/lms/djangoapps/shoppingcart/tests/test_models.py @@ -52,6 +52,21 @@ class OrderTest(ModuleStoreTestCase): item = OrderItem(order=cart, user=self.user) item.save() self.assertTrue(Order.user_cart_has_items(self.user)) + self.assertFalse(Order.user_cart_has_items(self.user, CertificateItem)) + self.assertFalse(Order.user_cart_has_items(self.user, PaidCourseRegistration)) + + def test_user_cart_has_paid_course_registration_items(self): + cart = Order.get_cart_for_user(self.user) + item = PaidCourseRegistration(order=cart, user=self.user) + item.save() + self.assertTrue(Order.user_cart_has_items(self.user, PaidCourseRegistration)) + self.assertFalse(Order.user_cart_has_items(self.user, CertificateItem)) + + def test_user_cart_has_certificate_items(self): + cart = Order.get_cart_for_user(self.user) + CertificateItem.add_to_order(cart, self.course_key, self.cost, 'honor') + self.assertTrue(Order.user_cart_has_items(self.user, CertificateItem)) + self.assertFalse(Order.user_cart_has_items(self.user, PaidCourseRegistration)) def test_cart_clear(self): cart = Order.get_cart_for_user(user=self.user)