Bläddra i källkod

Allow the user of an order to be specified

This helps with B2B situations where a sales rep places an order on
behalf of a customer.
master
David Winterbottom 13 år sedan
förälder
incheckning
ab77096d5a
1 ändrade filer med 35 tillägg och 20 borttagningar
  1. 35
    20
      oscar/apps/checkout/mixins.py

+ 35
- 20
oscar/apps/checkout/mixins.py Visa fil

39
     communication_type_code = 'ORDER_PLACED'
39
     communication_type_code = 'ORDER_PLACED'
40
 
40
 
41
     def handle_order_placement(self, order_number, basket, total_incl_tax,
41
     def handle_order_placement(self, order_number, basket, total_incl_tax,
42
-                               total_excl_tax, **kwargs):
42
+                               total_excl_tax, user=None, **kwargs):
43
         """
43
         """
44
         Write out the order models and return the appropriate HTTP response
44
         Write out the order models and return the appropriate HTTP response
45
 
45
 
46
         We deliberately pass the basket in here as the one tied to the request
46
         We deliberately pass the basket in here as the one tied to the request
47
-        isn't necessarily the correct one to use in placing the order.  This can
48
-        happen when a basket gets frozen.
47
+        isn't necessarily the correct one to use in placing the order.  This
48
+        can happen when a basket gets frozen.
49
         """
49
         """
50
-        order = self.place_order(order_number, basket, total_incl_tax, total_excl_tax, **kwargs)
50
+        order = self.place_order(order_number, basket, total_incl_tax,
51
+                                 total_excl_tax, user, **kwargs)
51
         basket.set_as_submitted()
52
         basket.set_as_submitted()
52
         return self.handle_successful_order(order)
53
         return self.handle_successful_order(order)
53
 
54
 
57
         self._payment_sources.append(source)
58
         self._payment_sources.append(source)
58
 
59
 
59
     def add_payment_event(self, event_type_name, amount):
60
     def add_payment_event(self, event_type_name, amount):
60
-        event_type, __ = PaymentEventType.objects.get_or_create(name=event_type_name)
61
+        event_type, __ = PaymentEventType.objects.get_or_create(
62
+            name=event_type_name)
61
         if self._payment_events is None:
63
         if self._payment_events is None:
62
             self._payment_events = []
64
             self._payment_events = []
63
         event = PaymentEvent(event_type=event_type, amount=amount)
65
         event = PaymentEvent(event_type=event_type, amount=amount)
65
 
67
 
66
     def handle_successful_order(self, order):
68
     def handle_successful_order(self, order):
67
         """
69
         """
68
-        Handle the various steps required after an order has been successfully placed.
70
+        Handle the various steps required after an order has been successfully
71
+        placed.
69
 
72
 
70
         Override this view if you want to perform custom actions when an
73
         Override this view if you want to perform custom actions when an
71
         order is submitted.
74
         order is submitted.
84
     def get_success_url(self):
87
     def get_success_url(self):
85
         return reverse('checkout:thank-you')
88
         return reverse('checkout:thank-you')
86
 
89
 
87
-    def place_order(self, order_number, basket, total_incl_tax, total_excl_tax, **kwargs):
90
+    def place_order(self, order_number, basket, total_incl_tax,
91
+                    total_excl_tax, user=None, **kwargs):
88
         """
92
         """
89
         Writes the order out to the DB including the payment models
93
         Writes the order out to the DB including the payment models
90
         """
94
         """
97
         else:
101
         else:
98
             status = kwargs.pop('status')
102
             status = kwargs.pop('status')
99
 
103
 
104
+        # We allow a user to be passed in to handle cases where the order is
105
+        # being placed on behalf of someone else.
106
+        if user is None:
107
+            user = self.request.user
108
+
100
         # Set guest email address for anon checkout.   Some libraries (eg
109
         # Set guest email address for anon checkout.   Some libraries (eg
101
         # PayPal) will pass this explicitly so we take care not to clobber.
110
         # PayPal) will pass this explicitly so we take care not to clobber.
102
-        if not self.request.user.is_authenticated() and 'guest_email' not in kwargs:
111
+        if (not self.request.user.is_authenticated() and 'guest_email'
112
+            not in kwargs):
103
             kwargs['guest_email'] = self.checkout_session.get_guest_email()
113
             kwargs['guest_email'] = self.checkout_session.get_guest_email()
104
 
114
 
105
         order = OrderCreator().place_order(basket=basket,
115
         order = OrderCreator().place_order(basket=basket,
106
                                            total_incl_tax=total_incl_tax,
116
                                            total_incl_tax=total_incl_tax,
107
                                            total_excl_tax=total_excl_tax,
117
                                            total_excl_tax=total_excl_tax,
108
-                                           user=self.request.user,
118
+                                           user=user,
109
                                            shipping_method=shipping_method,
119
                                            shipping_method=shipping_method,
110
                                            shipping_address=shipping_address,
120
                                            shipping_address=shipping_address,
111
                                            billing_address=billing_address,
121
                                            billing_address=billing_address,
160
             # Check that this address isn't already in the db as we don't want
170
             # Check that this address isn't already in the db as we don't want
161
             # to fill up the customer address book with duplicate addresses
171
             # to fill up the customer address book with duplicate addresses
162
             try:
172
             try:
163
-                UserAddress._default_manager.get(hash=user_addr.generate_hash())
173
+                UserAddress._default_manager.get(
174
+                    hash=user_addr.generate_hash())
164
             except ObjectDoesNotExist:
175
             except ObjectDoesNotExist:
165
                 user_addr.save()
176
                 user_addr.save()
166
 
177
 
210
         """
221
         """
211
         Saves any payment sources used in this order.
222
         Saves any payment sources used in this order.
212
 
223
 
213
-        When the payment sources are created, the order model does not exist and
214
-        so they need to have it set before saving.
224
+        When the payment sources are created, the order model does not exist
225
+        and so they need to have it set before saving.
215
         """
226
         """
216
         if not self._payment_sources:
227
         if not self._payment_sources:
217
             return
228
             return
228
 
239
 
229
     def restore_frozen_basket(self):
240
     def restore_frozen_basket(self):
230
         """
241
         """
231
-        Restores a frozen basket as the sole OPEN basket.  Note that this also merges
232
-        in any new products that have been added to a basket that has been created while payment.
242
+        Restores a frozen basket as the sole OPEN basket.  Note that this also
243
+        merges in any new products that have been added to a basket that has
244
+        been created while payment.
233
         """
245
         """
234
         try:
246
         try:
235
             fzn_basket = self.get_submitted_basket()
247
             fzn_basket = self.get_submitted_basket()
246
     def send_confirmation_message(self, order, **kwargs):
258
     def send_confirmation_message(self, order, **kwargs):
247
         code = self.communication_type_code
259
         code = self.communication_type_code
248
         ctx = {'order': order,
260
         ctx = {'order': order,
249
-               'lines': order.lines.all(),}
261
+               'lines': order.lines.all()}
250
 
262
 
251
         if not self.request.user.is_authenticated():
263
         if not self.request.user.is_authenticated():
252
             path = reverse('customer:anon-order',
264
             path = reverse('customer:anon-order',
258
         try:
270
         try:
259
             event_type = CommunicationEventType.objects.get(code=code)
271
             event_type = CommunicationEventType.objects.get(code=code)
260
         except CommunicationEventType.DoesNotExist:
272
         except CommunicationEventType.DoesNotExist:
261
-            # No event-type in database, attempt to find templates for this type
262
-            # and render them immediately to get the messages
273
+            # No event-type in database, attempt to find templates for this
274
+            # type and render them immediately to get the messages
263
             messages = CommunicationEventType.objects.get_and_render(code, ctx)
275
             messages = CommunicationEventType.objects.get_and_render(code, ctx)
264
             event_type = None
276
             event_type = None
265
         else:
277
         else:
266
             # Create order event
278
             # Create order event
267
-            CommunicationEvent._default_manager.create(order=order, event_type=event_type)
279
+            CommunicationEvent._default_manager.create(order=order,
280
+                                                       event_type=event_type)
268
             messages = event_type.get_messages(ctx)
281
             messages = event_type.get_messages(ctx)
269
 
282
 
270
         if messages and messages['body']:
283
         if messages and messages['body']:
271
             logger.info("Order #%s - sending %s messages", order.number, code)
284
             logger.info("Order #%s - sending %s messages", order.number, code)
272
             dispatcher = Dispatcher(logger)
285
             dispatcher = Dispatcher(logger)
273
-            dispatcher.dispatch_order_messages(order, messages, event_type, **kwargs)
286
+            dispatcher.dispatch_order_messages(order, messages,
287
+                                               event_type, **kwargs)
274
         else:
288
         else:
275
-            logger.warning("Order #%s - no %s communication event type", order.number, code)
289
+            logger.warning("Order #%s - no %s communication event type",
290
+                           order.number, code)

Laddar…
Avbryt
Spara