|
|
@@ -1,14 +1,18 @@
|
|
1
|
1
|
import logging
|
|
|
2
|
+import warnings
|
|
2
|
3
|
|
|
3
|
4
|
from django.conf import settings
|
|
4
|
5
|
from django.contrib.sites.models import Site
|
|
5
|
6
|
from django.core import mail
|
|
6
|
7
|
from django.db.models import Max
|
|
7
|
|
-from django.template import loader
|
|
|
8
|
+from django.template import loader, TemplateDoesNotExist
|
|
8
|
9
|
|
|
9
|
10
|
from oscar.apps.customer.notifications import services
|
|
|
11
|
+from oscar.apps.customer.utils import Dispatcher
|
|
10
|
12
|
from oscar.core.loading import get_class, get_model
|
|
|
13
|
+from oscar.utils.deprecation import RemovedInOscar20Warning
|
|
11
|
14
|
|
|
|
15
|
+CommunicationEventType = get_model('customer', 'CommunicationEventType')
|
|
12
|
16
|
ProductAlert = get_model('customer', 'ProductAlert')
|
|
13
|
17
|
Product = get_model('catalogue', 'Product')
|
|
14
|
18
|
Selector = get_class('partner.strategy', 'Selector')
|
|
|
@@ -36,16 +40,38 @@ def send_alert_confirmation(alert):
|
|
36
|
40
|
'alert': alert,
|
|
37
|
41
|
'site': Site.objects.get_current(),
|
|
38
|
42
|
}
|
|
39
|
|
- subject_tpl = loader.get_template('customer/alerts/emails/'
|
|
40
|
|
- 'confirmation_subject.txt')
|
|
41
|
|
- body_tpl = loader.get_template('customer/alerts/emails/'
|
|
42
|
|
- 'confirmation_body.txt')
|
|
43
|
|
- mail.send_mail(
|
|
44
|
|
- subject_tpl.render(ctx).strip(),
|
|
45
|
|
- body_tpl.render(ctx),
|
|
46
|
|
- settings.OSCAR_FROM_EMAIL,
|
|
47
|
|
- [alert.email],
|
|
48
|
|
- )
|
|
|
43
|
+
|
|
|
44
|
+ # For backwards compability, we check if the old (non-communication-event)
|
|
|
45
|
+ # templates exist, and use them if they do.
|
|
|
46
|
+ # This will be removed in Oscar 2.0
|
|
|
47
|
+ try:
|
|
|
48
|
+ subject_tpl = loader.get_template('customer/alerts/emails/'
|
|
|
49
|
+ 'confirmation_subject.txt')
|
|
|
50
|
+ body_tpl = loader.get_template('customer/alerts/emails/'
|
|
|
51
|
+ 'confirmation_body.txt')
|
|
|
52
|
+ warnings.warn(
|
|
|
53
|
+ "Product alert notifications now use the CommunicationEvent. "
|
|
|
54
|
+ "Move '{}' to '{}', and '{}' to '{}'".format(
|
|
|
55
|
+ 'customer/alerts/emails/confirmation_subject.txt',
|
|
|
56
|
+ 'customer/emails/commtype_product_alert_confirmation_subject.txt',
|
|
|
57
|
+ 'customer/alerts/emails/confirmation_body.txt',
|
|
|
58
|
+ 'customer/emails/commtype_product_alert_confirmation_body.txt',
|
|
|
59
|
+ ),
|
|
|
60
|
+ category=RemovedInOscar20Warning, stacklevel=2
|
|
|
61
|
+ )
|
|
|
62
|
+
|
|
|
63
|
+ messages = {
|
|
|
64
|
+ 'subject': subject_tpl.render(ctx).strip(),
|
|
|
65
|
+ 'body': body_tpl.render(ctx),
|
|
|
66
|
+ 'html': '',
|
|
|
67
|
+ 'sms': '',
|
|
|
68
|
+ }
|
|
|
69
|
+ except TemplateDoesNotExist:
|
|
|
70
|
+ code = 'PRODUCT_ALERT_CONFIRMATION'
|
|
|
71
|
+ messages = CommunicationEventType.objects.get_and_render(code, ctx)
|
|
|
72
|
+
|
|
|
73
|
+ if messages and messages['body']:
|
|
|
74
|
+ Dispatcher().dispatch_direct_messages(alert.email, messages)
|
|
49
|
75
|
|
|
50
|
76
|
|
|
51
|
77
|
def send_product_alerts(product):
|
|
|
@@ -75,14 +101,37 @@ def send_product_alerts(product):
|
|
75
|
101
|
# hurry_mode is false if num_in_stock is None
|
|
76
|
102
|
hurry_mode = num_in_stock is not None and alerts.count() > num_in_stock
|
|
77
|
103
|
|
|
78
|
|
- # Load templates
|
|
79
|
|
- message_tpl = loader.get_template('customer/alerts/message.html')
|
|
80
|
|
- email_subject_tpl = loader.get_template('customer/alerts/emails/'
|
|
81
|
|
- 'alert_subject.txt')
|
|
82
|
|
- email_body_tpl = loader.get_template('customer/alerts/emails/'
|
|
83
|
|
- 'alert_body.txt')
|
|
|
104
|
+ # For backwards compability, we check if the old (non-communication-event)
|
|
|
105
|
+ # templates exist, and use them if they do.
|
|
|
106
|
+ # This will be removed in Oscar 2.0
|
|
|
107
|
+ try:
|
|
|
108
|
+ email_subject_tpl = loader.get_template('customer/alerts/emails/'
|
|
|
109
|
+ 'alert_subject.txt')
|
|
|
110
|
+ email_body_tpl = loader.get_template('customer/alerts/emails/'
|
|
|
111
|
+ 'alert_body.txt')
|
|
|
112
|
+
|
|
|
113
|
+ use_deprecated_templates = True
|
|
|
114
|
+ warnings.warn(
|
|
|
115
|
+ "Product alert notifications now use the CommunicationEvent. "
|
|
|
116
|
+ "Move '{}' to '{}', and '{}' to '{}'".format(
|
|
|
117
|
+ 'customer/alerts/emails/alert_subject.txt',
|
|
|
118
|
+ 'customer/emails/commtype_product_alert_subject.txt',
|
|
|
119
|
+ 'customer/alerts/emails/alert_body.txt',
|
|
|
120
|
+ 'customer/emails/commtype_product_alert_body.txt',
|
|
|
121
|
+ ),
|
|
|
122
|
+ category=RemovedInOscar20Warning, stacklevel=2
|
|
|
123
|
+ )
|
|
|
124
|
+
|
|
|
125
|
+ except TemplateDoesNotExist:
|
|
|
126
|
+ code = 'PRODUCT_ALERT'
|
|
|
127
|
+ try:
|
|
|
128
|
+ event_type = CommunicationEventType.objects.get(code=code)
|
|
|
129
|
+ except CommunicationEventType.DoesNotExist:
|
|
|
130
|
+ event_type = CommunicationEventType.objects.model(code=code)
|
|
|
131
|
+ use_deprecated_templates = False
|
|
84
|
132
|
|
|
85
|
|
- emails = []
|
|
|
133
|
+ messages_to_send = []
|
|
|
134
|
+ user_messages_to_send = []
|
|
86
|
135
|
num_notifications = 0
|
|
87
|
136
|
selector = Selector()
|
|
88
|
137
|
for alert in alerts:
|
|
|
@@ -100,26 +149,41 @@ def send_product_alerts(product):
|
|
100
|
149
|
if alert.user:
|
|
101
|
150
|
# Send a site notification
|
|
102
|
151
|
num_notifications += 1
|
|
|
152
|
+ message_tpl = loader.get_template('customer/alerts/message.html')
|
|
103
|
153
|
services.notify_user(alert.user, message_tpl.render(ctx))
|
|
104
|
154
|
|
|
105
|
|
- # Build email and add to list
|
|
106
|
|
- emails.append(
|
|
107
|
|
- mail.EmailMessage(
|
|
108
|
|
- email_subject_tpl.render(ctx).strip(),
|
|
109
|
|
- email_body_tpl.render(ctx),
|
|
110
|
|
- settings.OSCAR_FROM_EMAIL,
|
|
111
|
|
- [alert.get_email_address()],
|
|
112
|
|
- )
|
|
113
|
|
- )
|
|
|
155
|
+ # Build message and add to list
|
|
|
156
|
+ if use_deprecated_templates:
|
|
|
157
|
+ messages = {
|
|
|
158
|
+ 'subject': email_subject_tpl.render(ctx).strip(),
|
|
|
159
|
+ 'body': email_body_tpl.render(ctx),
|
|
|
160
|
+ 'html': '',
|
|
|
161
|
+ 'sms': '',
|
|
|
162
|
+ }
|
|
|
163
|
+ else:
|
|
|
164
|
+ messages = event_type.get_messages(ctx)
|
|
|
165
|
+
|
|
|
166
|
+ if messages and messages['body']:
|
|
|
167
|
+ if alert.user:
|
|
|
168
|
+ user_messages_to_send.append(
|
|
|
169
|
+ (alert.user, messages)
|
|
|
170
|
+ )
|
|
|
171
|
+ else:
|
|
|
172
|
+ messages_to_send.append(
|
|
|
173
|
+ (alert.get_email_address(), messages)
|
|
|
174
|
+ )
|
|
114
|
175
|
alert.close()
|
|
115
|
176
|
|
|
116
|
|
- # Send all emails in one go to prevent multiple SMTP
|
|
117
|
|
- # connections to be opened
|
|
118
|
|
- if emails:
|
|
|
177
|
+ # Send all messages using one SMTP connection to avoid opening lots of them
|
|
|
178
|
+ if messages_to_send or user_messages_to_send:
|
|
119
|
179
|
connection = mail.get_connection()
|
|
120
|
180
|
connection.open()
|
|
121
|
|
- connection.send_messages(emails)
|
|
|
181
|
+ disp = Dispatcher(mail_connection=connection)
|
|
|
182
|
+ for message in messages_to_send:
|
|
|
183
|
+ disp.dispatch_direct_messages(*message)
|
|
|
184
|
+ for message in user_messages_to_send:
|
|
|
185
|
+ disp.dispatch_user_messages(*message)
|
|
122
|
186
|
connection.close()
|
|
123
|
187
|
|
|
124
|
|
- logger.info("Sent %d notifications and %d emails", num_notifications,
|
|
125
|
|
- len(emails))
|
|
|
188
|
+ logger.info("Sent %d notifications and %d messages", num_notifications,
|
|
|
189
|
+ len(messages_to_send) + len(user_messages_to_send))
|