def do_create_realm(string_id: str, name: str, emails_restricted_to_domains: Optional[bool]=None) -> Realm:
    if Realm.objects.filter(string_id=string_id).exists():
        raise AssertionError(f'Realm {string_id} already exists!')
    if (not server_initialized()):
        logging.info('Server not yet initialized. Creating the internal realm first.')
        create_internal_realm()
    kwargs: Dict[(str, Any)] = {}
    if (emails_restricted_to_domains is not None):
        kwargs['emails_restricted_to_domains'] = emails_restricted_to_domains
    realm = Realm(string_id=string_id, name=name, **kwargs)
    realm.save()
    notifications_stream = ensure_stream(realm, Realm.DEFAULT_NOTIFICATION_STREAM_NAME, stream_description='Everyone is added to this stream by default. Welcome! :octopus:', acting_user=None)
    realm.notifications_stream = notifications_stream
    DefaultStream.objects.create(stream=notifications_stream, realm=realm)
    signup_notifications_stream = ensure_stream(realm, Realm.INITIAL_PRIVATE_STREAM_NAME, invite_only=True, stream_description='A private stream for core team members.', acting_user=None)
    realm.signup_notifications_stream = signup_notifications_stream
    realm.save(update_fields=['notifications_stream', 'signup_notifications_stream'])
    if settings.BILLING_ENABLED:
        do_change_plan_type(realm, Realm.LIMITED)
    log_event({'type': 'realm_created', 'string_id': string_id, 'emails_restricted_to_domains': emails_restricted_to_domains})
    sender = get_system_bot(settings.NOTIFICATION_BOT)
    admin_realm = sender.realm
    with override_language(admin_realm.default_language):
        signup_message = _('Signups enabled')
    try:
        signups_stream = get_signups_stream(admin_realm)
        topic = realm.display_subdomain
        internal_send_stream_message(admin_realm, sender, signups_stream, topic, signup_message)
    except Stream.DoesNotExist:
        pass
    return realm
def do_create_realm(string_id: str, name: str, emails_restricted_to_domains: Optional[bool]=None) -> Realm:
    if Realm.objects.filter(string_id=string_id).exists():
        raise AssertionError(f'Realm {string_id} already exists!')
    if (not server_initialized()):
        logging.info('Server not yet initialized. Creating the internal realm first.')
        create_internal_realm()
    kwargs: Dict[(str, Any)] = {}
    if (emails_restricted_to_domains is not None):
        kwargs['emails_restricted_to_domains'] = emails_restricted_to_domains
    realm = Realm(string_id=string_id, name=name, **kwargs)
    realm.save()
    notifications_stream = ensure_stream(realm, Realm.DEFAULT_NOTIFICATION_STREAM_NAME, stream_description='Everyone is added to this stream by default. Welcome! :octopus:', acting_user=None)
    realm.notifications_stream = notifications_stream
    DefaultStream.objects.create(stream=notifications_stream, realm=realm)
    signup_notifications_stream = ensure_stream(realm, Realm.INITIAL_PRIVATE_STREAM_NAME, invite_only=True, stream_description='A private stream for core team members.', acting_user=None)
    realm.signup_notifications_stream = signup_notifications_stream
    realm.save(update_fields=['notifications_stream', 'signup_notifications_stream'])
    if settings.BILLING_ENABLED:
        do_change_plan_type(realm, Realm.LIMITED)
    log_event({'type': 'realm_created', 'string_id': string_id, 'emails_restricted_to_domains': emails_restricted_to_domains})
    sender = get_system_bot(settings.NOTIFICATION_BOT)
    admin_realm = sender.realm
    with override_language(admin_realm.default_language):
        signup_message = _('Signups enabled')
    try:
        signups_stream = get_signups_stream(admin_realm)
        topic = realm.display_subdomain
        internal_send_stream_message(admin_realm, sender, signups_stream, topic, signup_message)
    except Stream.DoesNotExist:
        pass
    return realm