templates: Add new context variables to all templates.

This adds a few new helpful context variables that we can use to
compute URLs in all of our templates:
* external_uri_scheme: http(s)://
* server_uri: The base URL for the server's canonical name
* realm_uri: The base URL for the user's realm

This is preparatory work for making realm_uri != server_uri when we
add support for subdomains.
This commit is contained in:
Tim Abbott
2016-08-13 15:57:45 -07:00
parent 4a46b879ee
commit 4fbb8c3eee
7 changed files with 38 additions and 8 deletions

View File

@@ -9,7 +9,14 @@ from zproject.backends import (password_auth_enabled, dev_auth_enabled,
def add_settings(request):
# type: (HttpRequest) -> Dict[str, Any]
realm = request.user.realm if hasattr(request.user, "realm") else None
if hasattr(request.user, "realm"):
realm = request.user.realm
realm_uri = realm.uri
else:
realm = None
# TODO: Figure out how to add an assertion that this is not used
realm_uri = settings.SERVER_URI
return {
'custom_logo_url': settings.CUSTOM_LOGO_URL,
'register_link_disabled': settings.REGISTER_LINK_DISABLED,
@@ -20,7 +27,10 @@ def add_settings(request):
'only_sso': settings.ONLY_SSO,
'external_api_path': settings.EXTERNAL_API_PATH,
'external_api_uri': settings.EXTERNAL_API_URI,
'external_host': settings.EXTERNAL_HOST,
'external_uri_scheme': settings.EXTERNAL_URI_SCHEME,
'realm_uri': realm_uri,
'server_uri': settings.SERVER_URI,
'api_site_required': settings.EXTERNAL_API_PATH != "api.zulip.com",
'email_integration_enabled': settings.EMAIL_GATEWAY_BOT != "",
'email_gateway_example': settings.EMAIL_GATEWAY_EXAMPLE,

View File

@@ -170,6 +170,9 @@ def handle_digest_email(user_profile_id, cutoff):
template_payload = {
'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]

View File

@@ -43,9 +43,8 @@ def one_click_unsubscribe_link(user_profile, endpoint):
Zulip e-mails without having to first log in.
"""
token = unsubscribe_token(user_profile)
base_url = "https://" + settings.EXTERNAL_HOST
resource_path = "accounts/unsubscribe/%s/%s" % (endpoint, token)
return "%s/%s" % (base_url.rstrip("/"), resource_path)
return "%s/%s" % (settings.SERVER_URI.rstrip("/"), resource_path)
def hashchange_encode(string):
# type: (text_type) -> text_type
@@ -58,17 +57,17 @@ def hashchange_encode(string):
def pm_narrow_url(participants):
# type: (List[text_type]) -> text_type
participants.sort()
base_url = u"https://%s/#narrow/pm-with/" % (settings.EXTERNAL_HOST,)
base_url = u"%s/#narrow/pm-with/" % (settings.SERVER_URI,)
return base_url + hashchange_encode(",".join(participants))
def stream_narrow_url(stream):
# type: (text_type) -> text_type
base_url = u"https://%s/#narrow/stream/" % (settings.EXTERNAL_HOST,)
base_url = u"%s/#narrow/stream/" % (settings.SERVER_URI,)
return base_url + hashchange_encode(stream)
def topic_narrow_url(stream, topic):
# type: (text_type, text_type) -> text_type
base_url = u"https://%s/#narrow/stream/" % (settings.EXTERNAL_HOST,)
base_url = u"%s/#narrow/stream/" % (settings.SERVER_URI,)
return u"%s%s/topic/%s" % (base_url, hashchange_encode(stream),
hashchange_encode(topic))
@@ -249,9 +248,12 @@ def do_send_missedmessage_events_reply_in_zulip(user_profile, missed_messages, m
'name': user_profile.full_name,
'messages': build_message_list(user_profile, missed_messages),
'message_count': message_count,
'url': 'https://%s' % (settings.EXTERNAL_HOST,),
'url': settings.SERVER_URI,
'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,
}
@@ -471,6 +473,9 @@ def enqueue_welcome_emails(email, name):
template_payload = {'name': name,
'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}
# Send day 1 email

View File

@@ -190,6 +190,10 @@ class Realm(ModelReprMixin, models.Model):
# TODO: Change return type to QuerySet[UserProfile]
return UserProfile.objects.filter(realm=self, is_active=True).select_related()
@property
def uri(self):
return settings.SERVER_URI
@property
def is_zephyr_mirror_realm(self):
# type: () -> bool

View File

@@ -1228,7 +1228,11 @@ def process_unsubscribe(token, subscription_type, unsubscribe_function):
unsubscribe_function(user_profile)
return render_to_response('zerver/unsubscribe_success.html',
{"subscription_type": subscription_type,
"external_host": settings.EXTERNAL_HOST})
"external_host": settings.EXTERNAL_HOST,
'external_uri_scheme': settings.EXTERNAL_URI_SCHEME,
'server_uri': settings.SERVER_URI,
'realm_uri': user_profile.realm.uri,
})
# Email unsubscribe functions. All have the function signature
# processor(user_profile).

View File

@@ -158,6 +158,9 @@ class ConfirmationEmailWorker(QueueProcessingWorker):
'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"],

View File

@@ -469,6 +469,7 @@ if DEVELOPMENT:
if "EXTERNAL_API_PATH" not in vars():
EXTERNAL_API_PATH = EXTERNAL_HOST + "/api"
EXTERNAL_API_URI = EXTERNAL_URI_SCHEME + EXTERNAL_API_PATH
SERVER_URI = EXTERNAL_URI_SCHEME + EXTERNAL_HOST
S3_KEY = get_secret("s3_key")
S3_SECRET_KEY = get_secret("s3_secret_key")