Переглянути джерело

Reworked the newly split out mixins

master
David Winterbottom 13 роки тому
джерело
коміт
2d5c9f794a

+ 3
- 88
oscar/apps/checkout/mixins.py Переглянути файл

@@ -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.

+ 87
- 0
oscar/apps/checkout/session.py Переглянути файл

@@ -0,0 +1,87 @@
1
+import logging
2
+
3
+from django.db.models import get_model
4
+
5
+from oscar.apps.shipping.methods import Free
6
+from oscar.core.loading import get_class
7
+OrderTotalCalculator = get_class('checkout.calculators', 'OrderTotalCalculator')
8
+CheckoutSessionData = get_class('checkout.utils', 'CheckoutSessionData')
9
+
10
+ShippingAddress = get_model('order', 'ShippingAddress')
11
+UserAddress = get_model('address', 'UserAddress')
12
+
13
+# Standard logger for checkout events
14
+logger = logging.getLogger('oscar.checkout')
15
+
16
+
17
+class CheckoutSessionMixin(object):
18
+    """
19
+    Mixin to provide common functionality shared between checkout views.
20
+    """
21
+
22
+    def dispatch(self, request, *args, **kwargs):
23
+        self.checkout_session = CheckoutSessionData(request)
24
+        return super(CheckoutSessionMixin, self).dispatch(request, *args, **kwargs)
25
+
26
+    def get_shipping_address(self):
27
+        """
28
+        Return the current shipping address for this checkout session.
29
+
30
+        This could either be a ShippingAddress model which has been
31
+        pre-populated (not saved), or a UserAddress model which will
32
+        need converting into a ShippingAddress model at submission
33
+        """
34
+        addr_data = self.checkout_session.new_shipping_address_fields()
35
+        if addr_data:
36
+            # Load address data into a blank address model
37
+            return ShippingAddress(**addr_data)
38
+        addr_id = self.checkout_session.user_address_id()
39
+        if addr_id:
40
+            try:
41
+                return UserAddress._default_manager.get(pk=addr_id)
42
+            except UserAddress.DoesNotExist:
43
+                # This can happen if you reset all your tables and you still have
44
+                # session data that refers to addresses that no longer exist
45
+                pass
46
+        return None
47
+
48
+    def get_shipping_method(self, basket=None):
49
+        method = self.checkout_session.shipping_method()
50
+        if method:
51
+            if not basket:
52
+                basket = self.request.basket
53
+            method.set_basket(basket)
54
+        else:
55
+            # We default to using free shipping
56
+            method = Free()
57
+        return method
58
+
59
+    def get_order_totals(self, basket=None, shipping_method=None, **kwargs):
60
+        """
61
+        Returns the total for the order with and without tax (as a tuple)
62
+        """
63
+        calc = OrderTotalCalculator(self.request)
64
+        if not basket:
65
+            basket = self.request.basket
66
+        if not shipping_method:
67
+            shipping_method = self.get_shipping_method(basket)
68
+        total_incl_tax = calc.order_total_incl_tax(basket, shipping_method, **kwargs)
69
+        total_excl_tax = calc.order_total_excl_tax(basket, shipping_method, **kwargs)
70
+        return total_incl_tax, total_excl_tax
71
+
72
+    def get_context_data(self, **kwargs):
73
+        """
74
+        Assign common template variables to the context.
75
+        """
76
+        ctx = super(CheckoutSessionMixin, self).get_context_data(**kwargs)
77
+        ctx['shipping_address'] = self.get_shipping_address()
78
+
79
+        method = self.get_shipping_method()
80
+        if method:
81
+            ctx['shipping_method'] = method
82
+            ctx['shipping_total_excl_tax'] = method.basket_charge_excl_tax()
83
+            ctx['shipping_total_incl_tax'] = method.basket_charge_incl_tax()
84
+
85
+        ctx['order_total_incl_tax'], ctx['order_total_excl_tax'] = self.get_order_totals()
86
+
87
+        return ctx

+ 2
- 2
oscar/apps/checkout/views.py Переглянути файл

@@ -23,8 +23,8 @@ Dispatcher = get_class('customer.utils', 'Dispatcher')
23 23
 RedirectRequired, UnableToTakePayment, PaymentError = get_classes(
24 24
     'payment.exceptions', ['RedirectRequired', 'UnableToTakePayment', 'PaymentError'])
25 25
 UnableToPlaceOrder = get_class('order.exceptions', 'UnableToPlaceOrder')
26
-CheckoutSessionMixin, OrderPlacementMixin = get_classes('checkout.mixins',
27
-    ('CheckoutSessionMixin', 'OrderPlacementMixin'))
26
+OrderPlacementMixin = get_class('checkout.mixins', 'OrderPlacementMixin')
27
+CheckoutSessionMixin = get_class('checkout.session', 'CheckoutSessionMixin')
28 28
 
29 29
 Order = get_model('order', 'Order')
30 30
 ShippingAddress = get_model('order', 'ShippingAddress')

Завантаження…
Відмінити
Зберегти