diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 2c367718d7..bc4c16b1e2 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -1959,7 +1959,7 @@ def get_realm_creation_defaults(org_type=None, restricted_to_domain=None, invite 'restricted_to_domain': restricted_to_domain, 'invite_required': invite_required} -def do_create_realm(domain, name, subdomain=None, restricted_to_domain=None, +def do_create_realm(domain, name, string_id=None, restricted_to_domain=None, invite_required=None, org_type=None): # type: (text_type, text_type, Optional[text_type], Optional[bool], Optional[bool], Optional[int]) -> Tuple[Realm, bool] domain = domain.lower() @@ -1972,7 +1972,7 @@ def do_create_realm(domain, name, subdomain=None, restricted_to_domain=None, org_type = realm_params['org_type'] restricted_to_domain = realm_params['restricted_to_domain'] invite_required = realm_params['invite_required'] - realm = Realm(domain=domain, name=name, org_type=org_type, subdomain=subdomain, + realm = Realm(domain=domain, name=name, org_type=org_type, string_id=string_id, restricted_to_domain=restricted_to_domain, invite_required=invite_required) realm.save() diff --git a/zerver/migrations/0036_rename_subdomain_to_string_id.py b/zerver/migrations/0036_rename_subdomain_to_string_id.py new file mode 100644 index 0000000000..867152c6b0 --- /dev/null +++ b/zerver/migrations/0036_rename_subdomain_to_string_id.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('zerver', '0035_realm_message_retention_period_days'), + ] + + operations = [ + migrations.RenameField( + model_name='realm', + old_name='subdomain', + new_name='string_id', + ), + ] diff --git a/zerver/models.py b/zerver/models.py index 79760616f5..721f7207ed 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -138,7 +138,7 @@ class Realm(ModelReprMixin, models.Model): # name is the user-visible identifier for the realm. It has no required # structure. name = models.CharField(max_length=40, null=True) # type: Optional[text_type] - subdomain = models.CharField(max_length=40, null=True, unique=True) # type: Optional[text_type] + string_id = models.CharField(max_length=40, null=True, unique=True) # type: Optional[text_type] restricted_to_domain = models.BooleanField(default=True) # type: bool invite_required = models.BooleanField(default=False) # type: bool invite_by_admins_only = models.BooleanField(default=False) # type: bool @@ -201,6 +201,13 @@ 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 subdomain(self): + # type: () -> text_type + if settings.REALMS_HAVE_SUBDOMAINS: + return self.string_id + return None + @property def uri(self): # type: () -> str @@ -272,7 +279,7 @@ def resolve_email_to_domain(email): def resolve_subdomain_to_realm(subdomain): # type: (text_type) -> Optional[Realm] try: - return Realm.objects.get(subdomain=subdomain) + return Realm.objects.get(string_id=subdomain) except Realm.DoesNotExist: return None diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py index 9f63c1193a..b28fb002ef 100644 --- a/zerver/tests/test_auth_backends.py +++ b/zerver/tests/test_auth_backends.py @@ -96,7 +96,7 @@ class AuthBackendTest(TestCase): def setup_subdomain(self, user_profile): # type: (UserProfile) -> None realm = user_profile.realm - realm.subdomain = 'zulip' + realm.string_id = 'zulip' realm.save() def test_email_auth_backend(self): @@ -1100,7 +1100,7 @@ class TestLDAP(ZulipTestCase): def setup_subdomain(self, user_profile): # type: (UserProfile) -> None realm = user_profile.realm - realm.subdomain = 'zulip' + realm.string_id = 'zulip' realm.save() def test_login_success(self): diff --git a/zerver/views/__init__.py b/zerver/views/__init__.py index 953a5bee4c..630d10921a 100644 --- a/zerver/views/__init__.py +++ b/zerver/views/__init__.py @@ -223,7 +223,7 @@ def accounts_register(request): org_type = int(form.cleaned_data['realm_org_type']) if settings.REALMS_HAVE_SUBDOMAINS: realm = do_create_realm(domain, realm_name, org_type=org_type, - subdomain=form.cleaned_data['realm_subdomain'])[0] + string_id=form.cleaned_data['realm_subdomain'])[0] else: realm = do_create_realm(domain, realm_name, org_type=org_type)[0] diff --git a/zerver/views/auth.py b/zerver/views/auth.py index 306ae50e48..51c98d0902 100644 --- a/zerver/views/auth.py +++ b/zerver/views/auth.py @@ -164,7 +164,7 @@ def send_oauth_request_to_google(request): # type: (HttpRequest) -> HttpResponse subdomain = request.GET.get('subdomain', '') if settings.REALMS_HAVE_SUBDOMAINS: - if not subdomain or not Realm.objects.filter(subdomain=subdomain).count(): + if not subdomain or not Realm.objects.filter(string_id=subdomain).exists(): return redirect_to_subdomain_login_url() google_uri = 'https://accounts.google.com/o/oauth2/auth?' @@ -264,7 +264,7 @@ def finish_google_oauth2(request): full_name, invalid_subdomain) try: - realm = Realm.objects.get(subdomain=subdomain) + realm = Realm.objects.get(string_id=subdomain) except Realm.DoesNotExist: return redirect_to_subdomain_login_url() diff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.py index 69db571817..cfad4b1c35 100644 --- a/zilencer/management/commands/populate_db.py +++ b/zilencer/management/commands/populate_db.py @@ -125,10 +125,10 @@ class Command(BaseCommand): # Create our two default realms # Could in theory be done via zerver.lib.actions.do_create_realm, but # welcome-bot (needed for do_create_realm) hasn't been created yet - zulip_realm = Realm.objects.create(domain="zulip.com", name="Zulip Dev", subdomain="zulip") + zulip_realm = Realm.objects.create(domain="zulip.com", name="Zulip Dev", string_id="zulip") RealmAlias.objects.create(realm=zulip_realm, domain="zulip.com") if options["test_suite"]: - mit_realm = Realm.objects.create(domain="mit.edu", name="MIT", subdomain="mit") + mit_realm = Realm.objects.create(domain="mit.edu", name="MIT", string_id="mit") RealmAlias.objects.create(realm=mit_realm, domain="mit.edu") realms = {} # type: Dict[text_type, Realm] for realm in Realm.objects.all():