mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +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>
 | 
			
		||||
        {% else %}
 | 
			
		||||
            <h4>Subject: {{email.subject}}</h4>
 | 
			
		||||
            {% if email.text %}
 | 
			
		||||
                <pre>{{ email.body }}</pre>
 | 
			
		||||
            {% else %}
 | 
			
		||||
                {% autoescape off %}
 | 
			
		||||
                {{ email.body }}
 | 
			
		||||
                {% endautoescape %}
 | 
			
		||||
            {% endif %}
 | 
			
		||||
            {% autoescape off %}
 | 
			
		||||
            {{ email.html_message }}
 | 
			
		||||
            {% endautoescape %}
 | 
			
		||||
            <pre>{{ email.body }}</pre>
 | 
			
		||||
        {% endif %}
 | 
			
		||||
    {% endfor %}
 | 
			
		||||
</div>
 | 
			
		||||
 
 | 
			
		||||
@@ -16,8 +16,9 @@ def display_email(user):
 | 
			
		||||
    # https://github.com/zulip/zulip/issues/4676 is resolved
 | 
			
		||||
    return user.email
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
# Intended only for test code
 | 
			
		||||
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',
 | 
			
		||||
                                      context=context, using='Jinja2_plaintext').strip()
 | 
			
		||||
    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)
 | 
			
		||||
    if html_message is not None:
 | 
			
		||||
        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
 | 
			
		||||
 | 
			
		||||
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.template import loader, TemplateDoesNotExist
 | 
			
		||||
 | 
			
		||||
from zerver.lib.send_email import build_email
 | 
			
		||||
from zerver.models import get_realm
 | 
			
		||||
 | 
			
		||||
import os
 | 
			
		||||
@@ -74,28 +75,13 @@ def email_page(request):
 | 
			
		||||
        if template not in templates and template not in ignore:
 | 
			
		||||
            templates.append(template)
 | 
			
		||||
            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
 | 
			
		||||
                data.append({'template': template, 'failed': True, 'reason': e})
 | 
			
		||||
    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