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

# -*- coding: utf-8 -*- 

from shop.models.cartmodel import Cart 

from django.contrib.auth.models import AnonymousUser 

 

def get_or_create_cart(request): 

    """ 

    Let's inspect the request for session or for user, then either find a 

    matching cart and return it or create a new one bound to the user (if one 

    exists), or to the session. 

    """ 

    cart = None 

    if not hasattr(request, '_cart'): 

        if request.user and not isinstance(request.user, AnonymousUser): 

            # There is a logged in user 

            cart = Cart.objects.filter(user=request.user) # a list 

            if not cart: # if list is empty 

                cart = Cart.objects.create(user=request.user) 

            else: 

                cart = cart[0] # Get the first one from the list 

        else: 

            session = getattr(request, 'session', None) 

            if session != None : 

                # There is a session 

                cart_id = session.get('cart_id') 

                if cart_id: 

                    try: 

                        cart = Cart.objects.get(pk=cart_id) 

                    except Cart.DoesNotExist: 

                        cart = None 

                if not cart: 

                    cart = Cart.objects.create() 

                    session['cart_id'] = cart.id 

        setattr(request, '_cart', cart) 

    cart = getattr(request, '_cart') # There we *must* have a cart 

    return cart