subdomains: Add a variable for how root domain is represented.

We use Realm.SUBDOMAIN_FOR_ROOT_DOMAIN as the special name for how the
root domain is referred to as a subdomain in the code.
This commit is contained in:
Tim Abbott 2017-10-18 22:46:05 -07:00
parent 07438b2f2c
commit 0884588a5e
2 changed files with 5 additions and 2 deletions

View File

@ -4,19 +4,21 @@ from django.conf import settings
from django.http import HttpRequest
from typing import Optional, Text
from zerver.models import get_realm, Realm
def _extract_subdomain(request):
# type: (HttpRequest) -> Text
domain = request.get_host().lower()
index = domain.find("." + settings.EXTERNAL_HOST)
if index == -1:
return ""
return Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
return domain[0:index]
def get_subdomain(request):
# type: (HttpRequest) -> Text
subdomain = _extract_subdomain(request)
if subdomain in settings.ROOT_SUBDOMAIN_ALIASES:
return ""
return Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
return subdomain
def is_subdomain_root_or_alias(request):

View File

@ -137,6 +137,7 @@ class Realm(ModelReprMixin, models.Model):
MAX_REALM_NAME_LENGTH = 40
MAX_REALM_SUBDOMAIN_LENGTH = 40
AUTHENTICATION_FLAGS = [u'Google', u'Email', u'GitHub', u'LDAP', u'Dev', u'RemoteUser']
SUBDOMAIN_FOR_ROOT_DOMAIN = ''
name = models.CharField(max_length=MAX_REALM_NAME_LENGTH, null=True) # type: Optional[Text]
string_id = models.CharField(max_length=MAX_REALM_SUBDOMAIN_LENGTH, unique=True) # type: Text