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)