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

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

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

from django.contrib.auth.models import AnonymousUser 

from shop.models import AddressModel 

 

 

#============================================================================== 

# Addresses handling 

#============================================================================== 

 

def get_shipping_address_from_request(request): 

    """ 

    Get the shipping address from the request. This abstracts the fact that 

    users can be either registered (and thus, logged in), or only session-based 

    guests 

    """ 

    shipping_address = None 

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

        # There is a logged-in user here, but he might not have an address 

        # defined. 

        try: 

            shipping_address = AddressModel.objects.get( 

                user_shipping=request.user) 

        except AddressModel.DoesNotExist: 

            shipping_address = None 

    else: 

        # The client is a guest - let's use the session instead. 

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

        shipping_address = None 

        session_address_id = session.get('shipping_address_id') 

32        if session is not None and session_address_id: 

            shipping_address = AddressModel.objects.get(pk=session_address_id) 

    return shipping_address 

 

 

def get_billing_address_from_request(request): 

    """ 

    Get the billing address from the request. This abstracts the fact that 

    users can be either registered (and thus, logged in), or only session-based 

    guests 

    """ 

    billing_address = None 

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

        # There is a logged-in user here, but he might not have an address 

        # defined. 

        try: 

            billing_address = AddressModel.objects.get( 

                user_billing=request.user) 

        except AddressModel.DoesNotExist: 

            billing_address = None 

    else: 

        # The client is a guest - let's use the session instead. 

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

        session_billing_id = session.get('billing_address_id') 

56        if session is not None and session_billing_id: 

            billing_address = AddressModel.objects.get(pk=session_billing_id) 

    return billing_address 

 

 

def assign_address_to_request(request, address, shipping=True): 

    """ 

    Sets the passed address as either the shipping or the billing address for 

    the passed request.  This abstracts the difference between logged-in users 

    and session-based guests. 

 

    The `shipping` parameter controls whether the address is a shipping address 

    (default) or a billing address. 

    """ 

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

        # There is a logged-in user here. 

        if shipping: 

            address.user_shipping = request.user 

            address.save() 

        else: 

            address.user_billing = request.user 

            address.save() 

    else: 

        # The client is a guest - let's use the session instead.  There has to 

        # be a session. Otherwise it's fine to get an AttributeError 

        if shipping: 

            request.session['shipping_address_id'] = address.pk 

        else: 

            request.session['billing_address_id'] = address.pk 

 

 

def get_user_name_from_request(request): 

    """ 

    Simple helper to return the username from the request, or '' if the user is 

    AnonymousUser. 

    """ 

    name = '' 

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

        name = request.user.get_full_name()  # TODO: Administrators! 

    return name