Commit 11483669 by Chris Dodge

change the context_processor in the shopping cart to return a pointer to a…

change the context_processor in the shopping cart to return a pointer to a function so that only the templates that actually need to know if the shopping cart button should be shown will actually make the database roundtrips
parent 0bce5569
......@@ -16,16 +16,23 @@ def user_has_cart_context_processor(request):
be displayed. Anonymous users don't.
Adds `display_shopping_cart` to the context
"""
display_shopping_cart = (
# user is logged in and
request.user.is_authenticated() and
# do we have the feature turned on
is_shopping_cart_enabled() and
# user's cart has PaidCourseRegistrations
Order.user_cart_has_items(
request.user,
[PaidCourseRegistration, CourseRegCodeItem]
def should_display_shopping_cart():
"""
Returns a boolean if the user has an items in a cart whereby the shopping cart should be
displayed to the logged in user
"""
return (
# user is logged in and
request.user.is_authenticated() and
# do we have the feature turned on
is_shopping_cart_enabled() and
# does the user actually have a cart (optimized query to prevent creation of a cart when not needed)
Order.does_user_have_cart(request.user) and
# user's cart has PaidCourseRegistrations or CourseRegCodeItem
Order.user_cart_has_items(
request.user,
[PaidCourseRegistration, CourseRegCodeItem]
)
)
)
return {'display_shopping_cart': display_shopping_cart}
return {'should_display_shopping_cart_func': should_display_shopping_cart}
......@@ -151,6 +151,13 @@ class Order(models.Model):
return cart_order
@classmethod
def does_user_have_cart(cls, user):
"""
Returns a boolean whether a shopping cart (Order) exists for the specified user
"""
return cls.objects.filter(user=user, status='cart').exists()
@classmethod
def user_cart_has_items(cls, user, item_types=None):
"""
Returns true if the user (anonymous user ok) has
......
......@@ -42,7 +42,7 @@ class UserCartContextProcessorUnitTest(ModuleStoreTestCase):
self.add_to_cart()
self.request.user = self.user
context = user_has_cart_context_processor(self.request)
self.assertFalse(context['display_shopping_cart'])
self.assertFalse(context['should_display_shopping_cart_func']())
@patch.dict(settings.FEATURES, {'ENABLE_SHOPPING_CART': True, 'ENABLE_PAID_COURSE_REGISTRATION': False})
def test_no_enable_paid_course_registration(self):
......@@ -52,7 +52,7 @@ class UserCartContextProcessorUnitTest(ModuleStoreTestCase):
self.add_to_cart()
self.request.user = self.user
context = user_has_cart_context_processor(self.request)
self.assertFalse(context['display_shopping_cart'])
self.assertFalse(context['should_display_shopping_cart_func']())
@patch.dict(settings.FEATURES, {'ENABLE_SHOPPING_CART': True, 'ENABLE_PAID_COURSE_REGISTRATION': True})
def test_anonymous_user(self):
......@@ -61,7 +61,7 @@ class UserCartContextProcessorUnitTest(ModuleStoreTestCase):
"""
self.request.user = AnonymousUser()
context = user_has_cart_context_processor(self.request)
self.assertFalse(context['display_shopping_cart'])
self.assertFalse(context['should_display_shopping_cart_func']())
@patch.dict(settings.FEATURES, {'ENABLE_SHOPPING_CART': True, 'ENABLE_PAID_COURSE_REGISTRATION': True})
def test_no_items_in_cart(self):
......@@ -70,7 +70,7 @@ class UserCartContextProcessorUnitTest(ModuleStoreTestCase):
"""
self.request.user = self.user
context = user_has_cart_context_processor(self.request)
self.assertFalse(context['display_shopping_cart'])
self.assertFalse(context['should_display_shopping_cart_func']())
@patch.dict(settings.FEATURES, {'ENABLE_SHOPPING_CART': True, 'ENABLE_PAID_COURSE_REGISTRATION': True})
def test_items_in_cart(self):
......@@ -80,4 +80,4 @@ class UserCartContextProcessorUnitTest(ModuleStoreTestCase):
self.add_to_cart()
self.request.user = self.user
context = user_has_cart_context_processor(self.request)
self.assertTrue(context['display_shopping_cart'])
self.assertTrue(context['should_display_shopping_cart_func']())
......@@ -85,7 +85,7 @@ site_status_msg = get_site_status_msg(course_id)
</li>
</ol>
% if display_shopping_cart: # see shoppingcart.context_processor.user_has_cart_context_processor
% if should_display_shopping_cart_func(): # see shoppingcart.context_processor.user_has_cart_context_processor
<ol class="user">
<li class="primary">
<a class="shopping-cart" href="${reverse('shoppingcart.views.show_cart')}">
......
......@@ -79,7 +79,7 @@ site_status_msg = get_site_status_msg(course_id)
</ul>
</li>
</ol>
% if display_shopping_cart: # see shoppingcart.context_processor.user_has_cart_context_processor
% if should_display_shopping_cart_func(): # see shoppingcart.context_processor.user_has_cart_context_processor
<ol class="user">
<li class="primary">
<a class="shopping-cart" href="${reverse('shoppingcart.views.show_cart')}">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment