2013-11-04 16:54:15 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any, Iterable, Tuple
|
|
|
|
|
2013-11-04 16:54:15 +01:00
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
|
|
|
|
from django.contrib.sites.models import Site
|
|
|
|
from zerver.models import UserProfile, Stream, Recipient, \
|
|
|
|
Subscription, Realm, get_client, email_to_username
|
|
|
|
from django.conf import settings
|
2013-11-14 16:59:10 +01:00
|
|
|
from zerver.lib.bulk_create import bulk_create_users
|
2013-11-14 16:06:12 +01:00
|
|
|
from zerver.lib.actions import set_default_streams, do_create_realm
|
2013-11-04 16:54:15 +01:00
|
|
|
|
|
|
|
from optparse import make_option
|
2016-06-04 00:52:29 +02:00
|
|
|
from six import text_type
|
2013-11-04 16:54:15 +01:00
|
|
|
|
|
|
|
settings.TORNADO_SERVER = None
|
|
|
|
|
2016-05-18 20:23:03 +02:00
|
|
|
def create_users(name_list, bot_type=None):
|
2016-06-13 06:38:14 +02:00
|
|
|
# type: (Iterable[Tuple[text_type, text_type]], int) -> None
|
2013-11-14 16:59:10 +01:00
|
|
|
realms = {}
|
|
|
|
for realm in Realm.objects.all():
|
|
|
|
realms[realm.domain] = realm
|
|
|
|
|
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-05-18 20:23:03 +02:00
|
|
|
bulk_create_users(realms, 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
|
|
|
|
|
|
|
option_list = BaseCommand.option_list + (
|
|
|
|
make_option('--extra-users',
|
|
|
|
dest='extra_users',
|
|
|
|
type='int',
|
|
|
|
default=0,
|
|
|
|
help='The number of extra users to create'),
|
|
|
|
)
|
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
def handle(self, *args, **options):
|
|
|
|
# type: (*Any, **Any) -> None
|
2016-08-23 06:36:15 +02:00
|
|
|
Realm.objects.create(domain=settings.INTERNAL_BOT_DOMAIN)
|
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-05-18 20:23:03 +02:00
|
|
|
create_users(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-05-18 20:23:03 +02:00
|
|
|
create_users(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
|
|
|
|
email_gateway_bot = UserProfile.objects.get(email__iexact=settings.EMAIL_GATEWAY_BOT)
|
|
|
|
email_gateway_bot.is_api_super_user = True
|
|
|
|
email_gateway_bot.save()
|
|
|
|
|
2013-11-14 16:59:10 +01:00
|
|
|
(admin_realm, _) = do_create_realm(settings.ADMIN_DOMAIN,
|
|
|
|
settings.ADMIN_DOMAIN, True)
|
|
|
|
|
2016-06-17 03:53:40 +02:00
|
|
|
set_default_streams(admin_realm, settings.DEFAULT_NEW_REALM_STREAMS)
|
2013-11-14 03:42:21 +01:00
|
|
|
|
2013-11-10 17:24:37 +01:00
|
|
|
self.stdout.write("Successfully populated database with initial data.\n")
|
2013-11-04 16:54:15 +01:00
|
|
|
|
|
|
|
site = Site.objects.get_current()
|
2013-11-13 22:33:43 +01:00
|
|
|
site.domain = settings.EXTERNAL_HOST
|
2013-11-04 16:54:15 +01:00
|
|
|
site.save()
|