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
         help_text=_("For example, leave the parcel in the wheelie bin "
197
         help_text=_("For example, leave the parcel in the wheelie bin "
198
                     "if I'm not in."))
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
     class Meta:
207
     class Meta:
201
         abstract = True
208
         abstract = True
202
         verbose_name = _("Shipping address")
209
         verbose_name = _("Shipping address")
245
     hash = models.CharField(_("Address Hash"), max_length=255, db_index=True)
252
     hash = models.CharField(_("Address Hash"), max_length=255, db_index=True)
246
     date_created = models.DateTimeField(_("Date Created"), auto_now_add=True)
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
     def save(self, *args, **kwargs):
255
     def save(self, *args, **kwargs):
256
         """
256
         """
257
         Save a hash of the address fields
257
         Save a hash of the address fields

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

7
 from django.db.models import get_model
7
 from django.db.models import get_model
8
 
8
 
9
 from oscar.core.loading import get_class
9
 from oscar.core.loading import get_class
10
+from oscar.core.decorators import deprecated
10
 
11
 
11
 OrderCreator = get_class('order.utils', 'OrderCreator')
12
 OrderCreator = get_class('order.utils', 'OrderCreator')
12
 Dispatcher = get_class('customer.utils', 'Dispatcher')
13
 Dispatcher = get_class('customer.utils', 'Dispatcher')
138
         self.save_payment_details(order)
139
         self.save_payment_details(order)
139
         return order
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
         If the shipping address was entered manually, then we simply
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
         If the shipping address was selected from the user's address book,
149
         If the shipping address was selected from the user's address book,
151
         then we convert the UserAddress to a ShippingAddress.
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
         if not basket:
156
         if not basket:
154
             basket = self.request.basket
157
             basket = self.request.basket
158
         addr_data = self.checkout_session.new_shipping_address_fields()
161
         addr_data = self.checkout_session.new_shipping_address_fields()
159
         addr_id = self.checkout_session.user_address_id()
162
         addr_id = self.checkout_session.user_address_id()
160
         if addr_data:
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
         elif addr_id:
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
         else:
170
         else:
166
             raise AttributeError("No shipping address data found")
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
         return addr
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
     def create_shipping_address_from_form_fields(self, addr_data):
204
     def create_shipping_address_from_form_fields(self, addr_data):
170
         """Creates a shipping address model from the saved form fields"""
205
         """Creates a shipping address model from the saved form fields"""
171
         shipping_addr = ShippingAddress(**addr_data)
206
         shipping_addr = ShippingAddress(**addr_data)
172
         shipping_addr.save()
207
         shipping_addr.save()
173
         return shipping_addr
208
         return shipping_addr
174
 
209
 
210
+    @deprecated
175
     def create_user_address(self, session_addr_data):
211
     def create_user_address(self, session_addr_data):
176
         """
212
         """
177
         For signed-in users, we create a user address model which will go
213
         For signed-in users, we create a user address model which will go
189
             except ObjectDoesNotExist:
225
             except ObjectDoesNotExist:
190
                 user_addr.save()
226
                 user_addr.save()
191
 
227
 
228
+    @deprecated
192
     def create_shipping_address_from_user_address(self, addr_id):
229
     def create_shipping_address_from_user_address(self, addr_id):
193
         """Creates a shipping address from a user address"""
230
         """Creates a shipping address from a user address"""
194
         address = UserAddress._default_manager.get(pk=addr_id)
231
         address = UserAddress._default_manager.get(pk=addr_id)

Loading…
Cancel
Save