Skip to content

Commit

Permalink
context_processors: use a common context for emails.
Browse files Browse the repository at this point in the history
Fixes zulip#1611.
  • Loading branch information
paxapy authored and timabbott committed Nov 8, 2016
1 parent 8c7ed80 commit ff1e976
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 40 deletions.
12 changes: 11 additions & 1 deletion zerver/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@
from django.http import HttpRequest
from django.conf import settings

from zerver.models import get_realm_by_string_id
from zerver.models import UserProfile, get_realm_by_string_id
from zproject.backends import (password_auth_enabled, dev_auth_enabled,
google_auth_enabled, github_auth_enabled)
from zerver.lib.utils import get_subdomain


def common_context(user):
# type: (UserProfile) -> Dict[str, Any]
return {
'realm_uri': user.realm.uri,
'server_uri': settings.SERVER_URI,
'external_uri_scheme': settings.EXTERNAL_URI_SCHEME,
'external_host': settings.EXTERNAL_HOST,
}


def add_settings(request):
# type: (HttpRequest) -> Dict[str, Any]
realm = None
Expand Down
11 changes: 5 additions & 6 deletions zerver/lib/digest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
send_future_email, one_click_unsubscribe_link
from zerver.models import UserProfile, UserMessage, Recipient, Stream, \
Subscription, get_active_streams
from zerver.context_processors import common_context

import logging

Expand Down Expand Up @@ -166,15 +167,13 @@ def handle_digest_email(user_profile_id, cutoff):
user_profile=user_profile,
message__pub_date__gt=cutoff_date).order_by("message__pub_date")

template_payload = common_context(user_profile)

# Start building email template data.
template_payload = {
template_payload.update({
'name': user_profile.full_name,
'external_host': settings.EXTERNAL_HOST,
'external_uri_scheme': settings.EXTERNAL_URI_SCHEME,
'server_uri': settings.SERVER_URI,
'realm_uri': user_profile.realm.uri,
'unsubscribe_link': one_click_unsubscribe_link(user_profile, "digest")
} # type: Dict[str, Any]
})

# Gather recent missed PMs, re-using the missed PM email logic.
# You can't have an unread message that you sent, but when testing
Expand Down
23 changes: 10 additions & 13 deletions zerver/lib/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ def do_send_missedmessage_events_reply_in_zulip(user_profile, missed_messages, m
`missed_messages` is a list of Message objects to remind about they should
all have the same recipient and subject
"""
from zerver.context_processors import common_context
# Disabled missedmessage emails internally
if not user_profile.enable_offline_email_notifications:
return
Expand All @@ -247,19 +248,16 @@ def do_send_missedmessage_events_reply_in_zulip(user_profile, missed_messages, m
)

unsubscribe_link = one_click_unsubscribe_link(user_profile, "missed_messages")
template_payload = {
template_payload = common_context(user_profile)
template_payload.update({
'name': user_profile.full_name,
'messages': build_message_list(user_profile, missed_messages),
'message_count': message_count,
'reply_warning': False,
'external_host': settings.EXTERNAL_HOST,
'external_uri_scheme': settings.EXTERNAL_URI_SCHEME,
'server_uri': settings.SERVER_URI,
'realm_uri': user_profile.realm.uri,
'mention': missed_messages[0].recipient.type == Recipient.STREAM,
'reply_to_zulip': True,
'unsubscribe_link': unsubscribe_link,
}
})

headers = {}
from zerver.lib.email_mirror import create_missed_message_address
Expand Down Expand Up @@ -469,20 +467,19 @@ def send_local_email_template_with_delay(recipients, template_prefix,

def enqueue_welcome_emails(email, name):
# type: (text_type, text_type) -> None
from zerver.context_processors import common_context
if settings.WELCOME_EMAIL_SENDER is not None:
sender = settings.WELCOME_EMAIL_SENDER # type: Dict[str, text_type]
else:
sender = {'email': settings.ZULIP_ADMINISTRATOR, 'name': 'Zulip'}

user_profile = get_user_profile_by_email(email)
unsubscribe_link = one_click_unsubscribe_link(user_profile, "welcome")

template_payload = {'verbose_support_offers': settings.VERBOSE_SUPPORT_OFFERS,
'external_host': settings.EXTERNAL_HOST,
'external_uri_scheme': settings.EXTERNAL_URI_SCHEME,
'server_uri': settings.SERVER_URI,
'realm_uri': user_profile.realm.uri,
'unsubscribe_link': unsubscribe_link}
template_payload = common_context(user_profile)
template_payload.update({
'verbose_support_offers': settings.VERBOSE_SUPPORT_OFFERS,
'unsubscribe_link': unsubscribe_link
})

# Send day 1 email
send_local_email_template_with_delay([{'email': email, 'name': name}],
Expand Down
11 changes: 4 additions & 7 deletions zerver/views/unsubscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from zerver.lib.actions import do_change_enable_offline_email_notifications, \
do_change_enable_digest_emails, clear_followup_emails_queue
from zerver.models import UserProfile
from zerver.context_processors import common_context
from zproject.jinja2 import render_to_response

def process_unsubscribe(token, subscription_type, unsubscribe_function):
Expand All @@ -19,13 +20,9 @@ def process_unsubscribe(token, subscription_type, unsubscribe_function):

user_profile = confirmation.content_object
unsubscribe_function(user_profile)
return render_to_response('zerver/unsubscribe_success.html',
{"subscription_type": subscription_type,
"external_host": settings.EXTERNAL_HOST,
'external_uri_scheme': settings.EXTERNAL_URI_SCHEME,
'server_uri': settings.SERVER_URI,
'realm_uri': user_profile.realm.uri,
})
context = common_context(user_profile)
context.update({"subscription_type": subscription_type})
return render_to_response('zerver/unsubscribe_success.html', context)

# Email unsubscribe functions. All have the function signature
# processor(user_profile).
Expand Down
28 changes: 15 additions & 13 deletions zerver/worker/queue_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from zerver.lib.db import reset_queries
from django.core.mail import EmailMessage
from zerver.lib.redis_utils import get_redis_client
from zerver.context_processors import common_context

import os
import sys
Expand Down Expand Up @@ -152,19 +153,20 @@ def consume(self, data):

# queue invitation reminder for two days from now.
link = Confirmation.objects.get_link_for_object(invitee, host=referrer.realm.host)
send_local_email_template_with_delay([{'email': data["email"], 'name': ""}],
"zerver/emails/invitation/invitation_reminder_email",
{'activate_url': link,
'referrer': referrer,
'verbose_support_offers': settings.VERBOSE_SUPPORT_OFFERS,
'external_host': settings.EXTERNAL_HOST,
'external_uri_scheme': settings.EXTERNAL_URI_SCHEME,
'server_uri': settings.SERVER_URI,
'realm_uri': referrer.realm.uri,
'support_email': settings.ZULIP_ADMINISTRATOR},
datetime.timedelta(days=2),
tags=["invitation-reminders"],
sender={'email': settings.ZULIP_ADMINISTRATOR, 'name': 'Zulip'})
context = common_context(referrer)
context.update({
'activate_url': link,
'referrer': referrer,
'verbose_support_offers': settings.VERBOSE_SUPPORT_OFFERS,
'support_email': settings.ZULIP_ADMINISTRATOR
})
send_local_email_template_with_delay(
[{'email': data["email"], 'name': ""}],
"zerver/emails/invitation/invitation_reminder_email",
context,
datetime.timedelta(days=2),
tags=["invitation-reminders"],
sender={'email': settings.ZULIP_ADMINISTRATOR, 'name': 'Zulip'})

@assign_queue('user_activity')
class UserActivityWorker(QueueProcessingWorker):
Expand Down

0 comments on commit ff1e976

Please sign in to comment.