2013-11-04 16:54:15 +01:00
|
|
|
|
2017-11-16 00:43:27 +01:00
|
|
|
from argparse import ArgumentParser
|
2018-05-10 19:30:04 +02:00
|
|
|
from typing import Any, Iterable, Tuple, Optional
|
2016-06-04 16:52:18 +02:00
|
|
|
|
2017-11-16 00:43:27 +01:00
|
|
|
from django.conf import settings
|
2013-11-04 16:54:15 +01:00
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
|
2013-11-14 16:59:10 +01:00
|
|
|
from zerver.lib.bulk_create import bulk_create_users
|
2017-11-16 00:43:27 +01:00
|
|
|
from zerver.models import Realm, UserProfile, \
|
|
|
|
email_to_username, get_client, get_system_bot
|
2013-11-04 16:54:15 +01:00
|
|
|
|
|
|
|
settings.TORNADO_SERVER = None
|
|
|
|
|
2018-05-10 19:30:04 +02:00
|
|
|
def create_users(realm: Realm, name_list: Iterable[Tuple[str, str]], bot_type: Optional[int]=None) -> None:
|
2013-11-04 16:54:15 +01:00
|
|
|
user_set = set()
|
|
|
|
for full_name, email in name_list:
|
|
|
|
short_name = email_to_username(email)
|
|
|
|
user_set.add((email, full_name, short_name, True))
|
2016-12-15 17:25:59 +01:00
|
|
|
bulk_create_users(realm, user_set, bot_type)
|
2013-11-04 16:54:15 +01:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2015-08-21 11:56:54 +02:00
|
|
|
help = "Populate an initial database for Zulip Voyager"
|
2013-11-04 16:54:15 +01:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2016-10-16 00:29:57 +02:00
|
|
|
parser.add_argument('--extra-users',
|
|
|
|
dest='extra_users',
|
|
|
|
type=int,
|
|
|
|
default=0,
|
|
|
|
help='The number of extra users to create')
|
2013-11-04 16:54:15 +01:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2018-05-17 20:35:17 +02:00
|
|
|
if Realm.objects.count() > 0:
|
|
|
|
print("Database already initialized; doing nothing.")
|
|
|
|
return
|
2017-03-14 00:51:51 +01:00
|
|
|
realm = Realm.objects.create(string_id=settings.INTERNAL_BOT_DOMAIN.split('.')[0])
|
2013-11-04 16:54:15 +01:00
|
|
|
|
2013-11-13 16:59:27 +01:00
|
|
|
names = [(settings.FEEDBACK_BOT_NAME, settings.FEEDBACK_BOT)]
|
2016-12-15 17:25:59 +01:00
|
|
|
create_users(realm, names, bot_type=UserProfile.DEFAULT_BOT)
|
2013-11-04 16:54:15 +01:00
|
|
|
|
|
|
|
get_client("website")
|
|
|
|
get_client("API")
|
|
|
|
|
2013-11-14 16:59:10 +01:00
|
|
|
internal_bots = [(bot['name'], bot['email_template'] % (settings.INTERNAL_BOT_DOMAIN,))
|
|
|
|
for bot in settings.INTERNAL_BOTS]
|
2016-12-15 17:25:59 +01:00
|
|
|
create_users(realm, internal_bots, bot_type=UserProfile.DEFAULT_BOT)
|
2013-11-04 23:31:08 +01:00
|
|
|
# Set the owners for these bots to the bots themselves
|
2013-11-14 16:59:10 +01:00
|
|
|
bots = UserProfile.objects.filter(email__in=[bot_info[1] for bot_info in internal_bots])
|
2013-11-04 23:31:08 +01:00
|
|
|
for bot in bots:
|
|
|
|
bot.bot_owner = bot
|
|
|
|
bot.save()
|
2013-11-04 16:54:15 +01:00
|
|
|
|
2016-02-08 03:59:38 +01:00
|
|
|
# Initialize the email gateway bot as an API Super User
|
2017-08-07 20:23:35 +02:00
|
|
|
email_gateway_bot = get_system_bot(settings.EMAIL_GATEWAY_BOT)
|
2016-02-08 03:59:38 +01:00
|
|
|
email_gateway_bot.is_api_super_user = True
|
|
|
|
email_gateway_bot.save()
|
|
|
|
|
2013-11-10 17:24:37 +01:00
|
|
|
self.stdout.write("Successfully populated database with initial data.\n")
|
2017-10-27 02:47:30 +02:00
|
|
|
self.stdout.write("Please run ./manage.py generate_realm_creation_link "
|
|
|
|
"to generate link for creating organization")
|