Browse Source

Return shipping address without saving

OrderPaymentMixin has a new method `get_shipping_address` which returns
shipping address without saving it. Useful as part of checkout process.
master
Izidor Matušov 12 years ago
parent
commit
e224edea93
2 changed files with 52 additions and 15 deletions
  1. 7
    7
      oscar/apps/address/abstract_models.py
  2. 45
    8
      oscar/apps/checkout/mixins.py

+ 7
- 7
oscar/apps/address/abstract_models.py View File

@@ -197,6 +197,13 @@ class AbstractShippingAddress(AbstractAddress):
197 197
         help_text=_("For example, leave the parcel in the wheelie bin "
198 198
                     "if I'm not in."))
199 199
 
200
+    def generate_hash(self):
201
+        """
202
+        Returns a hash of the address summary
203
+        """
204
+        # We use an upper-case version of the summary
205
+        return zlib.crc32(self.summary.strip().upper().encode('UTF8'))
206
+
200 207
     class Meta:
201 208
         abstract = True
202 209
         verbose_name = _("Shipping address")
@@ -245,13 +252,6 @@ class AbstractUserAddress(AbstractShippingAddress):
245 252
     hash = models.CharField(_("Address Hash"), max_length=255, db_index=True)
246 253
     date_created = models.DateTimeField(_("Date Created"), auto_now_add=True)
247 254
 
248
-    def generate_hash(self):
249
-        """
250
-        Returns a hash of the address summary
251
-        """
252
-        # We use an upper-case version of the summary
253
-        return zlib.crc32(self.summary.strip().upper().encode('UTF8'))
254
-
255 255
     def save(self, *args, **kwargs):
256 256
         """
257 257
         Save a hash of the address fields

+ 45
- 8
oscar/apps/checkout/mixins.py View File

@@ -7,6 +7,7 @@ from django.core.exceptions import ObjectDoesNotExist
7 7
 from django.db.models import get_model
8 8
 
9 9
 from oscar.core.loading import get_class
10
+from oscar.core.decorators import deprecated
10 11
 
11 12
 OrderCreator = get_class('order.utils', 'OrderCreator')
12 13
 Dispatcher = get_class('customer.utils', 'Dispatcher')
@@ -138,17 +139,19 @@ class OrderPlacementMixin(CheckoutSessionMixin):
138 139
         self.save_payment_details(order)
139 140
         return order
140 141
 
141
-    def create_shipping_address(self, basket=None):
142
+    def get_shipping_address(self, basket=None):
142 143
         """
143
-        Create and returns the shipping address for the current order.
144
+        Return the shipping address for the current order.
144 145
 
145 146
         If the shipping address was entered manually, then we simply
146
-        write out a ShippingAddress model with the appropriate form data.  If
147
-        the user is authenticated, then we create a UserAddress from this data
148
-        too so it can be re-used in the future.
147
+        write out a ShippingAddress model with the appropriate form data.
149 148
 
150 149
         If the shipping address was selected from the user's address book,
151 150
         then we convert the UserAddress to a ShippingAddress.
151
+
152
+        ShippingAddress is not saved and should be used for read-only purposes.
153
+        See self.create_shipping_address() which also saves the address and
154
+        makes sure that appropriate UserAddress exists.
152 155
         """
153 156
         if not basket:
154 157
             basket = self.request.basket
@@ -158,20 +161,53 @@ class OrderPlacementMixin(CheckoutSessionMixin):
158 161
         addr_data = self.checkout_session.new_shipping_address_fields()
159 162
         addr_id = self.checkout_session.user_address_id()
160 163
         if addr_data:
161
-            addr = self.create_shipping_address_from_form_fields(addr_data)
162
-            self.create_user_address(addr_data)
164
+            return ShippingAddress(**addr_data)
163 165
         elif addr_id:
164
-            addr = self.create_shipping_address_from_user_address(addr_id)
166
+            address = UserAddress._default_manager.get(pk=addr_id)
167
+            shipping_addr = ShippingAddress()
168
+            address.populate_alternative_model(shipping_addr)
169
+            return shipping_addr
165 170
         else:
166 171
             raise AttributeError("No shipping address data found")
172
+
173
+    def create_shipping_address(self, basket=None):
174
+        """
175
+        Create and returns the shipping address for the current order.
176
+
177
+        Compared to self.get_shipping_address(), ShippingAddress is saved and
178
+        makes sure that appropriate UserAddress exists.
179
+        """
180
+        addr = self.get_shipping_address(basket=basket)
181
+        if addr:
182
+            self.update_address_book(addr)
167 183
         return addr
168 184
 
185
+    def update_address_book(self, shipping_addr):
186
+        """
187
+        Save ShippingAddress and increase the number of orders for UserAddress.
188
+        """
189
+        shipping_addr.save()
190
+        if self.request.user.is_authenticated():
191
+            try:
192
+                # Bump number of orders for the existing address
193
+                user_addr = UserAddress._default_manager.get(
194
+                    hash=shipping_addr.generate_hash())
195
+            except ObjectDoesNotExist:
196
+                # Create a new user address
197
+                user_addr = UserAddress(user_id=self.request.user.id)
198
+                shipping_addr.populate_alternative_model(user_addr)
199
+
200
+            user_addr.num_orders += 1
201
+            user_addr.save()
202
+
203
+    @deprecated
169 204
     def create_shipping_address_from_form_fields(self, addr_data):
170 205
         """Creates a shipping address model from the saved form fields"""
171 206
         shipping_addr = ShippingAddress(**addr_data)
172 207
         shipping_addr.save()
173 208
         return shipping_addr
174 209
 
210
+    @deprecated
175 211
     def create_user_address(self, session_addr_data):
176 212
         """
177 213
         For signed-in users, we create a user address model which will go
@@ -189,6 +225,7 @@ class OrderPlacementMixin(CheckoutSessionMixin):
189 225
             except ObjectDoesNotExist:
190 226
                 user_addr.save()
191 227
 
228
+    @deprecated
192 229
     def create_shipping_address_from_user_address(self, addr_id):
193 230
         """Creates a shipping address from a user address"""
194 231
         address = UserAddress._default_manager.get(pk=addr_id)

Loading…
Cancel
Save