-
Notifications
You must be signed in to change notification settings - Fork 82
/
email.py
208 lines (185 loc) · 7.83 KB
/
email.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from collections import namedtuple
from django.core.exceptions import ImproperlyConfigured
from django.core.mail.message import EmailMultiAlternatives
from django.template.exceptions import TemplateDoesNotExist
from django.template.loader import get_template, render_to_string
from rest_registration.settings import registration_settings
from rest_registration.utils import get_user_setting, identity
EmailTemplateConfig = namedtuple('EmailTemplateConfig', (
'subject_template_name',
'text_body_template_name',
'html_body_template_name',
'text_body_processor',
))
def send_verification_notification(
user, params_signer, template_config_data, email=None):
notification = create_verification_notification(
user, params_signer, template_config_data, email=email)
send_notification(notification)
def create_verification_notification(
user, params_signer, template_config_data, email=None):
if email is None:
email_field = get_user_setting('EMAIL_FIELD')
email = getattr(user, email_field)
from_email = registration_settings.VERIFICATION_FROM_EMAIL
reply_to_email = (registration_settings.VERIFICATION_REPLY_TO_EMAIL or
from_email)
context = {
'user': user,
'email': email,
'verification_url': params_signer.get_url(),
}
template_config = parse_template_config(template_config_data)
subject = render_to_string(
template_config.subject_template_name, context=context).strip()
text_body = template_config.text_body_processor(
render_to_string(
template_config.text_body_template_name, context=context))
email_msg = EmailMultiAlternatives(
subject, text_body, from_email, [email], reply_to=[reply_to_email])
if template_config.html_body_template_name:
html_body = render_to_string(
template_config.html_body_template_name, context=context)
email_msg.attach_alternative(html_body, 'text/html')
return email_msg
def parse_template_config(template_config_data):
"""
>>> from tests import doctest_utils
>>> convert_html_to_text = registration_settings.VERIFICATION_EMAIL_HTML_TO_TEXT_CONVERTER # noqa: E501
>>> parse_template_config({}) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ImproperlyConfigured
>>> parse_template_config({
... 'subject': 'blah',
... }) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ImproperlyConfigured
>>> parse_template_config({
... 'subject': 'blah',
... 'body': 'blah',
... }) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ImproperlyConfigured
>>> doctest_utils.equals(
... parse_template_config({
... 'subject': 'rest_registration/register/subject.txt',
... 'html_body': 'rest_registration/register/body.html',
... 'text_body': 'rest_registration/register/body.txt',
... }),
... EmailTemplateConfig(
... 'rest_registration/register/subject.txt',
... 'rest_registration/register/body.txt',
... 'rest_registration/register/body.html',
... identity))
OK
>>> doctest_utils.equals(
... parse_template_config({
... 'subject': 'rest_registration/register/subject.txt',
... 'html_body': 'rest_registration/register/body.html',
... }),
... EmailTemplateConfig(
... 'rest_registration/register/subject.txt',
... 'rest_registration/register/body.html',
... 'rest_registration/register/body.html',
... convert_html_to_text))
OK
>>> doctest_utils.equals(
... parse_template_config({
... 'subject': 'rest_registration/register/subject.txt',
... 'text_body': 'rest_registration/register/body.txt',
... }),
... EmailTemplateConfig(
... 'rest_registration/register/subject.txt',
... 'rest_registration/register/body.txt', None,
... identity))
OK
>>> doctest_utils.equals(
... parse_template_config({
... 'subject': 'rest_registration/register/subject.txt',
... 'body': 'rest_registration/register/body.txt',
... }),
... EmailTemplateConfig(
... 'rest_registration/register/subject.txt',
... 'rest_registration/register/body.txt', None,
... identity))
OK
>>> doctest_utils.equals(
... parse_template_config({
... 'subject': 'rest_registration/register/subject.txt',
... 'body': 'rest_registration/register/body.html',
... 'is_html': True,
... }),
... EmailTemplateConfig(
... 'rest_registration/register/subject.txt',
... 'rest_registration/register/body.html',
... 'rest_registration/register/body.html',
... convert_html_to_text))
OK
"""
try:
subject_template_name = template_config_data['subject']
except KeyError:
raise ImproperlyConfigured("No 'subject' key found")
body_template_name = template_config_data.get('body')
text_body_template_name = template_config_data.get('text_body')
html_body_template_name = template_config_data.get('html_body')
is_html_body = template_config_data.get('is_html')
convert_html_to_text = registration_settings.VERIFICATION_EMAIL_HTML_TO_TEXT_CONVERTER # noqa: E501
if html_body_template_name and text_body_template_name:
config = EmailTemplateConfig(
subject_template_name=subject_template_name,
text_body_template_name=text_body_template_name,
html_body_template_name=html_body_template_name,
text_body_processor=identity,
)
elif html_body_template_name:
config = EmailTemplateConfig(
subject_template_name=subject_template_name,
text_body_template_name=html_body_template_name,
html_body_template_name=html_body_template_name,
text_body_processor=convert_html_to_text,
)
elif text_body_template_name:
config = EmailTemplateConfig(
subject_template_name=subject_template_name,
text_body_template_name=text_body_template_name,
html_body_template_name=None,
text_body_processor=identity,
)
elif body_template_name:
if is_html_body:
config = EmailTemplateConfig(
subject_template_name=subject_template_name,
text_body_template_name=body_template_name,
html_body_template_name=body_template_name,
text_body_processor=convert_html_to_text,
)
else:
config = EmailTemplateConfig(
subject_template_name=subject_template_name,
text_body_template_name=body_template_name,
html_body_template_name=None,
text_body_processor=identity,
)
else:
raise ImproperlyConfigured(
'Could not parse template config data: {template_config_data}'.format( # noqa: E501
template_config_data=template_config_data))
_validate_template_name_existence(config.subject_template_name)
_validate_template_name_existence(config.text_body_template_name)
if config.html_body_template_name:
_validate_template_name_existence(config.html_body_template_name)
assert callable(config.text_body_processor)
return config
def _validate_template_name_existence(template_name):
try:
get_template(template_name)
except TemplateDoesNotExist:
raise ImproperlyConfigured(
'Template {template_name!r} does not exists'.format(
template_name=template_name))
def send_notification(notification):
notification.send()