bulk_create: Refactor bulk_create_streams to not take domain dictionary.

First step in cleaning up populate_db.create_streams and
bulk_create.bulk_create_streams. Part of a series of commits to remove
Realm.domain from populate_db.
This commit is contained in:
Rishi Gupta
2016-12-15 07:57:21 -08:00
committed by Tim Abbott
parent c24fbd28b2
commit fbf48f7ec6
2 changed files with 14 additions and 16 deletions

View File

@@ -66,25 +66,24 @@ def bulk_create_users(realms, users_raw, bot_type=None, tos_version=None):
recipient=recipients_by_email[email]))
Subscription.objects.bulk_create(subscriptions_to_create)
def bulk_create_streams(realms, stream_dict):
# type: (Mapping[text_type, Realm], Dict[text_type, Dict[text_type, Any]]) -> None
existing_streams = set((stream.realm.domain, stream.name.lower())
for stream in Stream.objects.select_related().all())
def bulk_create_streams(realm, stream_dict):
# type: (Realm, Dict[text_type, Dict[text_type, Any]]) -> None
existing_streams = set(stream.name.lower()
for stream in Stream.objects.select_related().filter(realm=realm))
streams_to_create = [] # type: List[Stream]
for name, options in stream_dict.items():
if (options["domain"], name.lower()) not in existing_streams:
if name.lower() not in existing_streams:
streams_to_create.append(
Stream(
realm=realms[options["domain"]],
name=name, description=options["description"],
realm=realm, name=name, description=options["description"],
invite_only=options["invite_only"]
)
)
Stream.objects.bulk_create(streams_to_create)
recipients_to_create = [] # type: List[Recipient]
for stream in Stream.objects.select_related().all():
if (stream.realm.domain, stream.name.lower()) not in existing_streams:
for stream in Stream.objects.select_related().filter(realm=realm):
if stream.name.lower() not in existing_streams:
recipients_to_create.append(Recipient(type_id=stream.id,
type=Recipient.STREAM))
Recipient.objects.bulk_create(recipients_to_create)

View File

@@ -36,17 +36,17 @@ def create_users(realms, name_list, bot_type=None):
tos_version = settings.TOS_VERSION if bot_type is None else None
bulk_create_users(realms, user_set, bot_type=bot_type, tos_version=tos_version)
def create_streams(realms, realm, stream_dict):
# type: (Mapping[Text, Realm], Realm, Dict[Text, Dict[Text, Any]]) -> None
def create_streams(realm, stream_dict):
# type: (Realm, Dict[Text, Dict[Text, Any]]) -> None
stream_dictionary = {
name: {
"domain": realm.domain,
"realm": realm,
"description": options["description"],
"invite_only": options["invite_only"],
} for name, options in stream_dict.items()
} # type: Dict[Text, Dict[Text, Any]]
bulk_create_streams(realms, stream_dictionary)
bulk_create_streams(realm, stream_dictionary)
class Command(BaseCommand):
@@ -165,7 +165,7 @@ class Command(BaseCommand):
"Rome": {"description": "Yet another Italian city", "invite_only": False}
} # type: Dict[Text, Dict[Text, Any]]
create_streams(realms, zulip_realm, stream_dict)
create_streams(zulip_realm, stream_dict)
recipient_streams = [Stream.objects.get(name=name, realm=zulip_realm).id
for name in stream_list] # type: List[int]
# Create subscriptions to streams. The following
@@ -280,7 +280,7 @@ class Command(BaseCommand):
"errors": {"description": "For errors", "invite_only": False},
"sales": {"description": "For sales discussion", "invite_only": False}
} # type: Dict[Text, Dict[Text, Any]]
create_streams(realms, zulip_realm, zulip_stream_dict)
create_streams(zulip_realm, zulip_stream_dict)
# Add a few default streams
for default_stream_name in ["design", "devel", "social", "support"]:
@@ -409,4 +409,3 @@ def send_messages(data):
recipients[num_messages] = (message_type, message.recipient.id, saved_data)
num_messages += 1
return tot_messages