2020-06-11 00:54:34 +02:00
|
|
|
from typing import Iterable, Optional, Tuple
|
|
|
|
|
2020-01-27 16:12:43 +01:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
from zerver.lib.bulk_create import bulk_create_users
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import Realm, UserProfile, email_to_username, get_client, get_system_bot
|
2020-01-27 16:12:43 +01:00
|
|
|
|
|
|
|
|
2020-01-30 14:43:46 +01:00
|
|
|
def server_initialized() -> bool:
|
2020-02-12 16:39:12 +01:00
|
|
|
return Realm.objects.exists()
|
2020-01-30 14:43:46 +01:00
|
|
|
|
2020-01-27 16:12:43 +01:00
|
|
|
def create_internal_realm() -> None:
|
2020-05-21 00:13:06 +02:00
|
|
|
from zerver.lib.actions import do_change_is_api_super_user
|
2020-02-12 16:39:12 +01:00
|
|
|
|
2020-01-28 14:50:18 +01:00
|
|
|
realm = Realm.objects.create(string_id=settings.SYSTEM_BOT_REALM)
|
2020-01-27 16:12:43 +01:00
|
|
|
|
2020-02-25 02:38:11 +01:00
|
|
|
# Create some client objects for common requests. Not required;
|
|
|
|
# just ensures these get low IDs in production, and in development
|
|
|
|
# avoids an extra database write for the first HTTP requset in
|
|
|
|
# most tests.
|
2020-01-27 16:21:32 +01:00
|
|
|
get_client("website")
|
2020-02-25 02:38:11 +01:00
|
|
|
get_client("ZulipMobile")
|
|
|
|
get_client("ZulipElectron")
|
2020-01-27 16:21:32 +01:00
|
|
|
|
2020-01-28 14:50:18 +01:00
|
|
|
internal_bots = [(bot['name'], bot['email_template'] % (settings.INTERNAL_BOT_DOMAIN,))
|
|
|
|
for bot in settings.INTERNAL_BOTS]
|
|
|
|
create_users(realm, internal_bots, bot_type=UserProfile.DEFAULT_BOT)
|
2020-01-28 14:41:08 +01:00
|
|
|
# Set the owners for these bots to the bots themselves
|
2020-01-28 14:50:18 +01:00
|
|
|
bots = UserProfile.objects.filter(email__in=[bot_info[1] for bot_info in internal_bots])
|
2020-01-28 14:41:08 +01:00
|
|
|
for bot in bots:
|
|
|
|
bot.bot_owner = bot
|
|
|
|
bot.save()
|
2020-01-27 16:12:43 +01:00
|
|
|
|
|
|
|
# Initialize the email gateway bot as an API Super User
|
|
|
|
email_gateway_bot = get_system_bot(settings.EMAIL_GATEWAY_BOT)
|
2020-05-21 00:13:06 +02:00
|
|
|
do_change_is_api_super_user(email_gateway_bot, True)
|
2020-01-27 16:12:43 +01:00
|
|
|
|
|
|
|
def create_users(realm: Realm, name_list: Iterable[Tuple[str, str]],
|
2020-01-27 18:55:11 +01:00
|
|
|
tos_version: Optional[str]=None,
|
2020-01-27 16:12:43 +01:00
|
|
|
bot_type: Optional[int]=None,
|
|
|
|
bot_owner: Optional[UserProfile]=None) -> None:
|
2020-01-28 14:39:19 +01:00
|
|
|
user_set = set()
|
2020-01-27 16:12:43 +01:00
|
|
|
for full_name, email in name_list:
|
|
|
|
short_name = email_to_username(email)
|
|
|
|
user_set.add((email, full_name, short_name, True))
|
|
|
|
bulk_create_users(realm, user_set, bot_type=bot_type, bot_owner=bot_owner, tos_version=tos_version)
|