|
|
@@ -6,108 +6,23 @@ from django.contrib.sites.models import Site
|
|
6
|
6
|
from django.core.exceptions import ObjectDoesNotExist
|
|
7
|
7
|
from django.db.models import get_model
|
|
8
|
8
|
|
|
9
|
|
-from oscar.apps.shipping.methods import Free
|
|
10
|
|
-from oscar.core.loading import get_class, get_classes
|
|
11
|
|
-ShippingAddressForm, GatewayForm = get_classes('checkout.forms', ['ShippingAddressForm', 'GatewayForm'])
|
|
12
|
|
-OrderTotalCalculator = get_class('checkout.calculators', 'OrderTotalCalculator')
|
|
13
|
|
-CheckoutSessionData = get_class('checkout.utils', 'CheckoutSessionData')
|
|
14
|
|
-pre_payment, post_payment = get_classes('checkout.signals', ['pre_payment', 'post_payment'])
|
|
15
|
|
-OrderNumberGenerator, OrderCreator = get_classes('order.utils', ['OrderNumberGenerator', 'OrderCreator'])
|
|
16
|
|
-UserAddressForm = get_class('address.forms', 'UserAddressForm')
|
|
17
|
|
-Repository = get_class('shipping.repository', 'Repository')
|
|
18
|
|
-AccountAuthView = get_class('customer.views', 'AccountAuthView')
|
|
|
9
|
+from oscar.core.loading import get_class
|
|
|
10
|
+OrderCreator = get_class('order.utils', 'OrderCreator')
|
|
19
|
11
|
Dispatcher = get_class('customer.utils', 'Dispatcher')
|
|
20
|
|
-RedirectRequired, UnableToTakePayment, PaymentError = get_classes(
|
|
21
|
|
- 'payment.exceptions', ['RedirectRequired', 'UnableToTakePayment', 'PaymentError'])
|
|
22
|
|
-UnableToPlaceOrder = get_class('order.exceptions', 'UnableToPlaceOrder')
|
|
|
12
|
+CheckoutSessionMixin = get_class('checkout.session', 'CheckoutSessionMixin')
|
|
23
|
13
|
|
|
24
|
|
-Order = get_model('order', 'Order')
|
|
25
|
14
|
ShippingAddress = get_model('order', 'ShippingAddress')
|
|
26
|
15
|
CommunicationEvent = get_model('order', 'CommunicationEvent')
|
|
27
|
16
|
PaymentEventType = get_model('order', 'PaymentEventType')
|
|
28
|
17
|
PaymentEvent = get_model('order', 'PaymentEvent')
|
|
29
|
18
|
UserAddress = get_model('address', 'UserAddress')
|
|
30
|
19
|
Basket = get_model('basket', 'Basket')
|
|
31
|
|
-Email = get_model('customer', 'Email')
|
|
32
|
20
|
CommunicationEventType = get_model('customer', 'CommunicationEventType')
|
|
33
|
21
|
|
|
34
|
22
|
# Standard logger for checkout events
|
|
35
|
23
|
logger = logging.getLogger('oscar.checkout')
|
|
36
|
24
|
|
|
37
|
25
|
|
|
38
|
|
-class CheckoutSessionMixin(object):
|
|
39
|
|
- """
|
|
40
|
|
- Mixin to provide common functionality shared between checkout views.
|
|
41
|
|
- """
|
|
42
|
|
-
|
|
43
|
|
- def dispatch(self, request, *args, **kwargs):
|
|
44
|
|
- self.checkout_session = CheckoutSessionData(request)
|
|
45
|
|
- return super(CheckoutSessionMixin, self).dispatch(request, *args, **kwargs)
|
|
46
|
|
-
|
|
47
|
|
- def get_shipping_address(self):
|
|
48
|
|
- """
|
|
49
|
|
- Return the current shipping address for this checkout session.
|
|
50
|
|
-
|
|
51
|
|
- This could either be a ShippingAddress model which has been
|
|
52
|
|
- pre-populated (not saved), or a UserAddress model which will
|
|
53
|
|
- need converting into a ShippingAddress model at submission
|
|
54
|
|
- """
|
|
55
|
|
- addr_data = self.checkout_session.new_shipping_address_fields()
|
|
56
|
|
- if addr_data:
|
|
57
|
|
- # Load address data into a blank address model
|
|
58
|
|
- return ShippingAddress(**addr_data)
|
|
59
|
|
- addr_id = self.checkout_session.user_address_id()
|
|
60
|
|
- if addr_id:
|
|
61
|
|
- try:
|
|
62
|
|
- return UserAddress._default_manager.get(pk=addr_id)
|
|
63
|
|
- except UserAddress.DoesNotExist:
|
|
64
|
|
- # This can happen if you reset all your tables and you still have
|
|
65
|
|
- # session data that refers to addresses that no longer exist
|
|
66
|
|
- pass
|
|
67
|
|
- return None
|
|
68
|
|
-
|
|
69
|
|
- def get_shipping_method(self, basket=None):
|
|
70
|
|
- method = self.checkout_session.shipping_method()
|
|
71
|
|
- if method:
|
|
72
|
|
- if not basket:
|
|
73
|
|
- basket = self.request.basket
|
|
74
|
|
- method.set_basket(basket)
|
|
75
|
|
- else:
|
|
76
|
|
- # We default to using free shipping
|
|
77
|
|
- method = Free()
|
|
78
|
|
- return method
|
|
79
|
|
-
|
|
80
|
|
- def get_order_totals(self, basket=None, shipping_method=None, **kwargs):
|
|
81
|
|
- """
|
|
82
|
|
- Returns the total for the order with and without tax (as a tuple)
|
|
83
|
|
- """
|
|
84
|
|
- calc = OrderTotalCalculator(self.request)
|
|
85
|
|
- if not basket:
|
|
86
|
|
- basket = self.request.basket
|
|
87
|
|
- if not shipping_method:
|
|
88
|
|
- shipping_method = self.get_shipping_method(basket)
|
|
89
|
|
- total_incl_tax = calc.order_total_incl_tax(basket, shipping_method, **kwargs)
|
|
90
|
|
- total_excl_tax = calc.order_total_excl_tax(basket, shipping_method, **kwargs)
|
|
91
|
|
- return total_incl_tax, total_excl_tax
|
|
92
|
|
-
|
|
93
|
|
- def get_context_data(self, **kwargs):
|
|
94
|
|
- """
|
|
95
|
|
- Assign common template variables to the context.
|
|
96
|
|
- """
|
|
97
|
|
- ctx = super(CheckoutSessionMixin, self).get_context_data(**kwargs)
|
|
98
|
|
- ctx['shipping_address'] = self.get_shipping_address()
|
|
99
|
|
-
|
|
100
|
|
- method = self.get_shipping_method()
|
|
101
|
|
- if method:
|
|
102
|
|
- ctx['shipping_method'] = method
|
|
103
|
|
- ctx['shipping_total_excl_tax'] = method.basket_charge_excl_tax()
|
|
104
|
|
- ctx['shipping_total_incl_tax'] = method.basket_charge_incl_tax()
|
|
105
|
|
-
|
|
106
|
|
- ctx['order_total_incl_tax'], ctx['order_total_excl_tax'] = self.get_order_totals()
|
|
107
|
|
-
|
|
108
|
|
- return ctx
|
|
109
|
|
-
|
|
110
|
|
-
|
|
111
|
26
|
class OrderPlacementMixin(CheckoutSessionMixin):
|
|
112
|
27
|
"""
|
|
113
|
28
|
Mixin which provides functionality for placing orders.
|