mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	This legacy cross-realm bot hasn't been used in several years, as far as I know. If we wanted to re-introduce it, I'd want to implement it as an embedded bot using those common APIs, rather than the totally custom hacky code used for it that involves unnecessary queue workers and similar details. Fixes #13533.
		
			
				
	
	
		
			56 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			56 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
from argparse import ArgumentParser
 | 
						|
from typing import Any, Iterable, Optional, Tuple
 | 
						|
 | 
						|
from django.conf import settings
 | 
						|
from django.core.management.base import BaseCommand
 | 
						|
 | 
						|
from zerver.lib.actions import do_change_is_admin
 | 
						|
from zerver.lib.bulk_create import bulk_create_users
 | 
						|
from zerver.models import Realm, UserProfile, email_to_username, get_client, \
 | 
						|
    get_system_bot
 | 
						|
 | 
						|
settings.TORNADO_SERVER = None
 | 
						|
 | 
						|
def create_users(realm: Realm, name_list: Iterable[Tuple[str, str]], bot_type: Optional[int]=None) -> None:
 | 
						|
    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))
 | 
						|
    bulk_create_users(realm, user_set, bot_type)
 | 
						|
 | 
						|
class Command(BaseCommand):
 | 
						|
    help = "Populate an initial database for Zulip Voyager"
 | 
						|
 | 
						|
    def add_arguments(self, parser: ArgumentParser) -> None:
 | 
						|
        parser.add_argument('--extra-users',
 | 
						|
                            dest='extra_users',
 | 
						|
                            type=int,
 | 
						|
                            default=0,
 | 
						|
                            help='The number of extra users to create')
 | 
						|
 | 
						|
    def handle(self, *args: Any, **options: Any) -> None:
 | 
						|
        if Realm.objects.count() > 0:
 | 
						|
            print("Database already initialized; doing nothing.")
 | 
						|
            return
 | 
						|
        realm = Realm.objects.create(string_id=settings.SYSTEM_BOT_REALM)
 | 
						|
 | 
						|
        get_client("website")
 | 
						|
        get_client("API")
 | 
						|
 | 
						|
        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)
 | 
						|
        # Set the owners for these bots to the bots themselves
 | 
						|
        bots = UserProfile.objects.filter(email__in=[bot_info[1] for bot_info in internal_bots])
 | 
						|
        for bot in bots:
 | 
						|
            bot.bot_owner = bot
 | 
						|
            bot.save()
 | 
						|
 | 
						|
        # Initialize the email gateway bot as an API Super User
 | 
						|
        email_gateway_bot = get_system_bot(settings.EMAIL_GATEWAY_BOT)
 | 
						|
        do_change_is_admin(email_gateway_bot, True, permission="api_super_user")
 | 
						|
 | 
						|
        self.stdout.write("Successfully populated database with initial data.\n")
 | 
						|
        self.stdout.write("Please run ./manage.py generate_realm_creation_link "
 | 
						|
                          "to generate link for creating organization")
 |