mirror of
https://github.com/zulip/zulip.git
synced 2025-11-05 06:23:38 +00:00
test_emails: Use zerver.lib.send_email for rendering email.
Previously the rendering code in test_emails.py did not match the rendering code in send_email.py. This commit removes the duplication to reduce the chance they drift in the future. This commit also changes test_emails.html to ensure that we always display both the HTML and text versions of an email.
This commit is contained in:
@@ -8,13 +8,10 @@
|
|||||||
<p>Email failed to render: {{ email.reason }}</p>
|
<p>Email failed to render: {{ email.reason }}</p>
|
||||||
{% else %}
|
{% else %}
|
||||||
<h4>Subject: {{email.subject}}</h4>
|
<h4>Subject: {{email.subject}}</h4>
|
||||||
{% if email.text %}
|
|
||||||
<pre>{{ email.body }}</pre>
|
|
||||||
{% else %}
|
|
||||||
{% autoescape off %}
|
{% autoescape off %}
|
||||||
{{ email.body }}
|
{{ email.html_message }}
|
||||||
{% endautoescape %}
|
{% endautoescape %}
|
||||||
{% endif %}
|
<pre>{{ email.body }}</pre>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -16,8 +16,9 @@ def display_email(user):
|
|||||||
# https://github.com/zulip/zulip/issues/4676 is resolved
|
# https://github.com/zulip/zulip/issues/4676 is resolved
|
||||||
return user.email
|
return user.email
|
||||||
|
|
||||||
def send_email(template_prefix, to_email, from_email=None, reply_to_email=None, context={}):
|
# Intended only for test code
|
||||||
# type: (str, Text, Optional[Text], Optional[Text], Dict[str, Any]) -> bool
|
def build_email(template_prefix, to_email, from_email=None, reply_to_email=None, context={}):
|
||||||
|
# type: (str, Text, Optional[Text], Optional[Text], Dict[str, Any]) -> EmailMultiAlternatives
|
||||||
subject = loader.render_to_string(template_prefix + '.subject',
|
subject = loader.render_to_string(template_prefix + '.subject',
|
||||||
context=context, using='Jinja2_plaintext').strip()
|
context=context, using='Jinja2_plaintext').strip()
|
||||||
message = loader.render_to_string(template_prefix + '.txt',
|
message = loader.render_to_string(template_prefix + '.txt',
|
||||||
@@ -36,6 +37,12 @@ def send_email(template_prefix, to_email, from_email=None, reply_to_email=None,
|
|||||||
mail = EmailMultiAlternatives(subject, message, from_email, [to_email], reply_to=reply_to)
|
mail = EmailMultiAlternatives(subject, message, from_email, [to_email], reply_to=reply_to)
|
||||||
if html_message is not None:
|
if html_message is not None:
|
||||||
mail.attach_alternative(html_message, 'text/html')
|
mail.attach_alternative(html_message, 'text/html')
|
||||||
|
return mail
|
||||||
|
|
||||||
|
def send_email(template_prefix, to_email, from_email=None, reply_to_email=None, context={}):
|
||||||
|
# type: (str, Text, Optional[Text], Optional[Text], Dict[str, Any]) -> bool
|
||||||
|
mail = build_email(template_prefix, to_email, from_email=from_email,
|
||||||
|
reply_to_email=reply_to_email, context=context)
|
||||||
return mail.send() > 0
|
return mail.send() > 0
|
||||||
|
|
||||||
def send_email_to_user(template_prefix, user, from_email=None, context={}):
|
def send_email_to_user(template_prefix, user, from_email=None, context={}):
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ from django.http import HttpRequest, HttpResponse
|
|||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.template import loader, TemplateDoesNotExist
|
from django.template import loader, TemplateDoesNotExist
|
||||||
|
|
||||||
|
from zerver.lib.send_email import build_email
|
||||||
from zerver.models import get_realm
|
from zerver.models import get_realm
|
||||||
|
|
||||||
import os
|
import os
|
||||||
@@ -74,28 +75,13 @@ def email_page(request):
|
|||||||
if template not in templates and template not in ignore:
|
if template not in templates and template not in ignore:
|
||||||
templates.append(template)
|
templates.append(template)
|
||||||
try:
|
try:
|
||||||
data.append(render_email(template, test_context))
|
email = build_email('zerver/emails/' + template, 'recipient@acme.com', context=test_context)
|
||||||
|
email_data = {
|
||||||
|
'template': template,
|
||||||
|
'subject': email.subject,
|
||||||
|
'body': email.body,
|
||||||
|
'html_message': email.alternatives[0][0] if len(email.alternatives) > 0 else 'Missing HTML message'}
|
||||||
|
data.append(email_data)
|
||||||
except Exception as e: # nocoverage
|
except Exception as e: # nocoverage
|
||||||
data.append({'template': template, 'failed': True, 'reason': e})
|
data.append({'template': template, 'failed': True, 'reason': e})
|
||||||
return render(request, 'zerver/test_emails.html', {'emails': data})
|
return render(request, 'zerver/test_emails.html', {'emails': data})
|
||||||
|
|
||||||
def render_email(template_prefix, context):
|
|
||||||
# type: (str, Dict[str, Any]) -> Dict[str, Any]
|
|
||||||
email = {} # type: Dict[str, Any]
|
|
||||||
email['template'] = template_prefix
|
|
||||||
email['subject'] = loader.render_to_string('zerver/emails/' + template_prefix + '.subject', context).strip()
|
|
||||||
message = loader.render_to_string('zerver/emails/' + template_prefix + '.txt', context)
|
|
||||||
|
|
||||||
try:
|
|
||||||
html_message = loader.render_to_string('/zerver/emails/' + template_prefix + '.html', context)
|
|
||||||
except TemplateDoesNotExist:
|
|
||||||
html_message = None
|
|
||||||
|
|
||||||
if html_message is not None:
|
|
||||||
email['body'] = html_message
|
|
||||||
email['text'] = False
|
|
||||||
else:
|
|
||||||
email['body'] = message
|
|
||||||
email['text'] = True
|
|
||||||
|
|
||||||
return email
|
|
||||||
|
|||||||
Reference in New Issue
Block a user