2017-10-19 07:21:57 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.http import HttpRequest
|
|
|
|
from typing import Optional, Text
|
|
|
|
|
2017-10-20 02:53:24 +02:00
|
|
|
from zerver.models import get_realm, Realm, UserProfile
|
2017-10-19 07:46:05 +02:00
|
|
|
|
2017-10-20 05:23:00 +02:00
|
|
|
def get_subdomain(request):
|
2017-10-19 07:21:57 +02:00
|
|
|
# type: (HttpRequest) -> Text
|
2017-10-20 05:23:00 +02:00
|
|
|
host = request.get_host().lower()
|
|
|
|
index = host.find("." + settings.EXTERNAL_HOST)
|
2017-10-19 07:21:57 +02:00
|
|
|
if index == -1:
|
2017-10-19 07:46:05 +02:00
|
|
|
return Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
|
2017-10-20 05:23:00 +02:00
|
|
|
subdomain = host[0:index]
|
2017-10-19 07:21:57 +02:00
|
|
|
if subdomain in settings.ROOT_SUBDOMAIN_ALIASES:
|
2017-10-19 07:46:05 +02:00
|
|
|
return Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
|
2017-10-19 07:21:57 +02:00
|
|
|
return subdomain
|
|
|
|
|
|
|
|
def is_subdomain_root_or_alias(request):
|
|
|
|
# type: (HttpRequest) -> bool
|
2017-10-20 05:10:32 +02:00
|
|
|
return get_subdomain(request) == Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
|
2017-10-19 07:21:57 +02:00
|
|
|
|
2017-10-20 02:53:24 +02:00
|
|
|
def user_matches_subdomain(realm_subdomain, user_profile):
|
|
|
|
# type: (Optional[Text], UserProfile) -> bool
|
2017-10-20 02:54:57 +02:00
|
|
|
if realm_subdomain is None:
|
|
|
|
return True
|
|
|
|
return user_profile.realm.subdomain == realm_subdomain
|
2017-10-20 02:53:24 +02:00
|
|
|
|
2017-10-19 07:42:03 +02:00
|
|
|
def is_root_domain_available():
|
|
|
|
# type: () -> bool
|
|
|
|
if settings.ROOT_DOMAIN_LANDING_PAGE:
|
|
|
|
return False
|
|
|
|
return get_realm(Realm.SUBDOMAIN_FOR_ROOT_DOMAIN) is None
|