Skip to content
Browse files

Extract BasketMixin#get_object to basket.utils for use elsewhere in t…

…he app
  • Loading branch information...
1 parent 07580ed commit cf7a190d7c72b077cfedc7282540021857a83b06 @martinogden committed Apr 3, 2011
Showing with 14 additions and 13 deletions.
  1. +12 −1 apps/basket/utils.py
  2. +2 −12 apps/basket/views.py
View
13 apps/basket/utils.py
@@ -1,4 +1,15 @@
from django.contrib.contenttypes.models import ContentType
# Shortcut to get an items content type
-content_type = lambda product: ContentType.objects.get_for_model(product)
+content_type = lambda product: ContentType.objects.get_for_model(product)
+
+
+def get_object(ct_pk, pk):
+ "Return an instance of the item defined by it's content type and the instance pk"
+ try:
+ model = ContentType.objects.get(pk=ct_pk)
+ item = model.get_object_for_this_type(pk=pk)
+ except ObjectDoesNotExist:
+ return None
+ else:
+ return item
View
14 apps/basket/views.py
@@ -1,4 +1,3 @@
-from django.core.exceptions import ObjectDoesNotExist
from django.utils.datastructures import MultiValueDictKeyError
from django.views.generic.base import RedirectView
from django.http import HttpResponseRedirect
@@ -7,6 +6,7 @@
from django.utils.translation import ugettext as _
from basket.models import Basket, InvalidItem
+from basket.utils import get_object
SUCCESS_MESSAGE = '%i %ss successfully added to your basket'
@@ -26,16 +26,6 @@ def action(self, request, *args):
i.e. add_item or remove_item"""
raise NotImplementedError
- def get_object(self, ct_pk, pk):
- "Return an instance of the item defined by it's content type and the instance pk"
- try:
- model = ContentType.objects.get(pk=ct_pk)
- item = model.get_object_for_this_type(pk=pk)
- except ObjectDoesNotExist:
- return None
- else:
- return item
-
def set_message(self, request):
"Dynamically set level and content of message"
try:
@@ -57,7 +47,7 @@ def get(self, request, *args, **kwargs):
self.basket = request.session.get('basket')
if self.verify_get_params(request):
- item = self.get_object(request.GET['ct'], request.GET['pk'])
+ item = get_object(request.GET['ct'], request.GET['pk'])
if item:
self.action(request, item)

0 comments on commit cf7a190

Please sign in to comment.
Something went wrong with that request. Please try again.