2017-11-16 00:43:27 +01:00
|
|
|
from argparse import ArgumentParser
|
2020-01-28 15:11:06 +01:00
|
|
|
from typing import Any
|
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
|
|
|
|
|
2019-07-13 00:09:11 +02:00
|
|
|
from zerver.lib.actions import do_change_is_admin
|
2020-01-28 15:11:06 +01:00
|
|
|
from zerver.lib.server_initialization import create_users
|
|
|
|
from zerver.models import Realm, UserProfile, get_client, \
|
2020-01-14 21:59:46 +01:00
|
|
|
get_system_bot
|
2013-11-04 16:54:15 +01:00
|
|
|
|
|
|
|
settings.TORNADO_SERVER = None
|
|
|
|
|
|
|
|
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
|
2019-07-24 08:37:13 +02:00
|
|
|
realm = Realm.objects.create(string_id=settings.SYSTEM_BOT_REALM)
|
2013-11-04 16:54:15 +01:00
|
|
|
|
2020-01-28 14:45:35 +01:00
|
|
|
# Create the "website" and "API" clients:
|
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)
|
2019-07-13 00:09:11 +02:00
|
|
|
do_change_is_admin(email_gateway_bot, True, permission="api_super_user")
|
2016-02-08 03:59:38 +01:00
|
|
|
|
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")
|