mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 05:23:35 +00:00
refactoring: Move set_default_streams into do_create_realm.
After the commits leading up to this, the only meaningful use of this function was in the realm creation process.
This commit is contained in:
@@ -3550,6 +3550,10 @@ def do_create_realm(string_id: str, name: str,
|
||||
notifications_stream = ensure_stream(realm, Realm.DEFAULT_NOTIFICATION_STREAM_NAME)
|
||||
realm.notifications_stream = notifications_stream
|
||||
|
||||
# With the current initial streams situation, the only public
|
||||
# stream is the 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.")
|
||||
@@ -3641,25 +3645,6 @@ def lookup_default_stream_groups(default_stream_group_names: List[str],
|
||||
default_stream_groups.append(default_stream_group)
|
||||
return default_stream_groups
|
||||
|
||||
def set_default_streams(realm: Realm, stream_dict: Dict[str, Dict[str, Any]]) -> None:
|
||||
DefaultStream.objects.filter(realm=realm).delete()
|
||||
stream_names = []
|
||||
for name, options in stream_dict.items():
|
||||
stream_names.append(name)
|
||||
stream = ensure_stream(realm,
|
||||
name,
|
||||
invite_only = options.get("invite_only", False),
|
||||
stream_description = options.get("description", ''))
|
||||
DefaultStream.objects.create(stream=stream, realm=realm)
|
||||
|
||||
# Always include the realm's default notifications streams, if it exists
|
||||
if realm.notifications_stream is not None:
|
||||
DefaultStream.objects.get_or_create(stream=realm.notifications_stream, realm=realm)
|
||||
|
||||
log_event({'type': 'default_streams',
|
||||
'realm': realm.string_id,
|
||||
'streams': stream_names})
|
||||
|
||||
def notify_default_streams(realm: Realm) -> None:
|
||||
event = dict(
|
||||
type="default_streams",
|
||||
|
||||
@@ -28,12 +28,11 @@ from zerver.views.development.registration import confirmation_key
|
||||
|
||||
from zerver.models import (
|
||||
get_realm, get_user, get_realm_stream, get_stream_recipient,
|
||||
CustomProfileField, CustomProfileFieldValue, PreregistrationUser,
|
||||
CustomProfileField, CustomProfileFieldValue, DefaultStream, PreregistrationUser,
|
||||
Realm, Recipient, Message, ScheduledEmail, UserProfile, UserMessage,
|
||||
Stream, Subscription, flush_per_request_caches
|
||||
)
|
||||
from zerver.lib.actions import (
|
||||
set_default_streams,
|
||||
do_change_is_admin,
|
||||
get_stream,
|
||||
do_create_default_stream_group,
|
||||
@@ -68,7 +67,7 @@ import re
|
||||
import smtplib
|
||||
import ujson
|
||||
|
||||
from typing import Any, Dict, List, Optional
|
||||
from typing import Any, List, Optional
|
||||
|
||||
import urllib
|
||||
import pytz
|
||||
@@ -130,12 +129,9 @@ class AddNewUserHistoryTest(ZulipTestCase):
|
||||
def test_add_new_user_history_race(self) -> None:
|
||||
"""Sends a message during user creation"""
|
||||
# Create a user who hasn't had historical messages added
|
||||
stream_dict = {
|
||||
"Denmark": {"description": "A Scandinavian country", "invite_only": False},
|
||||
"Verona": {"description": "A city in Italy", "invite_only": False}
|
||||
} # type: Dict[str, Dict[str, Any]]
|
||||
realm = get_realm('zulip')
|
||||
set_default_streams(realm, stream_dict)
|
||||
stream = Stream.objects.get(realm=realm, name='Denmark')
|
||||
DefaultStream.objects.create(stream=stream, realm=realm)
|
||||
with patch("zerver.lib.actions.add_new_user_history"):
|
||||
self.register(self.nonreg_email('test'), "test")
|
||||
user_profile = self.nonreg_user('test')
|
||||
@@ -447,12 +443,11 @@ class LoginTest(ZulipTestCase):
|
||||
|
||||
def test_register(self) -> None:
|
||||
realm = get_realm("zulip")
|
||||
stream_dict = {"stream_"+str(i): {"description": "stream_%s_description" % i, "invite_only": False}
|
||||
for i in range(40)} # type: Dict[str, Dict[str, Any]]
|
||||
for stream_name in stream_dict.keys():
|
||||
self.make_stream(stream_name, realm=realm)
|
||||
stream_names = ["stream_{}".format(i) for i in range(40)]
|
||||
for stream_name in stream_names:
|
||||
stream = self.make_stream(stream_name, realm=realm)
|
||||
DefaultStream.objects.create(stream=stream, realm=realm)
|
||||
|
||||
set_default_streams(realm, stream_dict)
|
||||
# Clear all the caches.
|
||||
flush_per_request_caches()
|
||||
ContentType.objects.clear_cache()
|
||||
|
||||
@@ -52,7 +52,7 @@ from zerver.lib.actions import (
|
||||
do_create_realm, do_remove_default_stream, bulk_get_subscriber_user_ids,
|
||||
gather_subscriptions_helper, bulk_add_subscriptions, bulk_remove_subscriptions,
|
||||
gather_subscriptions, get_default_streams_for_realm, get_realm, get_stream,
|
||||
set_default_streams, do_get_streams,
|
||||
do_get_streams,
|
||||
create_stream_if_needed, create_streams_if_needed,
|
||||
ensure_stream,
|
||||
do_deactivate_stream,
|
||||
@@ -1052,44 +1052,6 @@ class DefaultStreamTest(ZulipTestCase):
|
||||
stream_names = [s.name for s in streams]
|
||||
return set(stream_names)
|
||||
|
||||
def get_default_stream_descriptions(self, realm: Realm) -> Set[str]:
|
||||
streams = get_default_streams_for_realm(realm.id)
|
||||
stream_descriptions = [s.description for s in streams]
|
||||
return set(stream_descriptions)
|
||||
|
||||
def test_set_default_streams(self) -> None:
|
||||
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},
|
||||
"Carrot Cake": {"description": "A delicious treat", "invite_only": False}
|
||||
} # type: Dict[str, Dict[str, Any]]
|
||||
expected_names = list(stream_dict.keys())
|
||||
expected_names.append("general")
|
||||
expected_descriptions = [i["description"] for i in stream_dict.values()] + [""]
|
||||
set_default_streams(realm, stream_dict)
|
||||
stream_names_set = self.get_default_stream_names(realm)
|
||||
stream_descriptions_set = self.get_default_stream_descriptions(realm)
|
||||
self.assertEqual(stream_names_set, set(expected_names))
|
||||
self.assertEqual(stream_descriptions_set, set(expected_descriptions))
|
||||
|
||||
def test_set_default_streams_no_notifications_stream(self) -> None:
|
||||
realm = do_create_realm("testrealm", "Test Realm")
|
||||
realm.notifications_stream = None
|
||||
realm.save(update_fields=["notifications_stream"])
|
||||
stream_dict = {
|
||||
"apple": {"description": "A red fruit", "invite_only": False},
|
||||
"banana": {"description": "A yellow fruit", "invite_only": False},
|
||||
"Carrot Cake": {"description": "A delicious treat", "invite_only": False}
|
||||
} # type: Dict[str, Dict[str, Any]]
|
||||
expected_names = list(stream_dict.keys())
|
||||
expected_descriptions = [i["description"] for i in stream_dict.values()]
|
||||
set_default_streams(realm, stream_dict)
|
||||
stream_names_set = self.get_default_stream_names(realm)
|
||||
stream_descriptions_set = self.get_default_stream_descriptions(realm)
|
||||
self.assertEqual(stream_names_set, set(expected_names))
|
||||
self.assertEqual(stream_descriptions_set, set(expected_descriptions))
|
||||
|
||||
def test_add_and_remove_default_stream(self) -> None:
|
||||
realm = get_realm("zulip")
|
||||
stream = ensure_stream(realm, "Added Stream")
|
||||
|
||||
@@ -18,7 +18,7 @@ from zerver.models import UserProfile, Realm, Stream, MultiuseInvite, \
|
||||
from zerver.lib.send_email import send_email, FromAddress
|
||||
from zerver.lib.actions import do_change_password, do_change_full_name, \
|
||||
do_activate_user, do_create_user, do_create_realm, \
|
||||
validate_email_for_realm, set_default_streams, \
|
||||
validate_email_for_realm, \
|
||||
do_set_user_display_setting, lookup_default_stream_groups, bulk_add_subscriptions
|
||||
from zerver.forms import RegistrationForm, HomepageForm, RealmCreationForm, \
|
||||
FindMyTeamForm, RealmRedirectForm
|
||||
@@ -193,7 +193,6 @@ def accounts_register(request: HttpRequest) -> HttpResponse:
|
||||
string_id = form.cleaned_data['realm_subdomain']
|
||||
realm_name = form.cleaned_data['realm_name']
|
||||
realm = do_create_realm(string_id, realm_name)
|
||||
set_default_streams(realm, {})
|
||||
setup_realm_internal_bots(realm)
|
||||
assert(realm is not None)
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from typing import Any
|
||||
|
||||
from zerver.lib.actions import do_create_realm, do_create_user, \
|
||||
bulk_add_subscriptions, set_default_streams
|
||||
bulk_add_subscriptions
|
||||
from zerver.lib.management import ZulipBaseCommand
|
||||
from zerver.lib.onboarding import send_initial_realm_messages
|
||||
from zerver.models import Realm, UserProfile
|
||||
@@ -13,7 +13,6 @@ class Command(ZulipBaseCommand):
|
||||
string_id = 'realm%02d' % (
|
||||
Realm.objects.filter(string_id__startswith='realm').count(),)
|
||||
realm = do_create_realm(string_id, string_id)
|
||||
set_default_streams(realm, {})
|
||||
|
||||
name = '%02d-user' % (
|
||||
UserProfile.objects.filter(email__contains='user@').count(),)
|
||||
|
||||
Reference in New Issue
Block a user