def create_user(**kwargs: Any) -> UserProfile: self.assertEqual(kwargs['full_name'], 'fred') self.assertEqual(kwargs['email'], email) self.assertEqual(kwargs['active'], False) self.assertEqual(kwargs['is_mirror_dummy'], True) # We create an actual user here to simulate a race. # We use the minimal, un-mocked function. kwargs['bot_type'] = None kwargs['bot_owner'] = None kwargs['tos_version'] = None kwargs['timezone'] = timezone_now() create_user_profile(**kwargs).save() raise IntegrityError()
def create_user(**kwargs: Any) -> UserProfile: self.assertEqual(kwargs["full_name"], "fred") self.assertEqual(kwargs["email"], email) self.assertEqual(kwargs["active"], False) self.assertEqual(kwargs["is_mirror_dummy"], True) # We create an actual user here to simulate a race. # We use the minimal, un-mocked function. kwargs["bot_type"] = None kwargs["bot_owner"] = None kwargs["tos_version"] = None kwargs["timezone"] = timezone_now() create_user_profile(**kwargs).save() raise IntegrityError()
def bulk_create_users(realm: Realm, users_raw: Set[Tuple[str, str, str, bool]], bot_type: Optional[int]=None, bot_owner: Optional[UserProfile]=None, tos_version: Optional[str]=None, timezone: str="") -> None: """ Creates and saves a UserProfile with the given email. Has some code based off of UserManage.create_user, but doesn't .save() """ existing_users = frozenset(UserProfile.objects.filter( realm=realm).values_list('email', flat=True)) users = sorted([user_raw for user_raw in users_raw if user_raw[0] not in existing_users]) # If we have a different email_address_visibility mode, the code # below doesn't have the logic to set user_profile.email properly. assert realm.email_address_visibility == Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE # Now create user_profiles profiles_to_create = [] # type: List[UserProfile] for (email, full_name, short_name, active) in users: profile = create_user_profile(realm, email, initial_password(email), active, bot_type, full_name, short_name, bot_owner, False, tos_version, timezone, tutorial_status=UserProfile.TUTORIAL_FINISHED, enter_sends=True) profiles_to_create.append(profile) UserProfile.objects.bulk_create(profiles_to_create) RealmAuditLog.objects.bulk_create( [RealmAuditLog(realm=realm, modified_user=profile_, event_type=RealmAuditLog.USER_CREATED, event_time=profile_.date_joined) for profile_ in profiles_to_create]) profiles_by_email = {} # type: Dict[str, UserProfile] profiles_by_id = {} # type: Dict[int, UserProfile] for profile in UserProfile.objects.select_related().filter(realm=realm): profiles_by_email[profile.email] = profile profiles_by_id[profile.id] = profile recipients_to_create = [] # type: List[Recipient] for (email, full_name, short_name, active) in users: recipients_to_create.append(Recipient(type_id=profiles_by_email[email].id, type=Recipient.PERSONAL)) Recipient.objects.bulk_create(recipients_to_create) recipients_by_email = {} # type: Dict[str, Recipient] for recipient in recipients_to_create: recipients_by_email[profiles_by_id[recipient.type_id].email] = recipient subscriptions_to_create = [] # type: List[Subscription] for (email, full_name, short_name, active) in users: subscriptions_to_create.append( Subscription(user_profile_id=profiles_by_email[email].id, recipient=recipients_by_email[email])) Subscription.objects.bulk_create(subscriptions_to_create)
def bulk_create_users(realm, users_raw, bot_type=None, tos_version=None): # type: (Realm, Set[Tuple[Text, Text, Text, bool]], Optional[int], Optional[Text]) -> None """ Creates and saves a UserProfile with the given email. Has some code based off of UserManage.create_user, but doesn't .save() """ existing_users = frozenset( UserProfile.objects.values_list('email', flat=True)) users = sorted([ user_raw for user_raw in users_raw if user_raw[0] not in existing_users ]) # Now create user_profiles profiles_to_create = [] # type: List[UserProfile] for (email, full_name, short_name, active) in users: 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) UserProfile.objects.bulk_create(profiles_to_create) RealmAuditLog.objects.bulk_create([ RealmAuditLog(realm=profile_.realm, modified_user=profile_, event_type='user_created', event_time=profile_.date_joined) for profile_ in profiles_to_create ]) profiles_by_email = {} # type: Dict[Text, UserProfile] profiles_by_id = {} # type: Dict[int, UserProfile] for profile in UserProfile.objects.select_related().all(): profiles_by_email[profile.email] = profile profiles_by_id[profile.id] = profile recipients_to_create = [] # type: List[Recipient] for (email, full_name, short_name, active) in users: recipients_to_create.append( Recipient(type_id=profiles_by_email[email].id, type=Recipient.PERSONAL)) Recipient.objects.bulk_create(recipients_to_create) recipients_by_email = {} # type: Dict[Text, Recipient] for recipient in Recipient.objects.filter(type=Recipient.PERSONAL): recipients_by_email[profiles_by_id[ recipient.type_id].email] = recipient subscriptions_to_create = [] # type: List[Subscription] for (email, full_name, short_name, active) in users: subscriptions_to_create.append( Subscription(user_profile_id=profiles_by_email[email].id, recipient=recipients_by_email[email])) Subscription.objects.bulk_create(subscriptions_to_create)
def bulk_create_users(realm: Realm, users_raw: Set[Tuple[str, str, str, bool]], bot_type: Optional[int]=None, bot_owner: Optional[UserProfile]=None, tos_version: Optional[str]=None, timezone: str="") -> None: """ Creates and saves a UserProfile with the given email. Has some code based off of UserManage.create_user, but doesn't .save() """ existing_users = frozenset(UserProfile.objects.filter( realm=realm).values_list('email', flat=True)) users = sorted([user_raw for user_raw in users_raw if user_raw[0] not in existing_users]) # Now create user_profiles profiles_to_create = [] # type: List[UserProfile] for (email, full_name, short_name, active) in users: profile = create_user_profile(realm, email, initial_password(email), active, bot_type, full_name, short_name, bot_owner, False, tos_version, timezone, tutorial_status=UserProfile.TUTORIAL_FINISHED, enter_sends=True) profiles_to_create.append(profile) UserProfile.objects.bulk_create(profiles_to_create) RealmAuditLog.objects.bulk_create( [RealmAuditLog(realm=realm, modified_user=profile_, event_type=RealmAuditLog.USER_CREATED, event_time=profile_.date_joined) for profile_ in profiles_to_create]) profiles_by_email = {} # type: Dict[str, UserProfile] profiles_by_id = {} # type: Dict[int, UserProfile] for profile in UserProfile.objects.select_related().filter(realm=realm): profiles_by_email[profile.email] = profile profiles_by_id[profile.id] = profile recipients_to_create = [] # type: List[Recipient] for (email, full_name, short_name, active) in users: recipients_to_create.append(Recipient(type_id=profiles_by_email[email].id, type=Recipient.PERSONAL)) Recipient.objects.bulk_create(recipients_to_create) recipients_by_email = {} # type: Dict[str, Recipient] for recipient in recipients_to_create: recipients_by_email[profiles_by_id[recipient.type_id].email] = recipient subscriptions_to_create = [] # type: List[Subscription] for (email, full_name, short_name, active) in users: subscriptions_to_create.append( Subscription(user_profile_id=profiles_by_email[email].id, recipient=recipients_by_email[email])) Subscription.objects.bulk_create(subscriptions_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 """ Creates and saves a UserProfile with the given email. Has some code based off of UserManage.create_user, but doesn't .save() """ users = [] # type: List[Tuple[text_type, text_type, text_type, bool]] existing_users = set( u.email for u in UserProfile.objects.all()) # type: Set[text_type] for (email, full_name, short_name, active) in users_raw: if email in existing_users: continue users.append((email, full_name, short_name, active)) existing_users.add(email) users = sorted(users) # 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, initial_password(email), active, bot_type, full_name, short_name, None, False, tos_version) profiles_to_create.append(profile) UserProfile.objects.bulk_create(profiles_to_create) profiles_by_email = {} # type: Dict[text_type, UserProfile] profiles_by_id = {} # type: Dict[int, UserProfile] for profile in UserProfile.objects.select_related().all(): profiles_by_email[profile.email] = profile profiles_by_id[profile.id] = profile recipients_to_create = [] # type: List[Recipient] for (email, full_name, short_name, active) in users: recipients_to_create.append( Recipient(type_id=profiles_by_email[email].id, type=Recipient.PERSONAL)) Recipient.objects.bulk_create(recipients_to_create) recipients_by_email = {} # type: Dict[text_type, Recipient] for recipient in Recipient.objects.filter(type=Recipient.PERSONAL): recipients_by_email[profiles_by_id[ recipient.type_id].email] = recipient subscriptions_to_create = [] # type: List[Subscription] for (email, full_name, short_name, active) in users: subscriptions_to_create.append( Subscription(user_profile_id=profiles_by_email[email].id, recipient=recipients_by_email[email])) Subscription.objects.bulk_create(subscriptions_to_create)
def bulk_create_users(realms, users_raw, bot=False): """ Creates and saves a UserProfile with the given email. Has some code based off of UserManage.create_user, but doesn't .save() """ users = [] existing_users = set(u.email for u in UserProfile.objects.all()) for (email, full_name, short_name, active) in users_raw: if email in existing_users: continue users.append((email, full_name, short_name, active)) existing_users.add(email) users = sorted(users) # Now create user_profiles profiles_to_create = [] for (email, full_name, short_name, active) in users: domain = resolve_email_to_domain(email) profile = create_user_profile(realms[domain], email, initial_password(email), active, bot, full_name, short_name, None, False) profiles_to_create.append(profile) UserProfile.objects.bulk_create(profiles_to_create) profiles_by_email = {} profiles_by_id = {} for profile in UserProfile.objects.select_related().all(): profiles_by_email[profile.email] = profile profiles_by_id[profile.id] = profile recipients_to_create = [] for (email, _, _, _) in users: recipients_to_create.append( Recipient(type_id=profiles_by_email[email].id, type=Recipient.PERSONAL)) Recipient.objects.bulk_create(recipients_to_create) recipients_by_email = {} for recipient in Recipient.objects.filter(type=Recipient.PERSONAL): recipients_by_email[profiles_by_id[ recipient.type_id].email] = recipient subscriptions_to_create = [] for (email, _, _, _) in users: subscriptions_to_create.append( Subscription(user_profile_id=profiles_by_email[email].id, recipient=recipients_by_email[email])) Subscription.objects.bulk_create(subscriptions_to_create)
def bulk_create_users(realm, users_raw, bot_type=None, tos_version=None, timezone=u""): # type: (Realm, Set[Tuple[Text, Text, Text, bool]], Optional[int], Optional[Text], Text) -> None """ Creates and saves a UserProfile with the given email. Has some code based off of UserManage.create_user, but doesn't .save() """ existing_users = frozenset(UserProfile.objects.values_list('email', flat=True)) users = sorted([user_raw for user_raw in users_raw if user_raw[0] not in existing_users]) # Now create user_profiles profiles_to_create = [] # type: List[UserProfile] for (email, full_name, short_name, active) in users: profile = create_user_profile(realm, email, initial_password(email), active, bot_type, full_name, short_name, None, False, tos_version, timezone, tutorial_status=UserProfile.TUTORIAL_FINISHED, enter_sends=True) profiles_to_create.append(profile) UserProfile.objects.bulk_create(profiles_to_create) RealmAuditLog.objects.bulk_create( [RealmAuditLog(realm=profile_.realm, modified_user=profile_, event_type='user_created', event_time=profile_.date_joined) for profile_ in profiles_to_create]) profiles_by_email = {} # type: Dict[Text, UserProfile] profiles_by_id = {} # type: Dict[int, UserProfile] for profile in UserProfile.objects.select_related().all(): profiles_by_email[profile.email] = profile profiles_by_id[profile.id] = profile recipients_to_create = [] # type: List[Recipient] for (email, full_name, short_name, active) in users: recipients_to_create.append(Recipient(type_id=profiles_by_email[email].id, type=Recipient.PERSONAL)) Recipient.objects.bulk_create(recipients_to_create) recipients_by_email = {} # type: Dict[Text, Recipient] for recipient in Recipient.objects.filter(type=Recipient.PERSONAL): recipients_by_email[profiles_by_id[recipient.type_id].email] = recipient subscriptions_to_create = [] # type: List[Subscription] for (email, full_name, short_name, active) in users: subscriptions_to_create.append( Subscription(user_profile_id=profiles_by_email[email].id, recipient=recipients_by_email[email])) Subscription.objects.bulk_create(subscriptions_to_create)
def bulk_create_users(realms, users_raw, bot_type=None): # type: (Mapping[text_type, Realm], Set[Tuple[text_type, text_type, text_type, bool]], Optional[int]) -> None """ Creates and saves a UserProfile with the given email. Has some code based off of UserManage.create_user, but doesn't .save() """ users = [] # type: List[Tuple[text_type, text_type, text_type, bool]] existing_users = set(u.email for u in UserProfile.objects.all()) # type: Set[text_type] for (email, full_name, short_name, active) in users_raw: if email in existing_users: continue users.append((email, full_name, short_name, active)) existing_users.add(email) users = sorted(users) # Now create user_profiles profiles_to_create = [] # type: List[UserProfile] for (email, full_name, short_name, active) in users: domain = resolve_email_to_domain(email) profile = create_user_profile(realms[domain], email, initial_password(email), active, bot_type, full_name, short_name, None, False) profiles_to_create.append(profile) UserProfile.objects.bulk_create(profiles_to_create) profiles_by_email = {} # type: Dict[text_type, UserProfile] profiles_by_id = {} # type: Dict[int, UserProfile] for profile in UserProfile.objects.select_related().all(): profiles_by_email[profile.email] = profile profiles_by_id[profile.id] = profile recipients_to_create = [] # type: List[Recipient] for (email, full_name, short_name, active) in users: recipients_to_create.append(Recipient(type_id=profiles_by_email[email].id, type=Recipient.PERSONAL)) Recipient.objects.bulk_create(recipients_to_create) recipients_by_email = {} # type: Dict[text_type, Recipient] for recipient in Recipient.objects.filter(type=Recipient.PERSONAL): recipients_by_email[profiles_by_id[recipient.type_id].email] = recipient subscriptions_to_create = [] # type: List[Subscription] for (email, full_name, short_name, active) in users: subscriptions_to_create.append( Subscription(user_profile_id=profiles_by_email[email].id, recipient=recipients_by_email[email])) Subscription.objects.bulk_create(subscriptions_to_create)
def bulk_create_users(realms, users_raw, bot=False): """ Creates and saves a UserProfile with the given email. Has some code based off of UserManage.create_user, but doesn't .save() """ users = [] existing_users = set(u.email for u in UserProfile.objects.all()) for (email, full_name, short_name, active) in users_raw: if email in existing_users: continue users.append((email, full_name, short_name, active)) existing_users.add(email) # Now create user_profiles profiles_to_create = [] for (email, full_name, short_name, active) in users: domain = resolve_email_to_domain(email) profile = create_user_profile(realms[domain], email, initial_password(email), active, bot, full_name, short_name, None, False) profiles_to_create.append(profile) UserProfile.objects.bulk_create(profiles_to_create) profiles_by_email = {} profiles_by_id = {} for profile in UserProfile.objects.select_related().all(): profiles_by_email[profile.email] = profile profiles_by_id[profile.id] = profile recipients_to_create = [] for (email, _, _, _) in users: recipients_to_create.append(Recipient(type_id=profiles_by_email[email].id, type=Recipient.PERSONAL)) Recipient.objects.bulk_create(recipients_to_create) recipients_by_email = {} for recipient in Recipient.objects.filter(type=Recipient.PERSONAL): recipients_by_email[profiles_by_id[recipient.type_id].email] = recipient subscriptions_to_create = [] for (email, _, _, _) in users: subscriptions_to_create.append( Subscription(user_profile_id=profiles_by_email[email].id, recipient=recipients_by_email[email])) Subscription.objects.bulk_create(subscriptions_to_create)
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() """ existing_users = frozenset(UserProfile.objects.values_list('email', flat=True)) users = sorted([user_raw for user_raw in users_raw if user_raw[0] not in existing_users]) # Now create user_profiles profiles_to_create = [] # type: List[UserProfile] for (email, full_name, short_name, active) in users: 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) UserProfile.objects.bulk_create(profiles_to_create) profiles_by_email = {} # type: Dict[text_type, UserProfile] profiles_by_id = {} # type: Dict[int, UserProfile] for profile in UserProfile.objects.select_related().all(): profiles_by_email[profile.email] = profile profiles_by_id[profile.id] = profile recipients_to_create = [] # type: List[Recipient] for (email, full_name, short_name, active) in users: recipients_to_create.append(Recipient(type_id=profiles_by_email[email].id, type=Recipient.PERSONAL)) Recipient.objects.bulk_create(recipients_to_create) recipients_by_email = {} # type: Dict[text_type, Recipient] for recipient in Recipient.objects.filter(type=Recipient.PERSONAL): recipients_by_email[profiles_by_id[recipient.type_id].email] = recipient subscriptions_to_create = [] # type: List[Subscription] for (email, full_name, short_name, active) in users: subscriptions_to_create.append( Subscription(user_profile_id=profiles_by_email[email].id, recipient=recipients_by_email[email])) Subscription.objects.bulk_create(subscriptions_to_create)
def bulk_create_users(realm: Realm, users_raw: Set[Tuple[str, str, bool]], bot_type: Optional[int] = None, bot_owner: Optional[UserProfile] = None, tos_version: Optional[str] = None, timezone: str = "") -> None: """ Creates and saves a UserProfile with the given email. Has some code based off of UserManage.create_user, but doesn't .save() """ existing_users = frozenset( UserProfile.objects.filter(realm=realm).values_list('email', flat=True)) users = sorted(user_raw for user_raw in users_raw if user_raw[0] not in existing_users) # Now create user_profiles profiles_to_create: List[UserProfile] = [] for (email, full_name, active) in users: profile = create_user_profile( realm, email, initial_password(email), active, bot_type, full_name, bot_owner, False, tos_version, timezone, tutorial_status=UserProfile.TUTORIAL_FINISHED, enter_sends=True) profiles_to_create.append(profile) if realm.email_address_visibility == Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE: UserProfile.objects.bulk_create(profiles_to_create) else: for user_profile in profiles_to_create: user_profile.email = user_profile.delivery_email UserProfile.objects.bulk_create(profiles_to_create) for user_profile in profiles_to_create: user_profile.email = get_display_email_address(user_profile, realm) UserProfile.objects.bulk_update(profiles_to_create, ['email']) user_ids = {user.id for user in profiles_to_create} RealmAuditLog.objects.bulk_create( RealmAuditLog(realm=realm, modified_user=profile_, event_type=RealmAuditLog.USER_CREATED, event_time=profile_.date_joined) for profile_ in profiles_to_create) recipients_to_create: List[Recipient] = [] for user_id in user_ids: recipient = Recipient(type_id=user_id, type=Recipient.PERSONAL) recipients_to_create.append(recipient) Recipient.objects.bulk_create(recipients_to_create) bulk_set_users_or_streams_recipient_fields(UserProfile, profiles_to_create, recipients_to_create) recipients_by_user_id: Dict[int, Recipient] = {} for recipient in recipients_to_create: recipients_by_user_id[recipient.type_id] = recipient subscriptions_to_create: List[Subscription] = [] for user_id in user_ids: recipient = recipients_by_user_id[user_id] subscription = Subscription(user_profile_id=user_id, recipient=recipient) subscriptions_to_create.append(subscription) Subscription.objects.bulk_create(subscriptions_to_create)
def bulk_create_users( realm: Realm, users_raw: Set[Tuple[str, str, bool]], bot_type: Optional[int] = None, bot_owner: Optional[UserProfile] = None, tos_version: Optional[str] = None, timezone: str = "", ) -> None: """ Creates and saves a UserProfile with the given email. Has some code based off of UserManage.create_user, but doesn't .save() """ existing_users = frozenset( UserProfile.objects.filter(realm=realm).values_list("email", flat=True)) users = sorted(user_raw for user_raw in users_raw if user_raw[0] not in existing_users) realm_user_default = RealmUserDefault.objects.get(realm=realm) # Now create user_profiles profiles_to_create: List[UserProfile] = [] for (email, full_name, active) in users: profile = create_user_profile( realm, email, initial_password(email), active, bot_type, full_name, bot_owner, False, tos_version, timezone, tutorial_status=UserProfile.TUTORIAL_FINISHED, ) if bot_type is None: # This block simulates copy_default_settings from # zerver/lib/create_user.py. # # We cannot use 'copy_default_settings' directly here # because it calls '.save' after copying the settings, and # we are bulk creating the objects here instead. for settings_name in RealmUserDefault.property_types: if settings_name in [ "default_language", "enable_login_emails" ]: continue value = getattr(realm_user_default, settings_name) setattr(profile, settings_name, value) profiles_to_create.append(profile) if realm.email_address_visibility == Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE: UserProfile.objects.bulk_create(profiles_to_create) else: for user_profile in profiles_to_create: user_profile.email = user_profile.delivery_email UserProfile.objects.bulk_create(profiles_to_create) for user_profile in profiles_to_create: user_profile.email = get_display_email_address(user_profile) UserProfile.objects.bulk_update(profiles_to_create, ["email"]) user_ids = {user.id for user in profiles_to_create} RealmAuditLog.objects.bulk_create( RealmAuditLog( realm=realm, modified_user=profile_, event_type=RealmAuditLog.USER_CREATED, event_time=profile_.date_joined, ) for profile_ in profiles_to_create) recipients_to_create: List[Recipient] = [] for user_id in user_ids: recipient = Recipient(type_id=user_id, type=Recipient.PERSONAL) recipients_to_create.append(recipient) Recipient.objects.bulk_create(recipients_to_create) bulk_set_users_or_streams_recipient_fields(UserProfile, profiles_to_create, recipients_to_create) recipients_by_user_id: Dict[int, Recipient] = {} for recipient in recipients_to_create: recipients_by_user_id[recipient.type_id] = recipient subscriptions_to_create: List[Subscription] = [] for user_profile in profiles_to_create: recipient = recipients_by_user_id[user_profile.id] subscription = Subscription( user_profile_id=user_profile.id, recipient=recipient, is_user_active=user_profile.is_active, ) subscriptions_to_create.append(subscription) Subscription.objects.bulk_create(subscriptions_to_create)