models.Realm: Rename subdomain to string_id.

Does a database migration to rename Realm.subdomain to
Realm.string_id, and makes Realm.subdomain a property.  Eventually,
Realm.string_id will replace Realm.domain as the handle by which we
retrieve Realm objects.
This commit is contained in:
Rishi Gupta 2016-10-26 12:13:43 -04:00 committed by Tim Abbott
parent 4dd8cd9879
commit 64bcd71d6e
7 changed files with 37 additions and 11 deletions

View File

@ -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()

View File

@ -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',
),
]

View File

@ -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

View File

@ -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):

View File

@ -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]

View File

@ -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()

View File

@ -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():