forked from awesto/django-shop
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Moved order to request and order from request to a helper, this neede…
…d to change tests slightly
- Loading branch information
Chris Glass
committed
Feb 15, 2011
1 parent
b7b676b
commit 170e18f
Showing
4 changed files
with
49 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#-*- coding: utf-8 -*- | ||
from django.contrib.auth.models import AnonymousUser | ||
from shop.models.ordermodel import Order | ||
|
||
def get_order_from_request(request): | ||
''' | ||
Returns the currently processing Order from a request (switches between | ||
user or session mode) if any. | ||
''' | ||
order = None | ||
if request.user and not isinstance(request.user, AnonymousUser): | ||
# There is a logged in user | ||
order = Order.objects.filter(user=request.user).filter(status__lt=Order.COMPLETED) | ||
if len(order) >= 1: # The queryset returns a list | ||
order = order[0] | ||
else: | ||
order = None # There is a logged in user but he has no pending order | ||
else: | ||
session = getattr(request, 'session', None) | ||
if session != None : | ||
# There is a session | ||
order_id = session.get('order_id') | ||
if order_id: | ||
order = Order.objects.get(pk=order_id) | ||
return order | ||
|
||
|
||
def add_order_to_request(request,order): | ||
''' | ||
Checks that the order is linked to the current user or adds the order to | ||
the session should there be no logged in user. | ||
''' | ||
if request.user and not isinstance(request.user, AnonymousUser): | ||
# We should check that the current user is indeed the request's user. | ||
if order.user != request.user: | ||
order.user = request.user | ||
order.save() | ||
else: | ||
# Add the order_id to the session | ||
# There has to be a session. Otherwise it's fine to get an AttributeError | ||
request.session['order_id'] = order.id |