瀏覽代碼

fixes issue with sending order related messages using Dispatcher

master
Sebastian Vetter 13 年之前
父節點
當前提交
c718580cec

+ 15
- 14
oscar/apps/customer/utils.py 查看文件

@@ -8,7 +8,7 @@ CommunicationEvent = get_model('order', 'CommunicationEvent')
8 8
 Email = get_model('customer', 'Email')
9 9
 
10 10
 
11
-class Dispatcher(object): 
11
+class Dispatcher(object):
12 12
     def __init__(self, logger=None):
13 13
         if not logger:
14 14
             logger = logging.getLogger(__name__)
@@ -22,7 +22,7 @@ class Dispatcher(object):
22 22
         """
23 23
         if messages['subject'] and messages['body']:
24 24
             self.send_email_messages(recipient, messages)
25
-    
25
+
26 26
     def dispatch_order_messages(self, order, messages, event_type=None, **kwargs):
27 27
         """
28 28
         Dispatch order-related messages to the customer
@@ -36,11 +36,12 @@ class Dispatcher(object):
36 36
                 return
37 37
         else:
38 38
             self.dispatch_user_messages(order.user, messages)
39
-            
39
+
40 40
         # Create order comms event for audit
41 41
         if event_type:
42
-            CommunicationEvent._default_manager.create(order=order, type=event_type)
43
-    
42
+            CommunicationEvent._default_manager.create(order=order,
43
+                                                       event_type=event_type)
44
+
44 45
     def dispatch_user_messages(self, user, messages):
45 46
         """
46 47
         Send messages to a site user
@@ -49,9 +50,9 @@ class Dispatcher(object):
49 50
             self.send_user_email_messages(user, messages)
50 51
         if messages['sms']:
51 52
             self.send_text_message(user, messages['sms'])
52
-    
53
+
53 54
     # Internal
54
-    
55
+
55 56
     def send_user_email_messages(self, user, messages):
56 57
         """
57 58
         Sends message to the registered user / customer and collects data in database
@@ -59,16 +60,16 @@ class Dispatcher(object):
59 60
         if not user.email:
60 61
             self.logger.warning("Unable to send email messages as user #%d has no email address", user.id)
61 62
             return
62
-        
63
+
63 64
         email = self.send_email_messages(user.email, messages)
64
-        
65
+
65 66
         # Is user is signed in, record the event for audit
66 67
         if email and user.is_authenticated():
67
-            Email._default_manager.create(user=user, 
68
+            Email._default_manager.create(user=user,
68 69
                                           subject=email.subject,
69 70
                                           body_text=email.body,
70 71
                                           body_html=messages['html'])
71
-    
72
+
72 73
     def send_email_messages(self, recipient, messages):
73 74
         """
74 75
         Plain email sending to the specified recipient
@@ -77,7 +78,7 @@ class Dispatcher(object):
77 78
             from_email = settings.OSCAR_FROM_EMAIL
78 79
         else:
79 80
             from_email = None
80
-            
81
+
81 82
         # Determine whether we are sending a HTML version too
82 83
         if messages['html']:
83 84
             email = EmailMultiAlternatives(messages['subject'],
@@ -92,9 +93,9 @@ class Dispatcher(object):
92 93
                                  to=[recipient])
93 94
         self.logger.info("Sending email to %s" % recipient)
94 95
         email.send()
95
-        
96
+
96 97
         return email
97
-        
98
+
98 99
     def send_text_message(self, user, event_type):
99 100
         raise NotImplementedError
100 101
 

+ 1
- 1
oscar/templates/oscar/customer/emails/commtype_order_placed_body.html 查看文件

@@ -1,6 +1,6 @@
1 1
 {% load currency_filters i18n %}<p xmlns="http://www.w3.org/1999/html">{% trans 'Hello,' %}</p>
2 2
 
3
-<p>{% blocktrans %}We are pleased to confirm your order {{ order.number }} has been received and
3
+<p>{% blocktrans with order_number=order.number %}We are pleased to confirm your order {{ order_number }} has been received and
4 4
 will be processed shortly.{% endblocktrans %}</p>
5 5
 
6 6
 <p>{% trans 'Your order contains:' %}</p>

+ 2
- 2
oscar/templates/oscar/customer/emails/commtype_order_placed_body.txt 查看文件

@@ -1,6 +1,6 @@
1
-{% load currency_filters i18n %}{% blocktrans %}Hello,
1
+{% load currency_filters i18n %}{% blocktrans with order_number=order.number %}Hello,
2 2
 
3
-We are pleased to confirm your order {{ order.number }} has been received and
3
+We are pleased to confirm your order {{ order_number }} has been received and
4 4
 will be processed shortly.{% endblocktrans %}
5 5
 
6 6
 {% trans 'Your order contains:' %}

+ 34
- 0
tests/unit/customer/customer_tests.py 查看文件

@@ -1,5 +1,9 @@
1 1
 from django.test import TestCase
2
+from django.core import mail
3
+from django.contrib.auth.models import User
2 4
 
5
+from oscar.apps.customer.utils import Dispatcher
6
+from oscar.test.helpers import create_order
3 7
 from oscar.apps.customer.models import CommunicationEventType
4 8
 
5 9
 
@@ -17,3 +21,33 @@ class CommunicationTypeTest(TestCase):
17 21
         ctx = {'name': 'world'}
18 22
         messages = et.get_messages(ctx)
19 23
         self.assertEqual('Hello world', messages['subject'])
24
+
25
+
26
+class TestDispatcher(TestCase):
27
+
28
+    def test_sending_a_order_related_messages(self):
29
+        email = 'testuser@example.com'
30
+        user = User.objects.create_user('testuser', email,
31
+                                        'somesimplepassword')
32
+
33
+        order_number = '12345'
34
+        order = create_order(number=order_number, user=user)
35
+        et = CommunicationEventType.objects.create(code="ORDER_PLACED",
36
+                                                   name="Order Placed",
37
+                                                   category="Order related")
38
+
39
+        messages = et.get_messages({
40
+            'order': order,
41
+            'lines': order.lines.all()
42
+        })
43
+
44
+        self.assertIn(order_number, messages['body'])
45
+        self.assertIn(order_number, messages['html'])
46
+
47
+        dispatcher = Dispatcher()
48
+        dispatcher.dispatch_order_messages(order, messages, et)
49
+
50
+        self.assertEquals(len(mail.outbox), 1)
51
+
52
+        message = mail.outbox[0]
53
+        self.assertIn(order_number, message.body)

Loading…
取消
儲存