Skip to content

Commit

Permalink
Added support for custom lookup parameters. Issue #12.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazybird committed Mar 23, 2014
1 parent a32f4b4 commit 30c3926
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
15 changes: 14 additions & 1 deletion carton/cart.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from decimal import Decimal

from django.conf import settings

from carton import module_loading
from carton import settings as carton_settings

Expand Down Expand Up @@ -61,9 +63,20 @@ def __contains__(self, product):
def get_product_model(self):
return module_loading.get_product_model()

def filter_products(self, queryset):
"""
Applies lookup parameters defined in settings.
"""
lookup_parameters = getattr(settings, 'CART_PRODUCT_LOOKUP', None)
if lookup_parameters:
queryset = queryset.filter(**lookup_parameters)
return queryset

def get_queryset(self):
product_model = self.get_product_model()
return product_model._default_manager.all()
queryset = product_model._default_manager.all()
queryset = self.filter_products(queryset)
return queryset

def update_session(self):
"""
Expand Down
18 changes: 17 additions & 1 deletion carton/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from django.core.urlresolvers import reverse
from django.test import TestCase

from carton.tests.models import Product

try:
from django.test import override_settings
except ImportError:
from django.test.utils import override_settings


class CartTests(TestCase):

Expand Down Expand Up @@ -99,3 +103,15 @@ def test_cart_items_are_cleared(self):
response = self.client.get(self.url_show)
self.assertNotContains(response, 'deer')
self.assertNotContains(response, 'moose')

@override_settings(CART_PRODUCT_LOOKUP={'price__gt': 1})
def test_custom_product_filter_are_applied(self):
# We modify the queryset to exclude some products. For these excluded
# we should not be able to add them in the cart.
exclude = Product.objects.create(name='EXCLUDE', price=0.99, custom_id=100)
exclude_data = {'product_id': exclude.pk}
self.client.post(self.url_add, self.deer_data)
self.client.post(self.url_add, exclude_data)
response = self.client.get(self.url_show)
self.assertNotContains(response, 'EXCLUDE')
self.assertContains(response, 'deer')

0 comments on commit 30c3926

Please sign in to comment.