Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a default email template #3817

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
30 changes: 11 additions & 19 deletions esp/esp/program/modules/handlers/commmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from esp.tagdict.models import Tag
from django.template import Template
from django.template import Context as DjangoContext
from django.template.loader import render_to_string
from esp.middleware import ESPError

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

contextdict = {'user' : ActionHandler(firstuser, firstuser),
'program': ActionHandler(self.program, firstuser),
'request': ActionHandler(MessageRequest(), firstuser)}
'request': ActionHandler(MessageRequest(), firstuser),
'EMAIL_HOST_SENDER': settings.EMAIL_HOST_SENDER}

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

return render_to_response(self.baseDir()+'preview.html', request,
{'filterid': filterid,
Expand All @@ -138,7 +143,7 @@ def commprev(self, request, tl, one, two, module, extra, prog):
'replyto': replytoemail,
'public_view': public_view,
'body': body,
'renderedtext': renderedtext})
'rendered_text': rendered_text})

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

filterid, fromemail, replytoemail, subject, body = [
filterid, fromemail, replytoemail, subject, rendered_text = [
request.POST['filterid'],
request.POST['from'],
request.POST['replyto'],
request.POST['subject'],
request.POST['body'] ]
request.POST['rendered_text'] ]
sendto_fn_name = request.POST.get('sendto_fn_name', MessageRequest.SEND_TO_SELF_REAL)
public_view = 'public_view' in request.POST

Expand All @@ -198,26 +203,13 @@ def commfinal(self, request, tl, one, two, module, extra, prog):
sendto_fn_name = sendto_fn_name,
sender = fromemail,
creator = request.user,
msgtext = body,
msgtext = rendered_text,
public = public_view,
special_headers_dict
= { 'Reply-To': replytoemail, }, )

newmsg_request.save()

# now we're going to process everything
# nah, we'll do this later.
#newmsg_request.process()
# old code that prints out an estimated time
# numusers = self.approx_num_of_recipients(filterobj, sendto_fn)

# from django.conf import settings
# if hasattr(settings, 'EMAILTIMEOUT') and \
# settings.EMAILTIMEOUT is not None:
# est_time = settings.EMAILTIMEOUT * numusers
# else:
# est_time = 1.5 * numusers
# context = {'time': est_time}
context = {}
if public_view:
context['req_id'] = newmsg_request.id
Expand Down
23 changes: 19 additions & 4 deletions esp/esp/program/modules/tests/commpanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@
"""

from esp.program.tests import ProgramFrameworkTest
from esp.dbmail.models import MessageRequest
from esp.dbmail.models import ActionHandler, MessageRequest
from esp.dbmail.cronmail import process_messages, send_email_requests

from django.core import mail
from django.template import Context as DjangoContext
from django.template import Template
from django.template.loader import render_to_string
from django.utils.html import strip_tags

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

rendered_text = Template(render_to_string('email/default_email.html', {'msgbody': 'Test Body 123',})).render(
DjangoContext({'user': self.students[0], 'EMAIL_HOST_SENDER': 'testserver.learningu.org'}))
# Enter email information
post_data = {
'subject': 'Test Subject 123',
'body': 'Test Body 123',
'from': 'info@testserver.learningu.org',
'rendered_text': rendered_text,
'from': 'info@testserver.learningu.org',
'replyto': 'replyto@testserver.learningu.org',
'filterid': filterid,
}
Expand All @@ -113,10 +119,19 @@ def runTest(self):
# Check that the emails matched the entered information
msg = mail.outbox[0]
self.assertEqual(msg.subject, 'Test Subject 123')
self.assertEqual(msg.body, 'Test Body 123')
self.assertEqual(msg.from_email, 'info@testserver.learningu.org')
self.assertEqual(msg.extra_headers.get('Reply-To', ''), 'replyto@testserver.learningu.org')

# Check that the HTML-templated email renders correctly
context_dict = {'user' : ActionHandler(self.students[0], self.students[0]),
'program': ActionHandler(self.program, self.students[0]),
'request': ActionHandler(MessageRequest(), self.students[0]),
'EMAIL_HOST_SENDER': 'testserver.learningu.org'}
rendered_text = render_to_string('email/default_email.html', {'msgbody': 'Test Body 123',})
rendered_text = Template(rendered_text).render(DjangoContext(context_dict))
self.assertEqual(msg.body, strip_tags(rendered_text).strip())


# Check that the MessageRequest was marked as processed
m = MessageRequest.objects.filter(recipients__id=filterid, subject='Test Subject 123')
self.assertTrue(m.count() == 1)
Expand Down
14 changes: 14 additions & 0 deletions esp/templates/email/default_email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>

<head></head>

<body>
{% autoescape off %}{{msgbody|safe}}{% endautoescape %}
<br /><br />
<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 %}">
{% 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.
</small>
</body>

</html>
10 changes: 10 additions & 0 deletions esp/templates/email/minimal_email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>

<head></head>

<body>
{% autoescape off %}{{msgbody|safe}}{% endautoescape %}
</body>

</html>
2 changes: 1 addition & 1 deletion esp/templates/program/modules/commmodule/preview.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ <h2>Preview</h2>
<strong>Email body:</strong>
<br />
{% autoescape off %}
{{ renderedtext }}
{{ rendered_text }}
{% endautoescape %}
<br />
</p>
Expand Down
Loading