|
1
|
|
|
from copy import deepcopy |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
from django.contrib.sites.shortcuts import get_current_site |
|
|
|
|
|
|
4
|
|
|
from django.core import mail |
|
|
|
|
|
|
5
|
|
|
from django.template.context import make_context |
|
|
|
|
|
|
6
|
|
|
from django.template.loader import get_template |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
from django.conf import settings |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class BaseEmailMessage(mail.EmailMultiAlternatives): |
|
|
|
|
|
|
12
|
|
|
_node_map = { |
|
13
|
|
|
'subject': 'subject', |
|
14
|
|
|
'text_body': 'body', |
|
15
|
|
|
'html_body': 'html', |
|
16
|
|
|
} |
|
17
|
|
|
template_name = None |
|
18
|
|
|
|
|
19
|
|
|
def __init__(self, request=None, context=None, template_name=None, |
|
20
|
|
|
*args, **kwargs): |
|
21
|
|
|
super(BaseEmailMessage, self).__init__(*args, **kwargs) |
|
22
|
|
|
|
|
23
|
|
|
self.request = request |
|
24
|
|
|
self.context = {} if context is None else context |
|
25
|
|
|
self.html = None |
|
26
|
|
|
|
|
27
|
|
|
if template_name is not None: |
|
28
|
|
|
self.template_name = template_name |
|
29
|
|
|
|
|
30
|
|
|
def get_context_data(self): |
|
|
|
|
|
|
31
|
|
|
context = deepcopy(self.context) |
|
32
|
|
|
if self.request: |
|
33
|
|
|
site = get_current_site(self.request) |
|
34
|
|
|
domain = context.get('domain') or ( |
|
35
|
|
|
getattr(settings, 'DOMAIN', '') or site.domain |
|
36
|
|
|
) |
|
37
|
|
|
protocol = context.get('protocol') or ( |
|
38
|
|
|
'https' if self.request.is_secure() else 'http' |
|
39
|
|
|
) |
|
40
|
|
|
site_name = context.get('site_name') or ( |
|
41
|
|
|
getattr(settings, 'SITE_NAME', '') or site.name |
|
42
|
|
|
) |
|
43
|
|
|
user = context.get('user') or self.request.user |
|
44
|
|
|
else: |
|
45
|
|
|
domain = context.get('domain') or getattr(settings, 'DOMAIN', '') |
|
46
|
|
|
protocol = context.get('protocol') or 'http' |
|
47
|
|
|
site_name = context.get('site_name') or getattr( |
|
48
|
|
|
settings, 'SITE_NAME', '' |
|
49
|
|
|
) |
|
50
|
|
|
user = context.get('user') |
|
51
|
|
|
|
|
52
|
|
|
context.update({ |
|
53
|
|
|
'domain': domain, |
|
54
|
|
|
'protocol': protocol, |
|
55
|
|
|
'site_name': site_name, |
|
56
|
|
|
'user': user |
|
57
|
|
|
}) |
|
58
|
|
|
return context |
|
59
|
|
|
|
|
60
|
|
|
def render(self): |
|
|
|
|
|
|
61
|
|
|
context = make_context(self.get_context_data(), request=self.request) |
|
62
|
|
|
template = get_template(self.template_name) |
|
63
|
|
|
with context.bind_template(template.template): |
|
64
|
|
|
for node in template.template.nodelist: |
|
65
|
|
|
self._process_node(node, context) |
|
66
|
|
|
self._attach_body() |
|
67
|
|
|
|
|
68
|
|
|
def send(self, to, cc=None, bcc=None, *args, **kwargs): |
|
|
|
|
|
|
69
|
|
|
self.render() |
|
70
|
|
|
self.to = to |
|
|
|
|
|
|
71
|
|
|
if cc: |
|
72
|
|
|
self.cc = cc |
|
|
|
|
|
|
73
|
|
|
if bcc: |
|
74
|
|
|
self.bcc = bcc |
|
|
|
|
|
|
75
|
|
|
super(BaseEmailMessage, self).send(*args, **kwargs) |
|
76
|
|
|
|
|
77
|
|
|
def _process_node(self, node, context): |
|
78
|
|
|
attr = self._node_map.get(getattr(node, 'name', '')) |
|
79
|
|
|
if attr is not None: |
|
80
|
|
|
setattr(self, attr, node.render(context).strip()) |
|
81
|
|
|
|
|
82
|
|
|
def _attach_body(self): |
|
83
|
|
|
if self.body and self.html: |
|
|
|
|
|
|
84
|
|
|
self.attach_alternative(self.html, 'text/html') |
|
85
|
|
|
elif self.html: |
|
86
|
|
|
self.body = self.html |
|
|
|
|
|
|
87
|
|
|
self.content_subtype = 'html' |
|
|
|
|
|
|
88
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.