Преглед на файлове

Updated the checkout session manager with new methods

master
David Winterbottom преди 13 години
родител
ревизия
0b0c76540d
променени са 2 файла, в които са добавени 91 реда и са изтрити 12 реда
  1. 48
    12
      oscar/apps/checkout/utils.py
  2. 43
    0
      tests/unit/checkout_tests.py

+ 48
- 12
oscar/apps/checkout/utils.py Целия файл

@@ -43,6 +43,10 @@ class CheckoutSessionData(object):
43 43
             del self.request.session[self.SESSION_KEY][namespace][key]
44 44
             self.request.session.modified = True
45 45
 
46
+    def _flush_namespace(self, namespace):
47
+        self.request.session[self.SESSION_KEY][namespace] = {}
48
+        self.request.session.modified = True
49
+
46 50
     def flush(self):
47 51
         """
48 52
         Delete session key
@@ -57,12 +61,15 @@ class CheckoutSessionData(object):
57 61
     def get_guest_email(self):
58 62
         return self._get('guest', 'email')
59 63
 
60
-    # Shipping addresses
64
+    # Shipping address
65
+    # ================
66
+    # Options:
67
+    # 1. No shipping required (eg digital products)
68
+    # 2. Ship to new address (entered in a form)
69
+    # 3. Ship to an addressbook address (address chosen from list)
61 70
 
62 71
     def reset_shipping_data(self):
63
-        self._unset('shipping', 'not_required')
64
-        self._unset('shipping', 'new_address_fields')
65
-        self._unset('shipping', 'user_address_id')
72
+        self._flush_namespace('shipping')
66 73
 
67 74
     def no_shipping_required(self):
68 75
         """
@@ -98,11 +105,12 @@ class CheckoutSessionData(object):
98 105
         """
99 106
         return self._get('shipping', 'new_address_fields')
100 107
 
101
-    def user_address_id(self):
108
+    def shipping_user_address_id(self):
102 109
         """
103 110
         Get user address id from session
104 111
         """
105 112
         return self._get('shipping', 'user_address_id')
113
+    user_address_id = shipping_user_address_id
106 114
 
107 115
     def is_shipping_required(self):
108 116
         return self._get('shipping', 'is_required', True)
@@ -113,7 +121,8 @@ class CheckoutSessionData(object):
113 121
         has_old_address = self.user_address_id() > 0
114 122
         return has_new_address or has_old_address
115 123
 
116
-    # Shipping methods
124
+    # Shipping method
125
+    # ===============
117 126
 
118 127
     def use_free_shipping(self):
119 128
         """
@@ -141,30 +150,57 @@ class CheckoutSessionData(object):
141 150
         return bool(self._get('shipping', 'method_code'))
142 151
 
143 152
     # Billing address fields
153
+    # ======================
154
+    #
155
+    # There are 3 common options:
156
+    # 1. Billing address is entered manually through a form
157
+    # 2. Billing address is selected from address book
158
+    # 3. Billing address is the same as the shipping address
144 159
 
145 160
     def bill_to_new_address(self, address_fields):
146 161
         """
147 162
         Store address fields for a billing address.
148 163
         """
164
+        self._flush_namespace('billing')
149 165
         self._set('billing', 'new_address_fields', address_fields)
150 166
 
151
-    def new_billing_address_fields(self):
167
+    def bill_to_user_address(self, address):
152 168
         """
153
-        Return fields for a billing address
169
+        Set an address from a user's address book as the billing address
170
+
171
+        :address: The address object
154 172
         """
155
-        return self._get('billing', 'new_address_fields')
173
+        self._flush_namespace('billing')
174
+        self._set('billing', 'user_address_id', address.id)
156 175
 
157
-    def billing_address_same_as_shipping(self):
176
+    def bill_to_shipping_address(self):
158 177
         """
159 178
         Record fact that the billing address is to be the same as
160 179
         the shipping address.
161 180
         """
162
-        self._set('payment', 'billing_address_same_as_shipping', True)
181
+        self._flush_namespace('billing')
182
+        self._set('billing', 'billing_address_same_as_shipping', True)
183
+
184
+    # Legacy method name
185
+    billing_address_same_as_shipping = bill_to_shipping_address
163 186
 
164 187
     def is_billing_address_same_as_shipping(self):
165
-        return self._get('payment', 'billing_address_same_as_shipping', False)
188
+        return self._get('billing', 'billing_address_same_as_shipping', False)
189
+
190
+    def billing_user_address_id(self):
191
+        """
192
+        Return the ID of the user address being used for billing
193
+        """
194
+        return self._get('billing', 'user_address_id')
195
+
196
+    def new_billing_address_fields(self):
197
+        """
198
+        Return fields for a billing address
199
+        """
200
+        return self._get('billing', 'new_address_fields')
166 201
 
167 202
     # Payment methods
203
+    # ===============
168 204
 
169 205
     def pay_by(self, method):
170 206
         self._set('payment', 'method', method)

+ 43
- 0
tests/unit/checkout_tests.py Целия файл

@@ -0,0 +1,43 @@
1
+from django.test import TestCase
2
+from django.test.client import RequestFactory
3
+from django.contrib.sessions.middleware import SessionMiddleware
4
+import mock
5
+
6
+from oscar.apps.checkout.utils import CheckoutSessionData
7
+
8
+
9
+class TestCheckoutSession(TestCase):
10
+    """
11
+    oscar.apps.checkout.utils.CheckoutSessionData
12
+    """
13
+
14
+    def setUp(self):
15
+        request = RequestFactory().get('/')
16
+        SessionMiddleware().process_request(request)
17
+        self.session_data = CheckoutSessionData(request)
18
+
19
+    def test_allows_data_to_be_written_and_read_out(self):
20
+        self.session_data._set('namespace', 'key', 'value')
21
+        self.assertEqual('value', self.session_data._get('namespace', 'key'))
22
+
23
+    def test_allows_set_data_can_be_unset(self):
24
+        self.session_data._set('namespace', 'key', 'value')
25
+        self.session_data._unset('namespace', 'key')
26
+        self.assertIsNone(self.session_data._get('namespace', 'key'))
27
+
28
+    def test_stores_guest_email(self):
29
+        self.session_data.set_guest_email('a@a.com')
30
+        self.assertEquals('a@a.com', self.session_data.get_guest_email())
31
+
32
+    def test_allows_a_namespace_to_be_flushed(self):
33
+        self.session_data._set('ns', 'a', 1)
34
+        self.session_data._set('ns', 'b', 2)
35
+        self.session_data._flush_namespace('ns')
36
+        self.assertIsNone(self.session_data._get('ns', 'a'))
37
+        self.assertIsNone(self.session_data._get('ns', 'b'))
38
+
39
+    def test_allows_bill_to_user_address(self):
40
+        address = mock.Mock()
41
+        address.id = 1
42
+        self.session_data.bill_to_user_address(address)
43
+        self.assertEqual(1, self.session_data.billing_user_address_id())

Loading…
Отказ
Запис