email.py: Change recipients argument of send_future_email to to_email.

This commit is contained in:
Rishi Gupta
2017-05-03 18:23:33 -07:00
committed by Tim Abbott
parent 0d4c1b0467
commit 68c172192b
7 changed files with 21 additions and 32 deletions

View File

@@ -10,9 +10,9 @@ from django.db.models import Q, QuerySet
from django.template import loader
from django.conf import settings
from zerver.lib.send_email import send_future_email
from zerver.lib.notifications import build_message_list, hash_util_encode, \
one_click_unsubscribe_link
from zerver.lib.send_email import display_email, send_future_email
from zerver.models import UserProfile, UserMessage, Recipient, Stream, \
Subscription, get_active_streams
from zerver.context_processors import common_context
@@ -204,13 +204,11 @@ def handle_digest_email(user_profile_id, cutoff):
user_profile, cutoff_date)
template_payload["new_users"] = new_users
recipients = [{'email': user_profile.email, 'name': user_profile.full_name}]
# We don't want to send emails containing almost no information.
if enough_traffic(template_payload["unread_pms"],
template_payload["hot_conversations"],
new_streams_count, new_users_count):
logger.info("Sending digest email for %s" % (user_profile.email,))
# Send now, as a ScheduledJob
send_future_email('zerver/emails/digest', recipients,
send_future_email('zerver/emails/digest', display_email(user_profile),
context=template_payload, tags=["digest-emails"])

View File

@@ -397,11 +397,11 @@ def enqueue_welcome_emails(email, name):
'unsubscribe_link': unsubscribe_link
})
send_future_email(
"zerver/emails/followup_day1", [{'email': email, 'name': name}],
"zerver/emails/followup_day1", '%s <%s>' % (name, email),
from_email=from_email, context=context, delay=datetime.timedelta(hours=1),
tags=["followup-emails"])
send_future_email(
"zerver/emails/followup_day2", [{'email': email, 'name': name}],
"zerver/emails/followup_day2", '%s <%s>' % (name, email),
from_email=from_email, context=context, delay=datetime.timedelta(days=1),
tags=["followup-emails"])

View File

@@ -5,6 +5,7 @@ from django.utils.timezone import now as timezone_now
from zerver.models import UserProfile, ScheduledJob, get_user_profile_by_email
import datetime
from email.utils import parseaddr
import ujson
from typing import Any, Dict, Iterable, List, Optional, Text
@@ -32,22 +33,20 @@ def send_email_to_user(template_prefix, user, from_email=None, context={}):
# type: (str, UserProfile, Optional[Text], Dict[str, Text]) -> bool
return send_email(template_prefix, display_email(user), from_email=from_email, context=context)
def send_future_email(template_prefix, recipients, from_email=None, context={},
def send_future_email(template_prefix, to_email, from_email=None, context={},
delay=datetime.timedelta(0), tags=[]):
# type: (str, List[Dict[str, Any]], Optional[Text], Dict[str, Any], datetime.timedelta, Iterable[Text]) -> None
# type: (str, Text, Optional[Text], Dict[str, Any], datetime.timedelta, Iterable[Text]) -> None
subject = loader.render_to_string(template_prefix + '.subject', context).strip()
email_text = loader.render_to_string(template_prefix + '.txt', context)
email_html = loader.render_to_string(template_prefix + '.html', context)
if from_email is None:
from_email = settings.NOREPLY_EMAIL_ADDRESS
for recipient in recipients:
email_fields = {'email_html': email_html,
'email_subject': subject,
'email_text': email_text,
'recipient_email': recipient.get('email'),
'recipient_name': recipient.get('name'),
'from_email': from_email}
ScheduledJob.objects.create(type=ScheduledJob.EMAIL, filter_string=recipient.get('email'),
data=ujson.dumps(email_fields),
scheduled_timestamp=timezone_now() + delay)
email_fields = {'email_html': email_html,
'email_subject': subject,
'email_text': email_text,
'to_email': to_email,
'from_email': from_email}
ScheduledJob.objects.create(type=ScheduledJob.EMAIL, filter_string=parseaddr(to_email)[1],
data=ujson.dumps(email_fields),
scheduled_timestamp=timezone_now() + delay)

View File

@@ -37,20 +37,13 @@ logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(file_handler)
def get_recipient_as_string(dictionary):
# type: (Dict[str, str]) -> str
if not dictionary["recipient_name"]:
return dictionary["recipient_email"]
return format_html(u"\"{0}\" <{1}>", dictionary["recipient_name"], dictionary["recipient_email"])
def send_email_job(job):
# type: (ScheduledJob) -> bool
data = loads(job.data)
subject = data["email_subject"]
message = data["email_text"]
from_email = data["from_email"]
to_email = get_recipient_as_string(data)
to_email = data["to_email"]
if data["email_html"]:
html_message = data["email_html"]

View File

@@ -273,8 +273,7 @@ class TestDigestEmailMessages(ZulipTestCase):
handle_digest_email(user_profile.id, cutoff)
self.assertEqual(mock_send_future_email.call_count, 1)
self.assertEqual(mock_send_future_email.call_args[0][1][0]['email'],
u'othello@zulip.com')
self.assertEqual(mock_send_future_email.call_args[0][1], u'othello@zulip.com')
class TestReplyExtraction(ZulipTestCase):
def test_reply_is_extracted_from_plain(self):

View File

@@ -35,7 +35,7 @@ from zerver.lib.actions import (
get_stream,
do_create_realm,
)
from zerver.lib.send_email import send_future_email
from zerver.lib.send_email import display_email, send_future_email
from zerver.lib.initial_password import initial_password
from zerver.lib.actions import (
do_deactivate_realm,
@@ -738,7 +738,7 @@ so we didn't send them an invitation. We did send invitations to everyone else!"
})
with self.settings(EMAIL_BACKEND='django.core.mail.backends.console.EmailBackend'):
send_future_email(
"zerver/emails/invitation_reminder", [{'email': data["email"], 'name': ""}],
"zerver/emails/invitation_reminder", data["email"],
from_email=settings.ZULIP_ADMINISTRATOR,
context=context, tags=["invitation-reminders"])
email_jobs_to_deliver = ScheduledJob.objects.filter(
@@ -855,7 +855,7 @@ class EmailUnsubscribeTests(ZulipTestCase):
# Enqueue a fake digest email.
context = defaultdict(str) # type: Dict[str, Any]
context['new_streams'] = defaultdict(str)
send_future_email('zerver/emails/digest', [{'email': email, 'name': user_profile.full_name}],
send_future_email('zerver/emails/digest', display_email(user_profile),
context=context, tags=["digest-emails"])
self.assertEqual(1, len(ScheduledJob.objects.filter(

View File

@@ -166,7 +166,7 @@ class ConfirmationEmailWorker(QueueProcessingWorker):
})
send_future_email(
"zerver/emails/invitation_reminder",
[{'email': data["email"], 'name': ""}],
data["email"],
from_email=settings.ZULIP_ADMINISTRATOR,
context=context,
delay=datetime.timedelta(days=2),