bulk_create: Refactor bulk_create_users to take Realm instead of domains.

Previously bulk_create_users would figure out a user's realm from their
email domain. Now require that a realm be passed explicitly.
This commit is contained in:
Rishi Gupta
2016-12-15 08:25:59 -08:00
committed by Tim Abbott
parent 84bb73ab65
commit fc188de8e0
3 changed files with 19 additions and 24 deletions

View File

@@ -18,8 +18,8 @@ def bulk_create_realms(realm_list):
existing_realms.add(domain)
Realm.objects.bulk_create(realms_to_create)
def bulk_create_users(realms, users_raw, bot_type=None, tos_version=None):
# type: (Mapping[text_type, Realm], Set[Tuple[text_type, text_type, text_type, bool]], Optional[int], Optional[text_type]) -> None
def bulk_create_users(realm, users_raw, bot_type=None, tos_version=None):
# type: (Realm, Set[Tuple[text_type, text_type, text_type, bool]], Optional[int], Optional[text_type]) -> None
"""
Creates and saves a UserProfile with the given email.
Has some code based off of UserManage.create_user, but doesn't .save()
@@ -36,8 +36,7 @@ def bulk_create_users(realms, users_raw, bot_type=None, tos_version=None):
# Now create user_profiles
profiles_to_create = [] # type: List[UserProfile]
for (email, full_name, short_name, active) in users:
domain = email_to_domain(email)
profile = create_user_profile(realms[domain], email,
profile = create_user_profile(realm, email,
initial_password(email), active, bot_type,
full_name, short_name, None, False, tos_version)
profiles_to_create.append(profile)

View File

@@ -15,17 +15,13 @@ from argparse import ArgumentParser
settings.TORNADO_SERVER = None
def create_users(name_list, bot_type=None):
# type: (Iterable[Tuple[Text, Text]], int) -> None
realms = {}
for realm in Realm.objects.all():
realms[realm.domain] = realm
def create_users(realm, name_list, bot_type=None):
# type: (Realm, Iterable[Tuple[Text, Text]], int) -> 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(realms, user_set, bot_type)
bulk_create_users(realm, user_set, bot_type)
class Command(BaseCommand):
help = "Populate an initial database for Zulip Voyager"
@@ -40,18 +36,18 @@ class Command(BaseCommand):
def handle(self, *args, **options):
# type: (*Any, **Any) -> None
Realm.objects.create(string_id=settings.INTERNAL_BOT_DOMAIN.split('.')[0],
realm = Realm.objects.create(string_id=settings.INTERNAL_BOT_DOMAIN.split('.')[0],
domain=settings.INTERNAL_BOT_DOMAIN)
names = [(settings.FEEDBACK_BOT_NAME, settings.FEEDBACK_BOT)]
create_users(names, bot_type=UserProfile.DEFAULT_BOT)
create_users(realm, names, bot_type=UserProfile.DEFAULT_BOT)
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(internal_bots, bot_type=UserProfile.DEFAULT_BOT)
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:

View File

@@ -27,14 +27,14 @@ from typing import Any, Callable, Dict, List, Iterable, Mapping, Sequence, Set,
settings.TORNADO_SERVER = None
def create_users(realms, name_list, bot_type=None):
# type: (Mapping[Text, Realm], Iterable[Tuple[Text, Text]], int) -> None
def create_users(realm, name_list, bot_type=None):
# type: (Realm, Iterable[Tuple[Text, Text]], int) -> None
user_set = set() # type: Set[Tuple[Text, Text, Text, bool]]
for full_name, email in name_list:
short_name = email_to_username(email)
user_set.add((email, full_name, short_name, True))
tos_version = settings.TOS_VERSION if bot_type is None else None
bulk_create_users(realms, user_set, bot_type=bot_type, tos_version=tos_version)
bulk_create_users(realm, user_set, bot_type=bot_type, tos_version=tos_version)
class Command(BaseCommand):
@@ -140,7 +140,7 @@ class Command(BaseCommand):
]
for i in range(options["extra_users"]):
names.append(('Extra User %d' % (i,), 'extrauser%d@zulip.com' % (i,)))
create_users(realms, names)
create_users(zulip_realm, names)
iago = UserProfile.objects.get(email="iago@zulip.com")
do_change_is_admin(iago, True)
# Create public streams.
@@ -228,7 +228,7 @@ class Command(BaseCommand):
("Athena Consulting Exchange User (MIT)", "starnine@mit.edu"),
("Esp Classroom (MIT)", "espuser@mit.edu"),
]
create_users(realms, testsuite_mit_users)
create_users(mit_realm, testsuite_mit_users)
# These bots are directly referenced from code and thus
# are needed for the test suite.
@@ -240,12 +240,12 @@ class Command(BaseCommand):
("Zulip Default Bot", "default-bot@zulip.com"),
]
zulip_realm_bots.extend(all_realm_bots)
create_users(realms, zulip_realm_bots, bot_type=UserProfile.DEFAULT_BOT)
create_users(zulip_realm, zulip_realm_bots, bot_type=UserProfile.DEFAULT_BOT)
zulip_webhook_bots = [
("Zulip Webhook Bot", "webhook-bot@zulip.com"),
]
create_users(realms, zulip_webhook_bots, bot_type=UserProfile.INCOMING_WEBHOOK_BOT)
create_users(zulip_realm, zulip_webhook_bots, bot_type=UserProfile.INCOMING_WEBHOOK_BOT)
if not options["test_suite"]:
# Initialize the email gateway bot as an API Super User
@@ -296,12 +296,12 @@ class Command(BaseCommand):
("Zulip Trac Bot", "trac-bot@zulip.com"),
("Zulip Nagios Bot", "nagios-bot@zulip.com"),
]
create_users(realms, internal_zulip_users_nosubs, bot_type=UserProfile.DEFAULT_BOT)
create_users(zulip_realm, internal_zulip_users_nosubs, bot_type=UserProfile.DEFAULT_BOT)
zulip_cross_realm_bots = [
("Zulip Feedback Bot", "feedback@zulip.com"),
]
create_users(realms, zulip_cross_realm_bots, bot_type=UserProfile.DEFAULT_BOT)
create_users(zulip_realm, zulip_cross_realm_bots, bot_type=UserProfile.DEFAULT_BOT)
# Mark all messages as read
UserMessage.objects.all().update(flags=UserMessage.flags.read)