Coverage for /home/tribaal/workspace/django-shop/shop/util/order: 100.00%

# Coverage for /home/tribaal/workspace/django-shop/shop/util/order : 100.00%

## 25 statements 25 run 0 missing 0 excluded 0 partial

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #-*- coding: utf-8 -*- from django.contrib.auth.models import AnonymousUser from shop.models.ordermodel import Order def get_orders_from_request(request): """ Returns all the Orders created from the provided request. """ orders = None if request.user and not isinstance(request.user, AnonymousUser): # There is a logged in user orders = Order.objects.filter(user=request.user) orders = orders.order_by('-created') else: session = getattr(request, 'session', None) if session is not None: # There is a session order_id = session.get('order_id') if order_id: orders = Order.objects.filter(pk=order_id) return orders def get_order_from_request(request): """ Returns the currently processing Order from a request (switches between user or session mode) if any. """ orders = get_orders_from_request(request) if orders and len(orders) >= 1: order = orders[0] else: order = None 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.pk