Skip to content

Commit ae3462f

Browse files
Merge branch 'main' into main
2 parents 20dca4c + b92d500 commit ae3462f

File tree

5 files changed

+55
-24
lines changed

5 files changed

+55
-24
lines changed

esp/esp/program/modules/handlers/commmodule.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from esp.tagdict.models import Tag
4545
from django.template import Template
4646
from django.template import Context as DjangoContext
47+
from django.template.loader import render_to_string
4748
from esp.middleware import ESPError
4849

4950
import re
@@ -124,9 +125,13 @@ def commprev(self, request, tl, one, two, module, extra, prog):
124125

125126
contextdict = {'user' : ActionHandler(firstuser, firstuser),
126127
'program': ActionHandler(self.program, firstuser),
127-
'request': ActionHandler(MessageRequest(), firstuser)}
128+
'request': ActionHandler(MessageRequest(), firstuser),
129+
'EMAIL_HOST_SENDER': settings.EMAIL_HOST_SENDER}
128130

129-
renderedtext = Template(body).render(DjangoContext(contextdict))
131+
# Use whichever template the user selected or the default (just an unsubscribe slug) if 'None'
132+
rendered_text = render_to_string('email/{}_email.html'.format(request.POST.get('template', 'default')),
133+
{'msgbody': body})
134+
rendered_text = Template(rendered_text).render(DjangoContext(contextdict))
130135

131136
return render_to_response(self.baseDir()+'preview.html', request,
132137
{'filterid': filterid,
@@ -138,7 +143,7 @@ def commprev(self, request, tl, one, two, module, extra, prog):
138143
'replyto': replytoemail,
139144
'public_view': public_view,
140145
'body': body,
141-
'renderedtext': renderedtext})
146+
'rendered_text': rendered_text})
142147

143148
@staticmethod
144149
def approx_num_of_recipients(filterObj, sendto_fn):
@@ -170,12 +175,12 @@ def commfinal(self, request, tl, one, two, module, extra, prog):
170175
from esp.dbmail.models import MessageRequest
171176
from esp.users.models import PersistentQueryFilter
172177

173-
filterid, fromemail, replytoemail, subject, body = [
178+
filterid, fromemail, replytoemail, subject, rendered_text = [
174179
request.POST['filterid'],
175180
request.POST['from'],
176181
request.POST['replyto'],
177182
request.POST['subject'],
178-
request.POST['body'] ]
183+
request.POST['rendered_text'] ]
179184
sendto_fn_name = request.POST.get('sendto_fn_name', MessageRequest.SEND_TO_SELF_REAL)
180185
public_view = 'public_view' in request.POST
181186

@@ -198,26 +203,13 @@ def commfinal(self, request, tl, one, two, module, extra, prog):
198203
sendto_fn_name = sendto_fn_name,
199204
sender = fromemail,
200205
creator = request.user,
201-
msgtext = body,
206+
msgtext = rendered_text,
202207
public = public_view,
203208
special_headers_dict
204209
= { 'Reply-To': replytoemail, }, )
205210

206211
newmsg_request.save()
207212

208-
# now we're going to process everything
209-
# nah, we'll do this later.
210-
#newmsg_request.process()
211-
# old code that prints out an estimated time
212-
# numusers = self.approx_num_of_recipients(filterobj, sendto_fn)
213-
214-
# from django.conf import settings
215-
# if hasattr(settings, 'EMAILTIMEOUT') and \
216-
# settings.EMAILTIMEOUT is not None:
217-
# est_time = settings.EMAILTIMEOUT * numusers
218-
# else:
219-
# est_time = 1.5 * numusers
220-
# context = {'time': est_time}
221213
context = {}
222214
if public_view:
223215
context['req_id'] = newmsg_request.id

esp/esp/program/modules/tests/commpanel.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@
3434
"""
3535

3636
from esp.program.tests import ProgramFrameworkTest
37-
from esp.dbmail.models import MessageRequest
37+
from esp.dbmail.models import ActionHandler, MessageRequest
3838
from esp.dbmail.cronmail import process_messages, send_email_requests
3939

4040
from django.core import mail
41+
from django.template import Context as DjangoContext
42+
from django.template import Template
43+
from django.template.loader import render_to_string
44+
from django.utils.html import strip_tags
4145

4246
from datetime import datetime, timedelta
4347
import re
@@ -87,11 +91,13 @@ def runTest(self):
8791
s = re.search(r'<input type="hidden" name="listcount" value="([0-9]+)" />', response.content.decode('UTF-8'))
8892
listcount = s.groups()[0]
8993

94+
rendered_text = Template(render_to_string('email/default_email.html', {'msgbody': 'Test Body 123',})).render(
95+
DjangoContext({'user': self.students[0], 'EMAIL_HOST_SENDER': 'testserver.learningu.org'}))
9096
# Enter email information
9197
post_data = {
9298
'subject': 'Test Subject 123',
93-
'body': 'Test Body 123',
94-
'from': '[email protected]',
99+
'rendered_text': rendered_text,
100+
'from': '[email protected]',
95101
'replyto': '[email protected]',
96102
'filterid': filterid,
97103
}
@@ -113,10 +119,19 @@ def runTest(self):
113119
# Check that the emails matched the entered information
114120
msg = mail.outbox[0]
115121
self.assertEqual(msg.subject, 'Test Subject 123')
116-
self.assertEqual(msg.body, 'Test Body 123')
117122
self.assertEqual(msg.from_email, '[email protected]')
118123
self.assertEqual(msg.extra_headers.get('Reply-To', ''), '[email protected]')
119124

125+
# Check that the HTML-templated email renders correctly
126+
context_dict = {'user' : ActionHandler(self.students[0], self.students[0]),
127+
'program': ActionHandler(self.program, self.students[0]),
128+
'request': ActionHandler(MessageRequest(), self.students[0]),
129+
'EMAIL_HOST_SENDER': 'testserver.learningu.org'}
130+
rendered_text = render_to_string('email/default_email.html', {'msgbody': 'Test Body 123',})
131+
rendered_text = Template(rendered_text).render(DjangoContext(context_dict))
132+
self.assertEqual(msg.body, strip_tags(rendered_text).strip())
133+
134+
120135
# Check that the MessageRequest was marked as processed
121136
m = MessageRequest.objects.filter(recipients__id=filterid, subject='Test Subject 123')
122137
self.assertTrue(m.count() == 1)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head></head>
5+
6+
<body>
7+
{% autoescape off %}{{msgbody|safe}}{% endautoescape %}
8+
<br /><br />
9+
<small>You received this email because you have signed up for an account on our website, <a href="https://{% templatetag openvariable %}EMAIL_HOST_SENDER{% templatetag closevariable %}">
10+
{% templatetag openvariable %}EMAIL_HOST_SENDER{% templatetag closevariable %}</a>. This email was sent to the address associated with the user `{% templatetag openvariable %}user.username{% templatetag closevariable %}`. If you received the same email message multiple times, you may have duplicate accounts; please reply to this email and request to merge them. If you no longer wish for {% templatetag openvariable %}user.username{% templatetag closevariable %} to receive emails from us, <a href="{% templatetag openvariable %}user.unsubscribe_link{% templatetag closevariable %}">click here</a>. This action will unsubscribe you from receiving emails to {% templatetag openvariable %}user.username{% templatetag closevariable %}. You will still receive messages to this email address if it is listed in the contact information for other accounts on our site.
11+
</small>
12+
</body>
13+
14+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head></head>
5+
6+
<body>
7+
{% autoescape off %}{{msgbody|safe}}{% endautoescape %}
8+
</body>
9+
10+
</html>

esp/templates/program/modules/commmodule/preview.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ <h2>Preview</h2>
6161
<strong>Email body:</strong>
6262
<br />
6363
{% autoescape off %}
64-
{{ renderedtext }}
64+
{{ rendered_text }}
6565
{% endautoescape %}
6666
<br />
6767
</p>

0 commit comments

Comments
 (0)