2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2017-05-04 01:12:34 +02:00
|
|
|
from typing import Any, Dict, Optional
|
2016-08-08 11:27:28 +02:00
|
|
|
from django.http import HttpRequest
|
2012-10-15 22:52:08 +02:00
|
|
|
from django.conf import settings
|
2016-11-08 10:00:31 +01:00
|
|
|
|
2017-06-02 23:56:08 +02:00
|
|
|
from zerver.models import UserProfile, get_realm, get_unique_non_system_realm, Realm
|
2017-04-20 21:02:56 +02:00
|
|
|
from zproject.backends import (
|
|
|
|
any_oauth_backend_enabled,
|
|
|
|
dev_auth_enabled,
|
|
|
|
github_auth_enabled,
|
|
|
|
google_auth_enabled,
|
|
|
|
password_auth_enabled,
|
2017-04-27 06:46:43 +02:00
|
|
|
auth_enabled_helper,
|
|
|
|
AUTH_BACKEND_NAME_MAP
|
2017-04-20 21:02:56 +02:00
|
|
|
)
|
2017-05-11 22:52:14 +02:00
|
|
|
from zerver.lib.bugdown import convert
|
2017-07-02 05:27:01 +02:00
|
|
|
from zerver.lib.send_email import FromAddress
|
2016-11-02 21:41:10 +01:00
|
|
|
from zerver.lib.utils import get_subdomain
|
2017-04-18 07:10:41 +02:00
|
|
|
from zerver.lib.realm_icon import get_realm_icon_url
|
|
|
|
|
2017-03-09 08:40:03 +01:00
|
|
|
from version import ZULIP_VERSION
|
2016-11-08 10:00:31 +01:00
|
|
|
|
2016-11-08 10:07:47 +01:00
|
|
|
def common_context(user):
|
|
|
|
# type: (UserProfile) -> Dict[str, Any]
|
2017-05-02 00:45:12 +02:00
|
|
|
"""Common context used for things like outgoing emails that don't
|
|
|
|
have a request.
|
|
|
|
"""
|
2016-11-08 10:07:47 +01:00
|
|
|
return {
|
|
|
|
'realm_uri': user.realm.uri,
|
|
|
|
'server_uri': settings.SERVER_URI,
|
|
|
|
'external_uri_scheme': settings.EXTERNAL_URI_SCHEME,
|
|
|
|
'external_host': settings.EXTERNAL_HOST,
|
|
|
|
}
|
|
|
|
|
2017-05-04 01:12:34 +02:00
|
|
|
def get_realm_from_request(request):
|
|
|
|
# type: (HttpRequest) -> Optional[Realm]
|
|
|
|
if hasattr(request, "user") and hasattr(request.user, "realm"):
|
|
|
|
return request.user.realm
|
|
|
|
elif settings.REALMS_HAVE_SUBDOMAINS:
|
|
|
|
subdomain = get_subdomain(request)
|
|
|
|
return get_realm(subdomain)
|
|
|
|
# This will return None if there is no unique, open realm.
|
2017-06-02 23:56:08 +02:00
|
|
|
return get_unique_non_system_realm()
|
2017-05-04 01:12:34 +02:00
|
|
|
|
2017-05-02 00:45:12 +02:00
|
|
|
def zulip_default_context(request):
|
2016-08-08 11:27:28 +02:00
|
|
|
# type: (HttpRequest) -> Dict[str, Any]
|
2017-05-02 00:45:12 +02:00
|
|
|
"""Context available to all Zulip Jinja2 templates that have a request
|
|
|
|
passed in. Designed to provide the long list of variables at the
|
|
|
|
bottom of this function in a wide range of situations: logged-in
|
|
|
|
or logged-out, subdomains or not, etc.
|
|
|
|
|
|
|
|
The main variable in the below is whether we know the realm, which
|
|
|
|
is the case if there is only one realm, or we're on a
|
|
|
|
REALMS_HAVE_SUBDOMAINS subdomain, or the user is logged in.
|
|
|
|
"""
|
2017-05-04 01:12:34 +02:00
|
|
|
realm = get_realm_from_request(request)
|
2016-11-08 10:00:31 +01:00
|
|
|
|
|
|
|
if realm is not None:
|
2016-08-14 00:57:45 +02:00
|
|
|
realm_uri = realm.uri
|
2017-04-18 07:10:41 +02:00
|
|
|
realm_name = realm.name
|
|
|
|
realm_icon = get_realm_icon_url(realm)
|
2017-05-11 23:23:41 +02:00
|
|
|
realm_description_raw = realm.description or "The coolest place in the universe."
|
|
|
|
realm_description = convert(realm_description_raw, message_realm=realm)
|
2016-08-14 00:57:45 +02:00
|
|
|
else:
|
|
|
|
realm_uri = settings.SERVER_URI
|
2017-04-18 07:10:41 +02:00
|
|
|
realm_name = None
|
|
|
|
realm_icon = None
|
|
|
|
realm_description = None
|
2016-08-14 00:57:45 +02:00
|
|
|
|
2016-09-14 08:00:27 +02:00
|
|
|
register_link_disabled = settings.REGISTER_LINK_DISABLED
|
|
|
|
login_link_disabled = settings.LOGIN_LINK_DISABLED
|
|
|
|
about_link_disabled = settings.ABOUT_LINK_DISABLED
|
|
|
|
find_team_link_disabled = settings.FIND_TEAM_LINK_DISABLED
|
|
|
|
if settings.SUBDOMAINS_HOMEPAGE and get_subdomain(request) == "":
|
|
|
|
register_link_disabled = True
|
|
|
|
login_link_disabled = True
|
|
|
|
about_link_disabled = True
|
|
|
|
find_team_link_disabled = False
|
|
|
|
|
2017-06-06 02:06:52 +02:00
|
|
|
apps_page_url = 'https://zulipchat.com/apps/'
|
|
|
|
if settings.ZILENCER_ENABLED:
|
|
|
|
apps_page_url = '/apps/'
|
|
|
|
|
2012-10-17 20:34:38 +02:00
|
|
|
return {
|
2017-04-18 07:10:41 +02:00
|
|
|
'realms_have_subdomains': settings.REALMS_HAVE_SUBDOMAINS,
|
2017-01-24 06:21:14 +01:00
|
|
|
'custom_logo_url': settings.CUSTOM_LOGO_URL,
|
2016-09-14 08:00:27 +02:00
|
|
|
'register_link_disabled': register_link_disabled,
|
|
|
|
'login_link_disabled': login_link_disabled,
|
|
|
|
'about_link_disabled': about_link_disabled,
|
2017-01-24 06:21:14 +01:00
|
|
|
'terms_of_service': settings.TERMS_OF_SERVICE,
|
2017-04-10 12:55:43 +02:00
|
|
|
'privacy_policy': settings.PRIVACY_POLICY,
|
2017-01-24 06:21:14 +01:00
|
|
|
'login_url': settings.HOME_NOT_LOGGED_IN,
|
|
|
|
'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,
|
2017-04-18 07:10:41 +02:00
|
|
|
'realm_name': realm_name,
|
|
|
|
'realm_icon': realm_icon,
|
|
|
|
'realm_description': realm_description,
|
2017-01-24 06:21:14 +01:00
|
|
|
'server_uri': settings.SERVER_URI,
|
|
|
|
'api_site_required': settings.EXTERNAL_API_PATH != "api.zulip.com",
|
|
|
|
'email_gateway_example': settings.EMAIL_GATEWAY_EXAMPLE,
|
2017-06-06 02:06:52 +02:00
|
|
|
'apps_page_url': apps_page_url,
|
2017-01-24 06:21:14 +01:00
|
|
|
'open_realm_creation': settings.OPEN_REALM_CREATION,
|
|
|
|
'password_auth_enabled': password_auth_enabled(realm),
|
|
|
|
'dev_auth_enabled': dev_auth_enabled(realm),
|
|
|
|
'google_auth_enabled': google_auth_enabled(realm),
|
|
|
|
'github_auth_enabled': github_auth_enabled(realm),
|
2017-04-20 21:02:56 +02:00
|
|
|
'any_oauth_backend_enabled': any_oauth_backend_enabled(realm),
|
2017-04-27 06:46:43 +02:00
|
|
|
'no_auth_enabled': not auth_enabled_helper(list(AUTH_BACKEND_NAME_MAP.keys()), realm),
|
2017-01-24 06:21:14 +01:00
|
|
|
'development_environment': settings.DEVELOPMENT,
|
2017-07-02 05:27:01 +02:00
|
|
|
'support_email': FromAddress.SUPPORT,
|
2016-09-14 08:00:27 +02:00
|
|
|
'find_team_link_disabled': find_team_link_disabled,
|
2017-01-24 06:21:14 +01:00
|
|
|
'password_min_length': settings.PASSWORD_MIN_LENGTH,
|
|
|
|
'password_min_quality': settings.PASSWORD_MIN_ZXCVBN_QUALITY,
|
2017-03-09 08:40:03 +01:00
|
|
|
'zulip_version': ZULIP_VERSION,
|
2012-10-17 20:34:38 +02:00
|
|
|
}
|
2013-06-17 18:01:22 +02:00
|
|
|
|
2016-11-08 10:00:31 +01:00
|
|
|
|
2013-06-17 18:01:22 +02:00
|
|
|
def add_metrics(request):
|
2016-08-08 11:27:28 +02:00
|
|
|
# type: (HttpRequest) -> Dict[str, str]
|
2013-06-17 18:01:22 +02:00
|
|
|
return {
|
2013-10-29 22:21:17 +01:00
|
|
|
'dropboxAppKey': settings.DROPBOX_APP_KEY
|
2013-06-17 18:01:22 +02:00
|
|
|
}
|