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])) recipient=recipients_by_email[email]))
Subscription.objects.bulk_create(subscriptions_to_create) Subscription.objects.bulk_create(subscriptions_to_create)
def bulk_create_streams(realms, stream_dict): def bulk_create_streams(realm, stream_dict):
# type: (Mapping[text_type, Realm], Dict[text_type, Dict[text_type, Any]]) -> None # type: (Realm, Dict[text_type, Dict[text_type, Any]]) -> None
existing_streams = set((stream.realm.domain, stream.name.lower()) existing_streams = set(stream.name.lower()
for stream in Stream.objects.select_related().all()) for stream in Stream.objects.select_related().filter(realm=realm))
streams_to_create = [] # type: List[Stream] streams_to_create = [] # type: List[Stream]
for name, options in stream_dict.items(): 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( streams_to_create.append(
Stream( Stream(
realm=realms[options["domain"]], realm=realm, name=name, description=options["description"],
name=name, description=options["description"],
invite_only=options["invite_only"] invite_only=options["invite_only"]
) )
) )
Stream.objects.bulk_create(streams_to_create) Stream.objects.bulk_create(streams_to_create)
recipients_to_create = [] # type: List[Recipient] recipients_to_create = [] # type: List[Recipient]
for stream in Stream.objects.select_related().all(): for stream in Stream.objects.select_related().filter(realm=realm):
if (stream.realm.domain, stream.name.lower()) not in existing_streams: if stream.name.lower() not in existing_streams:
recipients_to_create.append(Recipient(type_id=stream.id, recipients_to_create.append(Recipient(type_id=stream.id,
type=Recipient.STREAM)) type=Recipient.STREAM))
Recipient.objects.bulk_create(recipients_to_create) 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 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) bulk_create_users(realms, user_set, bot_type=bot_type, tos_version=tos_version)
def create_streams(realms, realm, stream_dict): def create_streams(realm, stream_dict):
# type: (Mapping[Text, Realm], Realm, Dict[Text, Dict[Text, Any]]) -> None # type: (Realm, Dict[Text, Dict[Text, Any]]) -> None
stream_dictionary = { stream_dictionary = {
name: { name: {
"domain": realm.domain, "realm": realm,
"description": options["description"], "description": options["description"],
"invite_only": options["invite_only"], "invite_only": options["invite_only"],
} for name, options in stream_dict.items() } for name, options in stream_dict.items()
} # type: Dict[Text, Dict[Text, Any]] } # type: Dict[Text, Dict[Text, Any]]
bulk_create_streams(realms, stream_dictionary) bulk_create_streams(realm, stream_dictionary)
class Command(BaseCommand): class Command(BaseCommand):
@@ -165,7 +165,7 @@ class Command(BaseCommand):
"Rome": {"description": "Yet another Italian city", "invite_only": False} "Rome": {"description": "Yet another Italian city", "invite_only": False}
} # type: Dict[Text, Dict[Text, Any]] } # 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 recipient_streams = [Stream.objects.get(name=name, realm=zulip_realm).id
for name in stream_list] # type: List[int] for name in stream_list] # type: List[int]
# Create subscriptions to streams. The following # Create subscriptions to streams. The following
@@ -280,7 +280,7 @@ class Command(BaseCommand):
"errors": {"description": "For errors", "invite_only": False}, "errors": {"description": "For errors", "invite_only": False},
"sales": {"description": "For sales discussion", "invite_only": False} "sales": {"description": "For sales discussion", "invite_only": False}
} # type: Dict[Text, Dict[Text, Any]] } # 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 # Add a few default streams
for default_stream_name in ["design", "devel", "social", "support"]: 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) recipients[num_messages] = (message_type, message.recipient.id, saved_data)
num_messages += 1 num_messages += 1
return tot_messages return tot_messages