2017-03-03 19:01:52 +01:00
|
|
|
from typing import Any, Dict, Iterable, List, Mapping, Optional, Set, Tuple, Text
|
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, \
|
2017-02-15 04:35:10 +01:00
|
|
|
Subscription, Recipient, Client, RealmAuditLog, get_huddle_hash
|
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
|
|
|
|
2017-11-03 03:12:25 +01:00
|
|
|
def bulk_create_users(realm, users_raw, bot_type=None, bot_owner=None, tos_version=None, timezone=""):
|
2017-09-26 11:34:31 +02:00
|
|
|
# type: (Realm, Set[Tuple[Text, Text, Text, bool]], Optional[int], Optional[UserProfile], Optional[Text], Text) -> 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()
|
|
|
|
"""
|
2016-12-15 17:37:15 +01:00
|
|
|
existing_users = frozenset(UserProfile.objects.values_list('email', flat=True))
|
|
|
|
users = sorted([user_raw for user_raw in users_raw if user_raw[0] not in existing_users])
|
2013-01-10 21:50:09 +01:00
|
|
|
|
|
|
|
# Now create user_profiles
|
2017-05-07 17:01:57 +02:00
|
|
|
profiles_to_create = [] # type: List[UserProfile]
|
2013-01-10 21:50:09 +01:00
|
|
|
for (email, full_name, short_name, active) in users:
|
2016-12-15 17:25:59 +01:00
|
|
|
profile = create_user_profile(realm, email,
|
2016-05-18 20:23:03 +02:00
|
|
|
initial_password(email), active, bot_type,
|
2017-09-26 11:34:31 +02:00
|
|
|
full_name, short_name, bot_owner, False, tos_version,
|
2017-05-04 15:19:06 +02:00
|
|
|
timezone, tutorial_status=UserProfile.TUTORIAL_FINISHED,
|
2017-03-29 23:21:44 +02:00
|
|
|
enter_sends=True)
|
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
|
|
|
|
2017-02-15 04:35:10 +01:00
|
|
|
RealmAuditLog.objects.bulk_create(
|
|
|
|
[RealmAuditLog(realm=profile_.realm, modified_user=profile_,
|
|
|
|
event_type='user_created', event_time=profile_.date_joined)
|
|
|
|
for profile_ in profiles_to_create])
|
|
|
|
|
2017-05-07 17:01:57 +02:00
|
|
|
profiles_by_email = {} # type: Dict[Text, UserProfile]
|
|
|
|
profiles_by_id = {} # type: Dict[int, UserProfile]
|
2013-01-10 21:50:09 +01:00
|
|
|
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
|
|
|
|
2017-05-07 17:01:57 +02:00
|
|
|
recipients_to_create = [] # type: List[Recipient]
|
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
|
|
|
|
2017-05-07 17:01:57 +02:00
|
|
|
recipients_by_email = {} # type: Dict[Text, Recipient]
|
2013-01-10 21:50:09 +01:00
|
|
|
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
|
|
|
|
2017-05-07 17:01:57 +02:00
|
|
|
subscriptions_to_create = [] # type: List[Subscription]
|
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
|
|
|
|
2016-12-15 16:57:21 +01:00
|
|
|
def bulk_create_streams(realm, stream_dict):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Realm, Dict[Text, Dict[Text, Any]]) -> None
|
2016-12-15 02:38:14 +01:00
|
|
|
existing_streams = frozenset([name.lower() for name in
|
|
|
|
Stream.objects.filter(realm=realm)
|
|
|
|
.values_list('name', flat=True)])
|
2017-05-07 17:01:57 +02:00
|
|
|
streams_to_create = [] # type: List[Stream]
|
2016-12-08 00:02:21 +01:00
|
|
|
for name, options in stream_dict.items():
|
2016-12-15 16:57:21 +01:00
|
|
|
if name.lower() not in existing_streams:
|
2016-12-08 00:02:21 +01:00
|
|
|
streams_to_create.append(
|
|
|
|
Stream(
|
2016-12-15 16:57:21 +01:00
|
|
|
realm=realm, name=name, description=options["description"],
|
2017-10-08 21:16:51 +02:00
|
|
|
invite_only=options["invite_only"],
|
|
|
|
is_in_zephyr_realm=realm.is_zephyr_mirror_realm,
|
2016-12-08 00:02:21 +01:00
|
|
|
)
|
|
|
|
)
|
2017-07-16 22:46:34 +02:00
|
|
|
# Sort streams by name before creating them so that we can have a
|
|
|
|
# reliable ordering of `stream_id` across different python versions.
|
|
|
|
# This is required for test fixtures which contain `stream_id`. Prior
|
|
|
|
# to python 3.3 hashes were not randomized but after a security fix
|
|
|
|
# hash randomization was enabled in python 3.3 which made iteration
|
|
|
|
# of dictionaries and sets completely unpredictable. Here the order
|
|
|
|
# of elements while iterating `stream_dict` will be completely random
|
|
|
|
# for python 3.3 and later versions.
|
|
|
|
streams_to_create.sort(key=lambda x: x.name)
|
2013-03-27 15:58:23 +01:00
|
|
|
Stream.objects.bulk_create(streams_to_create)
|
2013-01-10 21:50:09 +01:00
|
|
|
|
2017-05-07 17:01:57 +02:00
|
|
|
recipients_to_create = [] # type: List[Recipient]
|
2016-12-15 02:38:14 +01:00
|
|
|
for stream in Stream.objects.filter(realm=realm).values('id', 'name'):
|
|
|
|
if stream['name'].lower() not in existing_streams:
|
|
|
|
recipients_to_create.append(Recipient(type_id=stream['id'],
|
2013-01-10 21:50:09 +01:00
|
|
|
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-12-21 13:17:53 +01:00
|
|
|
# type: (Iterable[Text]) -> None
|
2017-05-07 17:01:57 +02:00
|
|
|
existing_clients = set(client.name for client in Client.objects.select_related().all()) # type: Set[Text]
|
2013-01-10 21:50:09 +01:00
|
|
|
|
2017-05-07 17:01:57 +02:00
|
|
|
clients_to_create = [] # type: List[Client]
|
2013-01-10 21:50:09 +01:00
|
|
|
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-12-21 13:17:53 +01:00
|
|
|
# type: (Dict[Text, UserProfile], Iterable[Iterable[Text]]) -> None
|
2017-05-07 17:01:57 +02:00
|
|
|
huddles = {} # type: Dict[Text, Huddle]
|
|
|
|
huddles_by_id = {} # type: Dict[int, Huddle]
|
|
|
|
huddle_set = set() # type: Set[Tuple[Text, Tuple[int, ...]]]
|
|
|
|
existing_huddles = set() # type: Set[Text]
|
2013-01-10 21:50:09 +01:00
|
|
|
for huddle in Huddle.objects.all():
|
|
|
|
existing_huddles.add(huddle.huddle_hash)
|
|
|
|
for huddle_users in huddle_user_list:
|
2017-05-07 17:01:57 +02:00
|
|
|
user_ids = [users[email].id for email in huddle_users] # type: List[int]
|
2013-01-10 21:50:09 +01:00
|
|
|
huddle_hash = get_huddle_hash(user_ids)
|
|
|
|
if huddle_hash in existing_huddles:
|
|
|
|
continue
|
|
|
|
huddle_set.add((huddle_hash, tuple(sorted(user_ids))))
|
|
|
|
|
2017-05-07 17:01:57 +02:00
|
|
|
huddles_to_create = [] # type: List[Huddle]
|
2013-01-10 21:50:09 +01:00
|
|
|
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
|
|
|
|
|
2017-05-07 17:01:57 +02:00
|
|
|
recipients_to_create = [] # type: List[Recipient]
|
2013-01-10 21:50:09 +01:00
|
|
|
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
|
|
|
|
2017-05-07 17:01:57 +02:00
|
|
|
huddle_recipients = {} # type: Dict[Text, Recipient]
|
2013-01-10 21:50:09 +01:00
|
|
|
for recipient in Recipient.objects.filter(type=Recipient.HUDDLE):
|
|
|
|
huddle_recipients[huddles_by_id[recipient.type_id].huddle_hash] = recipient
|
|
|
|
|
2017-05-07 17:01:57 +02:00
|
|
|
subscriptions_to_create = [] # type: List[Subscription]
|
2013-01-10 21:50:09 +01:00
|
|
|
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)
|