forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
send_email: Add ScheduledEmail support for multiple recipients.
Follow up on 92dc363. This modifies the ScheduledEmail model and send_future_email to properly support multiple recipients. Tweaked by tabbott to add some useful explanatory comments and fix issues with the migration.
- Loading branch information
Showing
10 changed files
with
119 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
zerver/migrations/0211_add_users_field_to_scheduled_email.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.20 on 2019-03-14 01:11 | ||
from __future__ import unicode_literals | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor | ||
from django.db.migrations.state import StateApps | ||
|
||
def set_users_for_existing_scheduledemails( | ||
apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None: | ||
ScheduledEmail = apps.get_model("zerver", "ScheduledEmail") | ||
for email in ScheduledEmail.objects.all(): | ||
if email.user is not None: | ||
email.users.add(email.user) | ||
email.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('zerver', '0210_stream_first_message_id'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='scheduledemail', | ||
name='users', | ||
field=models.ManyToManyField(to=settings.AUTH_USER_MODEL), | ||
), | ||
migrations.RunPython(set_users_for_existing_scheduledemails, reverse_code=migrations.RunPython.noop), | ||
migrations.RemoveField( | ||
model_name='scheduledemail', | ||
name='user', | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ class TestFollowupEmails(ZulipTestCase): | |
def test_day1_email_context(self) -> None: | ||
hamlet = self.example_user("hamlet") | ||
enqueue_welcome_emails(hamlet) | ||
scheduled_emails = ScheduledEmail.objects.filter(user=hamlet) | ||
scheduled_emails = ScheduledEmail.objects.filter(users=hamlet) | ||
email_data = ujson.loads(scheduled_emails[0].data) | ||
self.assertEqual(email_data["context"]["email"], self.example_email("hamlet")) | ||
self.assertEqual(email_data["context"]["is_realm_admin"], False) | ||
|
@@ -39,7 +39,7 @@ def test_day1_email_context(self) -> None: | |
|
||
iago = self.example_user("iago") | ||
enqueue_welcome_emails(iago) | ||
scheduled_emails = ScheduledEmail.objects.filter(user=iago) | ||
scheduled_emails = ScheduledEmail.objects.filter(users=iago) | ||
email_data = ujson.loads(scheduled_emails[0].data) | ||
self.assertEqual(email_data["context"]["email"], self.example_email("iago")) | ||
self.assertEqual(email_data["context"]["is_realm_admin"], True) | ||
|
@@ -74,7 +74,7 @@ def test_day1_email_ldap_case_a_login_credentials(self) -> None: | |
AUTH_LDAP_USER_SEARCH=ldap_search): | ||
self.login_with_return("[email protected]", "testing") | ||
user = UserProfile.objects.get(email="[email protected]") | ||
scheduled_emails = ScheduledEmail.objects.filter(user=user) | ||
scheduled_emails = ScheduledEmail.objects.filter(users=user) | ||
|
||
self.assertEqual(len(scheduled_emails), 2) | ||
email_data = ujson.loads(scheduled_emails[0].data) | ||
|
@@ -107,7 +107,7 @@ def test_day1_email_ldap_case_b_login_credentials(self) -> None: | |
self.login_with_return("[email protected]", "testing") | ||
|
||
user = UserProfile.objects.get(email="[email protected]") | ||
scheduled_emails = ScheduledEmail.objects.filter(user=user) | ||
scheduled_emails = ScheduledEmail.objects.filter(users=user) | ||
|
||
self.assertEqual(len(scheduled_emails), 2) | ||
email_data = ujson.loads(scheduled_emails[0].data) | ||
|
@@ -139,7 +139,7 @@ def test_day1_email_ldap_case_c_login_credentials(self) -> None: | |
AUTH_LDAP_USER_DN_TEMPLATE='uid=%(user)s,ou=users,dc=zulip,dc=com'): | ||
self.login_with_return("newuser", "testing") | ||
user = UserProfile.objects.get(email="[email protected]") | ||
scheduled_emails = ScheduledEmail.objects.filter(user=user) | ||
scheduled_emails = ScheduledEmail.objects.filter(users=user) | ||
|
||
self.assertEqual(len(scheduled_emails), 2) | ||
email_data = ujson.loads(scheduled_emails[0].data) | ||
|
@@ -152,15 +152,15 @@ def test_followup_emails_count(self) -> None: | |
|
||
enqueue_welcome_emails(self.example_user("hamlet")) | ||
# Hamlet has account only in Zulip realm so both day1 and day2 emails should be sent | ||
scheduled_emails = ScheduledEmail.objects.filter(user=hamlet) | ||
scheduled_emails = ScheduledEmail.objects.filter(users=hamlet) | ||
self.assertEqual(2, len(scheduled_emails)) | ||
self.assertEqual(ujson.loads(scheduled_emails[0].data)["template_prefix"], 'zerver/emails/followup_day2') | ||
self.assertEqual(ujson.loads(scheduled_emails[1].data)["template_prefix"], 'zerver/emails/followup_day1') | ||
self.assertEqual(ujson.loads(scheduled_emails[1].data)["template_prefix"], 'zerver/emails/followup_day2') | ||
self.assertEqual(ujson.loads(scheduled_emails[0].data)["template_prefix"], 'zerver/emails/followup_day1') | ||
|
||
ScheduledEmail.objects.all().delete() | ||
|
||
enqueue_welcome_emails(cordelia) | ||
scheduled_emails = ScheduledEmail.objects.filter(user=cordelia) | ||
scheduled_emails = ScheduledEmail.objects.filter(users=cordelia) | ||
# Cordelia has account in more than 1 realm so day2 email should not be sent | ||
self.assertEqual(len(scheduled_emails), 1) | ||
email_data = ujson.loads(scheduled_emails[0].data) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters