2020-06-11 00:54:34 +02:00
|
|
|
from typing import Any, Dict, Optional
|
2019-05-16 14:07:12 +02:00
|
|
|
from urllib.parse import urljoin
|
|
|
|
|
2012-10-15 22:52:08 +02:00
|
|
|
from django.conf import settings
|
2021-04-24 01:32:32 +02:00
|
|
|
from django.contrib.staticfiles.storage import staticfiles_storage
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.http import HttpRequest
|
2020-09-16 19:30:05 +02:00
|
|
|
from django.utils.html import escape
|
|
|
|
from django.utils.safestring import SafeString
|
2021-04-24 01:32:32 +02:00
|
|
|
from django.utils.translation import get_language
|
2016-11-08 10:00:31 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from version import (
|
|
|
|
LATEST_MAJOR_VERSION,
|
|
|
|
LATEST_RELEASE_ANNOUNCEMENT,
|
|
|
|
LATEST_RELEASE_VERSION,
|
|
|
|
ZULIP_VERSION,
|
|
|
|
)
|
2020-09-01 13:56:15 +02:00
|
|
|
from zerver.lib.exceptions import InvalidSubdomainError
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.realm_description import get_realm_rendered_description, get_realm_text_description
|
|
|
|
from zerver.lib.realm_icon import get_realm_icon_url
|
|
|
|
from zerver.lib.send_email import FromAddress
|
|
|
|
from zerver.lib.subdomains import get_subdomain
|
|
|
|
from zerver.models import Realm, UserProfile, get_realm
|
2017-04-20 21:02:56 +02:00
|
|
|
from zproject.backends import (
|
2020-06-11 00:54:34 +02:00
|
|
|
AUTH_BACKEND_NAME_MAP,
|
2019-09-19 03:19:45 +02:00
|
|
|
any_social_backend_enabled,
|
2020-06-11 00:54:34 +02:00
|
|
|
auth_enabled_helper,
|
2019-12-08 23:11:25 +01:00
|
|
|
get_external_method_dicts,
|
2017-04-20 21:02:56 +02:00
|
|
|
password_auth_enabled,
|
2017-09-10 20:42:07 +02:00
|
|
|
require_email_format_usernames,
|
2017-04-20 21:02:56 +02:00
|
|
|
)
|
2017-04-18 07:10:41 +02:00
|
|
|
|
2021-04-24 01:32:32 +02:00
|
|
|
DEFAULT_PAGE_PARAMS = {
|
2021-05-20 20:14:17 +02:00
|
|
|
"development_environment": settings.DEVELOPMENT,
|
2021-04-24 01:32:32 +02:00
|
|
|
"webpack_public_path": staticfiles_storage.url(settings.WEBPACK_BUNDLES),
|
|
|
|
}
|
|
|
|
|
2016-11-08 10:00:31 +01:00
|
|
|
|
2017-11-27 07:33:05 +01:00
|
|
|
def common_context(user: 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 {
|
2021-02-12 08:20:45 +01:00
|
|
|
"realm_uri": user.realm.uri,
|
|
|
|
"realm_name": user.realm.name,
|
|
|
|
"root_domain_uri": settings.ROOT_DOMAIN_URI,
|
|
|
|
"external_uri_scheme": settings.EXTERNAL_URI_SCHEME,
|
|
|
|
"external_host": settings.EXTERNAL_HOST,
|
|
|
|
"user_name": user.full_name,
|
2016-11-08 10:07:47 +01:00
|
|
|
}
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-05-04 04:47:44 +02:00
|
|
|
def get_realm_from_request(request: HttpRequest) -> Optional[Realm]:
|
2017-05-04 01:12:34 +02:00
|
|
|
if hasattr(request, "user") and hasattr(request.user, "realm"):
|
|
|
|
return request.user.realm
|
2019-03-17 22:08:53 +01:00
|
|
|
if not hasattr(request, "realm"):
|
|
|
|
# We cache the realm object from this function on the request,
|
|
|
|
# so that functions that call get_realm_from_request don't
|
|
|
|
# need to do duplicate queries on the same realm while
|
|
|
|
# processing a single request.
|
|
|
|
subdomain = get_subdomain(request)
|
2019-05-04 04:47:44 +02:00
|
|
|
try:
|
|
|
|
request.realm = get_realm(subdomain)
|
|
|
|
except Realm.DoesNotExist:
|
|
|
|
request.realm = None
|
2019-03-17 22:08:53 +01:00
|
|
|
return request.realm
|
2017-05-04 01:12:34 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-09-01 13:56:15 +02:00
|
|
|
def get_valid_realm_from_request(request: HttpRequest) -> Realm:
|
|
|
|
realm = get_realm_from_request(request)
|
|
|
|
if realm is None:
|
|
|
|
raise InvalidSubdomainError()
|
|
|
|
return realm
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-06-15 14:15:17 +02:00
|
|
|
def get_apps_page_url() -> str:
|
|
|
|
if settings.ZILENCER_ENABLED:
|
|
|
|
return "/apps/"
|
|
|
|
return "https://zulip.com/apps/" # nocoverage
|
|
|
|
|
|
|
|
|
2017-11-27 07:33:05 +01:00
|
|
|
def zulip_default_context(request: 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.
|
|
|
|
|
2017-10-02 08:32:09 +02:00
|
|
|
The main variable in the below is whether we know what realm the
|
|
|
|
user is trying to interact with.
|
2017-05-02 00:45:12 +02:00
|
|
|
"""
|
2017-05-04 01:12:34 +02:00
|
|
|
realm = get_realm_from_request(request)
|
2016-11-08 10:00:31 +01:00
|
|
|
|
2017-08-04 07:53:19 +02:00
|
|
|
if realm is None:
|
2017-08-28 23:01:18 +02:00
|
|
|
realm_uri = settings.ROOT_DOMAIN_URI
|
2017-08-04 07:53:19 +02:00
|
|
|
realm_name = None
|
|
|
|
realm_icon = None
|
|
|
|
else:
|
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)
|
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
|
|
|
|
find_team_link_disabled = settings.FIND_TEAM_LINK_DISABLED
|
2018-05-01 20:59:24 +02:00
|
|
|
allow_search_engine_indexing = False
|
2017-07-28 01:18:37 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
if (
|
|
|
|
settings.ROOT_DOMAIN_LANDING_PAGE
|
|
|
|
and get_subdomain(request) == Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
|
|
|
|
):
|
2016-09-14 08:00:27 +02:00
|
|
|
register_link_disabled = True
|
|
|
|
login_link_disabled = True
|
|
|
|
find_team_link_disabled = False
|
2018-05-01 20:59:24 +02:00
|
|
|
allow_search_engine_indexing = True
|
2016-09-14 08:00:27 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
apps_page_web = settings.ROOT_DOMAIN_URI + "/accounts/go/"
|
2020-05-26 22:27:48 +02:00
|
|
|
|
2017-08-04 03:59:52 +02:00
|
|
|
user_is_authenticated = False
|
2021-02-12 08:20:45 +01:00
|
|
|
if hasattr(request, "user") and hasattr(request.user, "is_authenticated"):
|
2020-01-28 02:48:53 +01:00
|
|
|
user_is_authenticated = request.user.is_authenticated
|
2017-08-04 03:59:52 +02:00
|
|
|
|
2017-08-07 17:38:25 +02:00
|
|
|
if settings.DEVELOPMENT:
|
|
|
|
secrets_path = "zproject/dev-secrets.conf"
|
|
|
|
settings_path = "zproject/dev_settings.py"
|
|
|
|
settings_comments_path = "zproject/prod_settings_template.py"
|
|
|
|
else:
|
|
|
|
secrets_path = "/etc/zulip/zulip-secrets.conf"
|
|
|
|
settings_path = "/etc/zulip/settings.py"
|
|
|
|
settings_comments_path = "/etc/zulip/settings.py"
|
|
|
|
|
2020-09-16 19:30:05 +02:00
|
|
|
support_email = FromAddress.SUPPORT
|
2021-02-12 08:19:30 +01:00
|
|
|
support_email_html_tag = SafeString(
|
|
|
|
f'<a href="mailto:{escape(support_email)}">{escape(support_email)}</a>'
|
|
|
|
)
|
2020-09-16 19:30:05 +02:00
|
|
|
|
2021-04-24 01:32:32 +02:00
|
|
|
default_page_params = {
|
|
|
|
**DEFAULT_PAGE_PARAMS,
|
|
|
|
"request_language": get_language(),
|
|
|
|
}
|
|
|
|
|
2018-12-19 01:02:22 +01:00
|
|
|
context = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"root_domain_landing_page": settings.ROOT_DOMAIN_LANDING_PAGE,
|
|
|
|
"custom_logo_url": settings.CUSTOM_LOGO_URL,
|
|
|
|
"register_link_disabled": register_link_disabled,
|
|
|
|
"login_link_disabled": login_link_disabled,
|
|
|
|
"terms_of_service": settings.TERMS_OF_SERVICE,
|
|
|
|
"privacy_policy": settings.PRIVACY_POLICY,
|
|
|
|
"login_url": settings.HOME_NOT_LOGGED_IN,
|
|
|
|
"only_sso": settings.ONLY_SSO,
|
|
|
|
"external_host": settings.EXTERNAL_HOST,
|
|
|
|
"external_uri_scheme": settings.EXTERNAL_URI_SCHEME,
|
|
|
|
"realm_uri": realm_uri,
|
|
|
|
"realm_name": realm_name,
|
|
|
|
"realm_icon": realm_icon,
|
|
|
|
"root_domain_uri": settings.ROOT_DOMAIN_URI,
|
2021-06-15 14:15:17 +02:00
|
|
|
"apps_page_url": get_apps_page_url(),
|
2021-02-12 08:20:45 +01:00
|
|
|
"apps_page_web": apps_page_web,
|
|
|
|
"open_realm_creation": settings.OPEN_REALM_CREATION,
|
|
|
|
"development_environment": settings.DEVELOPMENT,
|
|
|
|
"support_email": support_email,
|
|
|
|
"support_email_html_tag": support_email_html_tag,
|
|
|
|
"find_team_link_disabled": find_team_link_disabled,
|
|
|
|
"password_min_length": settings.PASSWORD_MIN_LENGTH,
|
|
|
|
"password_min_guesses": settings.PASSWORD_MIN_GUESSES,
|
|
|
|
"zulip_version": ZULIP_VERSION,
|
|
|
|
"user_is_authenticated": user_is_authenticated,
|
|
|
|
"settings_path": settings_path,
|
|
|
|
"secrets_path": secrets_path,
|
|
|
|
"settings_comments_path": settings_comments_path,
|
2021-04-30 02:10:10 +02:00
|
|
|
"platform": request.client_name,
|
2021-02-12 08:20:45 +01:00
|
|
|
"allow_search_engine_indexing": allow_search_engine_indexing,
|
|
|
|
"landing_page_navbar_message": settings.LANDING_PAGE_NAVBAR_MESSAGE,
|
2021-04-24 01:32:32 +02:00
|
|
|
"default_page_params": default_page_params,
|
2012-10-17 20:34:38 +02:00
|
|
|
}
|
2018-12-19 01:02:22 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
context["OPEN_GRAPH_URL"] = f"{realm_uri}{request.path}"
|
2019-04-26 15:02:16 +02:00
|
|
|
if realm is not None and realm.icon_source == realm.ICON_UPLOADED:
|
2021-02-12 08:20:45 +01:00
|
|
|
context["OPEN_GRAPH_IMAGE"] = urljoin(realm_uri, realm_icon)
|
2019-04-26 15:02:16 +02:00
|
|
|
|
2019-03-20 13:13:44 +01:00
|
|
|
return context
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-20 13:13:44 +01:00
|
|
|
def login_context(request: HttpRequest) -> Dict[str, Any]:
|
|
|
|
realm = get_realm_from_request(request)
|
|
|
|
|
|
|
|
if realm is None:
|
|
|
|
realm_description = None
|
|
|
|
realm_invite_required = False
|
|
|
|
else:
|
|
|
|
realm_description = get_realm_rendered_description(realm)
|
|
|
|
realm_invite_required = realm.invite_required
|
|
|
|
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
context: Dict[str, Any] = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"realm_invite_required": realm_invite_required,
|
|
|
|
"realm_description": realm_description,
|
|
|
|
"require_email_format_usernames": require_email_format_usernames(realm),
|
|
|
|
"password_auth_enabled": password_auth_enabled(realm),
|
|
|
|
"any_social_backend_enabled": any_social_backend_enabled(realm),
|
|
|
|
"two_factor_authentication_enabled": settings.TWO_FACTOR_AUTHENTICATION_ENABLED,
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
}
|
2019-03-20 13:13:44 +01:00
|
|
|
|
2019-04-24 04:30:15 +02:00
|
|
|
if realm is not None and realm.description:
|
2021-02-12 08:20:45 +01:00
|
|
|
context["OPEN_GRAPH_TITLE"] = realm.name
|
|
|
|
context["OPEN_GRAPH_DESCRIPTION"] = get_realm_text_description(realm)
|
2019-04-24 04:30:15 +02:00
|
|
|
|
2018-12-19 01:02:22 +01:00
|
|
|
# Add the keys for our standard authentication backends.
|
2019-03-17 22:29:42 +01:00
|
|
|
no_auth_enabled = True
|
2018-12-19 01:02:22 +01:00
|
|
|
for auth_backend_name in AUTH_BACKEND_NAME_MAP:
|
|
|
|
name_lower = auth_backend_name.lower()
|
2020-06-10 06:41:04 +02:00
|
|
|
key = f"{name_lower}_auth_enabled"
|
2019-03-17 22:29:42 +01:00
|
|
|
is_enabled = auth_enabled_helper([auth_backend_name], realm)
|
|
|
|
context[key] = is_enabled
|
|
|
|
if is_enabled:
|
|
|
|
no_auth_enabled = False
|
2019-03-05 18:30:10 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
context["external_authentication_methods"] = get_external_method_dicts(realm)
|
|
|
|
context["no_auth_enabled"] = no_auth_enabled
|
2019-03-05 18:30:10 +01:00
|
|
|
|
2020-01-31 15:03:55 +01:00
|
|
|
# Include another copy of external_authentication_methods in page_params for use
|
|
|
|
# by the desktop client. We expand it with IDs of the <button> elements corresponding
|
|
|
|
# to the authentication methods.
|
2021-02-12 08:20:45 +01:00
|
|
|
context["page_params"] = dict(
|
2021-02-12 08:19:30 +01:00
|
|
|
external_authentication_methods=get_external_method_dicts(realm),
|
2020-01-31 15:03:55 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
for auth_dict in context["page_params"]["external_authentication_methods"]:
|
|
|
|
auth_dict["button_id_suffix"] = "auth_button_{}".format(auth_dict["name"])
|
2020-01-31 15:03:55 +01:00
|
|
|
|
2018-12-19 01:02:22 +01:00
|
|
|
return context
|
2019-03-20 13:13:44 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-20 13:13:44 +01:00
|
|
|
def latest_info_context() -> Dict[str, str]:
|
|
|
|
context = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"latest_release_version": LATEST_RELEASE_VERSION,
|
|
|
|
"latest_major_version": LATEST_MAJOR_VERSION,
|
|
|
|
"latest_release_announcement": LATEST_RELEASE_ANNOUNCEMENT,
|
2019-03-20 13:13:44 +01:00
|
|
|
}
|
|
|
|
return context
|