2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
2016-06-04 00:52:29 +02:00
|
|
|
from typing import Any, Iterable, Mapping, Optional, Set, Tuple
|
|
|
|
from six import text_type
|
2013-04-23 18:51:17 +02:00
|
|
|
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.initial_password import initial_password
|
|
|
|
from zerver.models import Realm, Stream, UserProfile, Huddle, \
|
2013-11-22 23:48:00 +01:00
|
|
|
Subscription, Recipient, Client, get_huddle_hash, resolve_email_to_domain
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.create_user import create_user_profile
|
2013-01-10 21:50:09 +01:00
|
|
|
|
|
|
|
def bulk_create_realms(realm_list):
|
2016-06-13 06:38:14 +02:00
|
|
|
# type: (Iterable[text_type]) -> None
|
2013-01-10 21:50:09 +01:00
|
|
|
existing_realms = set(r.domain for r in Realm.objects.select_related().all())
|
|
|
|
|
|
|
|
realms_to_create = []
|
|
|
|
for domain in realm_list:
|
|
|
|
if domain not in existing_realms:
|
2013-10-17 17:47:30 +02:00
|
|
|
realms_to_create.append(Realm(domain=domain, name=domain))
|
2013-01-10 21:50:09 +01:00
|
|
|
existing_realms.add(domain)
|
2013-03-27 15:58:23 +01:00
|
|
|
Realm.objects.bulk_create(realms_to_create)
|
2013-01-10 21:50:09 +01:00
|
|
|
|
2016-05-18 20:23:03 +02:00
|
|
|
def bulk_create_users(realms, users_raw, bot_type=None):
|
2016-06-13 06:38:14 +02:00
|
|
|
# type: (Mapping[text_type, Realm], Set[Tuple[text_type, text_type, text_type, bool]], Optional[int]) -> None
|
2013-01-10 21:50:09 +01:00
|
|
|
"""
|
2013-04-01 16:57:50 +02:00
|
|
|
Creates and saves a UserProfile with the given email.
|
2013-01-10 21:50:09 +01:00
|
|
|
Has some code based off of UserManage.create_user, but doesn't .save()
|
|
|
|
"""
|
|
|
|
users = []
|
2013-04-01 16:57:50 +02:00
|
|
|
existing_users = set(u.email for u in UserProfile.objects.all())
|
2013-01-10 21:50:09 +01:00
|
|
|
for (email, full_name, short_name, active) in users_raw:
|
|
|
|
if email in existing_users:
|
|
|
|
continue
|
|
|
|
users.append((email, full_name, short_name, active))
|
|
|
|
existing_users.add(email)
|
2016-04-13 04:49:13 +02:00
|
|
|
users = sorted(users)
|
2013-01-10 21:50:09 +01:00
|
|
|
|
|
|
|
# Now create user_profiles
|
|
|
|
profiles_to_create = []
|
|
|
|
for (email, full_name, short_name, active) in users:
|
2016-06-13 06:38:14 +02:00
|
|
|
domain = resolve_email_to_domain(email)
|
2013-04-01 16:57:50 +02:00
|
|
|
profile = create_user_profile(realms[domain], email,
|
2016-05-18 20:23:03 +02:00
|
|
|
initial_password(email), active, bot_type,
|
2014-01-07 18:57:54 +01:00
|
|
|
full_name, short_name, None, False)
|
2013-01-10 21:50:09 +01:00
|
|
|
profiles_to_create.append(profile)
|
2013-03-27 15:58:23 +01:00
|
|
|
UserProfile.objects.bulk_create(profiles_to_create)
|
2013-01-10 21:50:09 +01:00
|
|
|
|
|
|
|
profiles_by_email = {}
|
|
|
|
profiles_by_id = {}
|
|
|
|
for profile in UserProfile.objects.select_related().all():
|
2013-03-28 20:43:34 +01:00
|
|
|
profiles_by_email[profile.email] = profile
|
2013-04-01 17:07:41 +02:00
|
|
|
profiles_by_id[profile.id] = profile
|
2013-01-10 21:50:09 +01:00
|
|
|
|
|
|
|
recipients_to_create = []
|
2016-06-04 00:52:29 +02:00
|
|
|
for (email, full_name, short_name, active) in users:
|
2013-01-10 21:50:09 +01:00
|
|
|
recipients_to_create.append(Recipient(type_id=profiles_by_email[email].id,
|
|
|
|
type=Recipient.PERSONAL))
|
2013-03-27 15:58:23 +01:00
|
|
|
Recipient.objects.bulk_create(recipients_to_create)
|
2013-01-10 21:50:09 +01:00
|
|
|
|
|
|
|
recipients_by_email = {}
|
|
|
|
for recipient in Recipient.objects.filter(type=Recipient.PERSONAL):
|
2013-03-28 20:43:34 +01:00
|
|
|
recipients_by_email[profiles_by_id[recipient.type_id].email] = recipient
|
2013-01-10 21:50:09 +01:00
|
|
|
|
|
|
|
subscriptions_to_create = []
|
2016-06-04 00:52:29 +02:00
|
|
|
for (email, full_name, short_name, active) in users:
|
2013-01-10 21:50:09 +01:00
|
|
|
subscriptions_to_create.append(
|
|
|
|
Subscription(user_profile_id=profiles_by_email[email].id,
|
|
|
|
recipient=recipients_by_email[email]))
|
2013-03-27 15:58:23 +01:00
|
|
|
Subscription.objects.bulk_create(subscriptions_to_create)
|
2013-01-10 21:50:09 +01:00
|
|
|
|
|
|
|
def bulk_create_streams(realms, stream_list):
|
2016-06-13 06:38:14 +02:00
|
|
|
# type: (Mapping[text_type, Realm], Iterable[Tuple[text_type, text_type]]) -> None
|
2013-01-10 21:50:09 +01:00
|
|
|
existing_streams = set((stream.realm.domain, stream.name.lower())
|
|
|
|
for stream in Stream.objects.select_related().all())
|
|
|
|
streams_to_create = []
|
|
|
|
for (domain, name) in stream_list:
|
|
|
|
if (domain, name.lower()) not in existing_streams:
|
|
|
|
streams_to_create.append(Stream(realm=realms[domain], name=name))
|
2013-03-27 15:58:23 +01:00
|
|
|
Stream.objects.bulk_create(streams_to_create)
|
2013-01-10 21:50:09 +01:00
|
|
|
|
|
|
|
recipients_to_create = []
|
|
|
|
for stream in Stream.objects.select_related().all():
|
|
|
|
if (stream.realm.domain, stream.name.lower()) not in existing_streams:
|
|
|
|
recipients_to_create.append(Recipient(type_id=stream.id,
|
|
|
|
type=Recipient.STREAM))
|
2013-03-27 15:58:23 +01:00
|
|
|
Recipient.objects.bulk_create(recipients_to_create)
|
2013-01-10 21:50:09 +01:00
|
|
|
|
|
|
|
def bulk_create_clients(client_list):
|
2016-06-13 06:38:14 +02:00
|
|
|
# type: (Iterable[text_type]) -> None
|
2013-01-10 21:50:09 +01:00
|
|
|
existing_clients = set(client.name for client in Client.objects.select_related().all())
|
|
|
|
|
|
|
|
clients_to_create = []
|
|
|
|
for name in client_list:
|
|
|
|
if name not in existing_clients:
|
|
|
|
clients_to_create.append(Client(name=name))
|
|
|
|
existing_clients.add(name)
|
2013-03-27 15:58:23 +01:00
|
|
|
Client.objects.bulk_create(clients_to_create)
|
2013-01-10 21:50:09 +01:00
|
|
|
|
|
|
|
def bulk_create_huddles(users, huddle_user_list):
|
2016-06-13 06:38:14 +02:00
|
|
|
# type: (Dict[text_type, UserProfile], Iterable[Iterable[text_type]]) -> None
|
2013-01-10 21:50:09 +01:00
|
|
|
huddles = {}
|
|
|
|
huddles_by_id = {}
|
|
|
|
huddle_set = set()
|
|
|
|
existing_huddles = set()
|
|
|
|
for huddle in Huddle.objects.all():
|
|
|
|
existing_huddles.add(huddle.huddle_hash)
|
|
|
|
for huddle_users in huddle_user_list:
|
|
|
|
user_ids = [users[email].id for email in huddle_users]
|
|
|
|
huddle_hash = get_huddle_hash(user_ids)
|
|
|
|
if huddle_hash in existing_huddles:
|
|
|
|
continue
|
|
|
|
huddle_set.add((huddle_hash, tuple(sorted(user_ids))))
|
|
|
|
|
|
|
|
huddles_to_create = []
|
|
|
|
for (huddle_hash, _) in huddle_set:
|
|
|
|
huddles_to_create.append(Huddle(huddle_hash=huddle_hash))
|
2013-03-27 15:58:23 +01:00
|
|
|
Huddle.objects.bulk_create(huddles_to_create)
|
2013-01-10 21:50:09 +01:00
|
|
|
|
|
|
|
for huddle in Huddle.objects.all():
|
|
|
|
huddles[huddle.huddle_hash] = huddle
|
|
|
|
huddles_by_id[huddle.id] = huddle
|
|
|
|
|
|
|
|
recipients_to_create = []
|
|
|
|
for (huddle_hash, _) in huddle_set:
|
|
|
|
recipients_to_create.append(Recipient(type_id=huddles[huddle_hash].id, type=Recipient.HUDDLE))
|
2013-03-27 15:58:23 +01:00
|
|
|
Recipient.objects.bulk_create(recipients_to_create)
|
2013-01-10 21:50:09 +01:00
|
|
|
|
|
|
|
huddle_recipients = {}
|
|
|
|
for recipient in Recipient.objects.filter(type=Recipient.HUDDLE):
|
|
|
|
huddle_recipients[huddles_by_id[recipient.type_id].huddle_hash] = recipient
|
|
|
|
|
|
|
|
subscriptions_to_create = []
|
|
|
|
for (huddle_hash, huddle_user_ids) in huddle_set:
|
|
|
|
for user_id in huddle_user_ids:
|
|
|
|
subscriptions_to_create.append(Subscription(active=True, user_profile_id=user_id,
|
|
|
|
recipient=huddle_recipients[huddle_hash]))
|
2013-03-27 15:58:23 +01:00
|
|
|
Subscription.objects.bulk_create(subscriptions_to_create)
|