1
|
|
|
from django.contrib.sites.shortcuts import get_current_site |
|
|
|
|
2
|
|
|
from django.core import mail |
|
|
|
|
3
|
|
|
from django.template.context import make_context |
|
|
|
|
4
|
|
|
from django.template.loader import get_template |
|
|
|
|
5
|
|
|
|
6
|
|
|
from django.conf import settings |
|
|
|
|
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class BaseEmailMessage(mail.EmailMultiAlternatives): |
|
|
|
|
10
|
|
|
_node_map = { |
11
|
|
|
'subject': 'subject', |
12
|
|
|
'text_body': 'body', |
13
|
|
|
'html_body': 'html', |
14
|
|
|
} |
15
|
|
|
template_name = None |
16
|
|
|
|
17
|
|
|
def __init__(self, request, context=None, *args, **kwargs): |
18
|
|
|
super(BaseEmailMessage, self).__init__(*args, **kwargs) |
19
|
|
|
|
20
|
|
|
self.request = request |
21
|
|
|
self.context = {} if context is None else context |
22
|
|
|
self.html = None |
23
|
|
|
|
24
|
|
|
if request is not None: |
25
|
|
|
self.set_context_data() |
26
|
|
|
self.render() |
27
|
|
|
|
28
|
|
|
def set_context_data(self): |
|
|
|
|
29
|
|
|
site = get_current_site(self.request) |
30
|
|
|
self.context.update({ |
31
|
|
|
'domain': getattr(settings, 'DOMAIN', '') or site.domain, |
32
|
|
|
'protocol': self.context.get('protocol') or ( |
33
|
|
|
'https' if self.request.is_secure() else 'http'), |
34
|
|
|
'site_name': getattr(settings, 'SITE_NAME', '') or site.name, |
35
|
|
|
'user': self.context.get('user') or self.request.user, |
36
|
|
|
}) |
37
|
|
|
|
38
|
|
|
def render(self): |
|
|
|
|
39
|
|
|
context = make_context(self.context, request=self.request) |
40
|
|
|
template = get_template(self.template_name) |
41
|
|
|
with context.bind_template(template.template): |
42
|
|
|
for node in template.template.nodelist: |
43
|
|
|
self._process_node(node, context) |
44
|
|
|
self._attach_body() |
45
|
|
|
|
46
|
|
|
def send(self, to, cc=None, bcc=None, *args, **kwargs): |
|
|
|
|
47
|
|
|
self.to = to |
|
|
|
|
48
|
|
|
if cc: |
49
|
|
|
self.cc = cc |
|
|
|
|
50
|
|
|
if bcc: |
51
|
|
|
self.bcc = bcc |
|
|
|
|
52
|
|
|
super(BaseEmailMessage, self).send(*args, **kwargs) |
53
|
|
|
|
54
|
|
|
def _process_node(self, node, context): |
55
|
|
|
attr = self._node_map.get(getattr(node, 'name', '')) |
56
|
|
|
if attr is not None: |
57
|
|
|
setattr(self, attr, node.render(context).strip()) |
58
|
|
|
|
59
|
|
|
def _attach_body(self): |
60
|
|
|
if self.body and self.html: |
|
|
|
|
61
|
|
|
self.attach_alternative(self.html, 'text/html') |
62
|
|
|
elif self.html: |
63
|
|
|
self.body = self.html |
|
|
|
|
64
|
|
|
self.content_subtype = 'html' |
|
|
|
|
65
|
|
|
|
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.