-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
110 lines (91 loc) · 4.65 KB
/
views.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
# -*- coding: utf-8 -*-
import os
import tempfile
import shutil
from django.template.loader import render_to_string
from django.views.generic import TemplateView
from django.template import TemplateDoesNotExist
from django.conf import settings
from basic_email.send import send_email
class FakeEmailSend(TemplateView):
# template_name = 'emails/fake.html'
template_name = 'test.html'
def get_context_data(self, *args, **kwargs):
context = super(FakeEmailSend, self).get_context_data(*args, **kwargs)
context['template'] = self.request.GET.get('template')
context['email'] = getattr(settings, 'FAKE_EMAIL_TO', None)
if context['template'] and context['email']:
try:
send_email(context['template'], context['email'], 'Fake email!')
except TemplateDoesNotExist:
context['template_does_not_exist'] = True
return context
class ListEmailTemplatesView(TemplateView):
template_name = os.path.join(os.path.dirname(__file__), 'templates/admin/list.html')
def get_context_data(self, *args, **kwargs):
context = super(ListEmailTemplatesView, self).get_context_data(*args, **kwargs)
templates_dirs = [os.path.join(d, settings.BASIC_EMAIL_DIRECTORY) for d in settings.TEMPLATE_DIRS]
fileList = []
for rootdir in templates_dirs:
for root, subFolders, files in os.walk(rootdir):
for file in files:
if file.startswith('email_') and file.endswith('.html'):
fileList.append(file)
context['files'] = fileList
return context
class ListEmailVariables(TemplateView):
sub_tmp_dir = 'emails_tmp'
def get_template(self, *args, **kwargs):
if not self.request.GET.get('template'):
raise NameError("?template=... is required")
return os.path.join(settings.BASIC_EMAIL_DIRECTORY, self.request.GET.get('template'))
def get_template_names(self, *args, **kwargs):
return [self.get_template()]
def content_encode(self, content):
content = content.replace('{{', '{##_')
to_replace = "{{% extends '{dir}/".format(dir=settings.BASIC_EMAIL_DIRECTORY)
replacer = "{{#_extends '{dir}/".format(dir=self.sub_tmp_dir)
content = content.replace(to_replace, replacer)
to_replace = '{{% extends "{dir}'.format(dir=settings.BASIC_EMAIL_DIRECTORY)
replacer = '{{#_extends "{dir}'.format(dir=self.sub_tmp_dir)
content = content.replace(to_replace, replacer)
content = content.replace('{% block ', '{#_block ')
content = content.replace('{% endblock ', '{#_endblock ')
content = content.replace('{%', '{#_')
content = content.replace('{{', '{##_')
content = content.replace('{#_extends', '{% extends')
content = content.replace('{#_block ', '{% block ')
content = content.replace('{#_endblock ', '{% endblock ')
return content
def prepare_tmp_files(self, tmp_dir):
for d in settings.TEMPLATE_DIRS:
d = os.path.join(d, settings.BASIC_EMAIL_DIRECTORY)
if os.path.exists(d):
for file_name in os.listdir(d):
if not os.path.isdir(d + file_name):
f = open(os.path.join(d, file_name), 'r')
content = f.read()
content = self.content_encode(content)
path = os.path.join(tmp_dir, self.sub_tmp_dir, file_name)
dir_name = os.path.dirname(path)
if not os.path.exists(dir_name):
os.makedirs(dir_name)
tmp_f = open(path, 'w+')
tmp_f.write(content)
tmp_f.close()
def list_template_variables(self):
# the only way is to parse everything and load data for one. Just like Remi did in
# https://bitbucket.org/arabellatech/case.ify/src/56491706a59f33929f76279f248bae38a43f400b/caseify/emails/management/commands/generate_emails.py?at=develop
# bad
tmp_dir = tempfile.mkdtemp()
settings.TEMPLATE_DIRS = settings.TEMPLATE_DIRS + (tmp_dir,)
self.prepare_tmp_files(tmp_dir)
file_name = self.get_template().replace(settings.BASIC_EMAIL_DIRECTORY, self.sub_tmp_dir)
content = render_to_string(file_name)
print (content)
#shutil.rmtree(tmp_dir) # careful! removes whole tree.
class PreviewEmailView(ListEmailVariables):
def get_context_data(self, *args, **kwargs):
context = super(PreviewEmailView, self).get_context_data(*args, **kwargs)
self.list_template_variables()
return context