do_create_realm: Remove unnecessary second return value.

Unlike creating a stream, there's really no reason one would want to
call the function to create a realm while uncertain whether that realm
already existed.
This commit is contained in:
Tim Abbott
2017-08-23 19:52:34 -07:00
parent 8a65fdce49
commit d3e3c704d4
4 changed files with 40 additions and 39 deletions

View File

@@ -2321,39 +2321,40 @@ def do_change_stream_description(stream, new_description):
def do_create_realm(string_id, name, restricted_to_domain=None,
invite_required=None, org_type=None):
# type: (Text, Text, Optional[bool], Optional[bool], Optional[int]) -> Tuple[Realm, bool]
realm = get_realm(string_id)
created = not realm
if created:
kwargs = {} # type: Dict[str, Any]
if restricted_to_domain is not None:
kwargs['restricted_to_domain'] = restricted_to_domain
if invite_required is not None:
kwargs['invite_required'] = invite_required
if org_type is not None:
kwargs['org_type'] = org_type
realm = Realm(string_id=string_id, name=name, **kwargs)
realm.save()
# type: (Text, Text, Optional[bool], Optional[bool], Optional[int]) -> Realm
existing_realm = get_realm(string_id)
if existing_realm is not None:
raise AssertionError("Realm %s already exists!" % (string_id,))
# Create stream once Realm object has been saved
notifications_stream, _ = create_stream_if_needed(realm, Realm.DEFAULT_NOTIFICATION_STREAM_NAME)
realm.notifications_stream = notifications_stream
realm.save(update_fields=['notifications_stream'])
kwargs = {} # type: Dict[str, Any]
if restricted_to_domain is not None:
kwargs['restricted_to_domain'] = restricted_to_domain
if invite_required is not None:
kwargs['invite_required'] = invite_required
if org_type is not None:
kwargs['org_type'] = org_type
realm = Realm(string_id=string_id, name=name, **kwargs)
realm.save()
# Log the event
log_event({"type": "realm_created",
"string_id": string_id,
"restricted_to_domain": restricted_to_domain,
"invite_required": invite_required,
"org_type": org_type})
# Create stream once Realm object has been saved
notifications_stream, _ = create_stream_if_needed(realm, Realm.DEFAULT_NOTIFICATION_STREAM_NAME)
realm.notifications_stream = notifications_stream
realm.save(update_fields=['notifications_stream'])
# Send a notification to the admin realm (if configured)
if settings.NEW_USER_BOT is not None:
signup_message = "Signups enabled"
admin_realm = get_system_bot(settings.NEW_USER_BOT).realm
internal_send_message(admin_realm, settings.NEW_USER_BOT, "stream",
"signups", string_id, signup_message)
return (realm, created)
# Log the event
log_event({"type": "realm_created",
"string_id": string_id,
"restricted_to_domain": restricted_to_domain,
"invite_required": invite_required,
"org_type": org_type})
# Send a notification to the admin realm (if configured)
if settings.NEW_USER_BOT is not None:
signup_message = "Signups enabled"
admin_realm = get_system_bot(settings.NEW_USER_BOT).realm
internal_send_message(admin_realm, settings.NEW_USER_BOT, "stream",
"signups", string_id, signup_message)
return realm
def do_change_notification_settings(user_profile, name, value, log=True):
# type: (UserProfile, str, bool, bool) -> None

View File

@@ -121,9 +121,9 @@ class RealmDomainTest(ZulipTestCase):
def test_get_realm_by_email_domain(self):
# type: () -> None
realm1, created = do_create_realm('testrealm1', 'Test Realm 1')
realm2, created = do_create_realm('testrealm2', 'Test Realm 2')
realm3, created = do_create_realm('testrealm3', 'Test Realm 3')
realm1 = do_create_realm('testrealm1', 'Test Realm 1')
realm2 = do_create_realm('testrealm2', 'Test Realm 2')
realm3 = do_create_realm('testrealm3', 'Test Realm 3')
realm_domain_1 = RealmDomain.objects.create(realm=realm1, domain='test1.com', allow_subdomains=True)
realm_domain_2 = RealmDomain.objects.create(realm=realm2, domain='test2.test1.com', allow_subdomains=False)
@@ -161,8 +161,8 @@ class RealmDomainTest(ZulipTestCase):
def test_email_allowed_for_realm(self):
# type: () -> None
realm1, created = do_create_realm('testrealm1', 'Test Realm 1', restricted_to_domain=True)
realm2, created = do_create_realm('testrealm2', 'Test Realm 2', restricted_to_domain=True)
realm1 = do_create_realm('testrealm1', 'Test Realm 1', restricted_to_domain=True)
realm2 = do_create_realm('testrealm2', 'Test Realm 2', restricted_to_domain=True)
realm_domain = RealmDomain.objects.create(realm=realm1, domain='test1.com', allow_subdomains=False)
RealmDomain.objects.create(realm=realm2, domain='test2.test1.com', allow_subdomains=True)

View File

@@ -740,7 +740,7 @@ class DefaultStreamTest(ZulipTestCase):
def test_set_default_streams(self):
# type: () -> None
(realm, _) = do_create_realm("testrealm", "Test Realm")
realm = do_create_realm("testrealm", "Test Realm")
stream_dict = {
"apple": {"description": "A red fruit", "invite_only": False},
"banana": {"description": "A yellow fruit", "invite_only": False},
@@ -757,7 +757,7 @@ class DefaultStreamTest(ZulipTestCase):
def test_set_default_streams_no_notifications_stream(self):
# type: () -> None
(realm, _) = do_create_realm("testrealm", "Test Realm")
realm = do_create_realm("testrealm", "Test Realm")
realm.notifications_stream = None
realm.save(update_fields=["notifications_stream"])
stream_dict = {
@@ -1439,7 +1439,7 @@ class SubscriptionAPITest(ZulipTestCase):
Calling POST /json/users/me/subscriptions in a new realm
should notify with a proper new stream link
"""
(realm, _) = do_create_realm("testrealm", "Test Realm")
realm = do_create_realm("testrealm", "Test Realm")
notifications_stream = Stream.objects.get(name='announce', realm=realm)
realm.notifications_stream = notifications_stream

View File

@@ -195,7 +195,7 @@ def accounts_register(request):
if realm_creation:
string_id = form.cleaned_data['realm_subdomain']
realm_name = form.cleaned_data['realm_name']
realm = do_create_realm(string_id, realm_name)[0]
realm = do_create_realm(string_id, realm_name)
setup_initial_streams(realm)
assert(realm is not None)