def create_user_profile(realm: Realm, email: str, password: Optional[str], active: bool, bot_type: Optional[int], full_name: str, short_name: str, bot_owner: Optional[UserProfile], is_mirror_dummy: bool, tos_version: Optional[str], timezone: Optional[str], tutorial_status: Optional[str] = UserProfile.TUTORIAL_WAITING, enter_sends: bool = False) -> UserProfile: now = timezone_now() email = UserManager.normalize_email(email) user_profile = UserProfile(is_staff=False, is_active=active, full_name=full_name, short_name=short_name, last_login=now, date_joined=now, realm=realm, pointer=-1, is_bot=bool(bot_type), bot_type=bot_type, bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy, tos_version=tos_version, timezone=timezone, tutorial_status=tutorial_status, enter_sends=enter_sends, onboarding_steps=ujson.dumps([]), default_language=realm.default_language, twenty_four_hour_time=realm.default_twenty_four_hour_time, delivery_email=email) if bot_type or not active: password = None if realm.email_address_visibility == Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE: # If emails are visible to everyone, we can set this here and save a DB query user_profile.email = get_display_email_address(user_profile, realm) user_profile.set_password(password) user_profile.api_key = generate_api_key() return user_profile
def create_user_profile(realm: Realm, email: str, password: Optional[str], active: bool, bot_type: Optional[int], full_name: str, short_name: str, bot_owner: Optional[UserProfile], is_mirror_dummy: bool, tos_version: Optional[str], timezone: Optional[str], tutorial_status: str = UserProfile.TUTORIAL_WAITING, enter_sends: bool = False) -> UserProfile: now = timezone_now() email = UserManager.normalize_email(email) user_profile = UserProfile(is_staff=False, is_active=active, full_name=full_name, short_name=short_name, last_login=now, date_joined=now, realm=realm, is_bot=bool(bot_type), bot_type=bot_type, bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy, tos_version=tos_version, timezone=timezone, tutorial_status=tutorial_status, enter_sends=enter_sends, onboarding_steps=ujson.dumps([]), default_language=realm.default_language, twenty_four_hour_time=realm.default_twenty_four_hour_time, delivery_email=email) if bot_type or not active: password = None if user_profile.email_address_is_realm_public(): # If emails are visible to everyone, we can set this here and save a DB query user_profile.email = get_display_email_address(user_profile, realm) user_profile.set_password(password) user_profile.api_key = generate_api_key() return user_profile
def create_user_profile( realm: Realm, email: str, password: Optional[str], active: bool, bot_type: Optional[int], full_name: str, bot_owner: Optional[UserProfile], is_mirror_dummy: bool, tos_version: Optional[str], timezone: Optional[str], tutorial_status: str = UserProfile.TUTORIAL_WAITING, enter_sends: bool = False, force_id: Optional[int] = None, force_date_joined: Optional[datetime] = None, ) -> UserProfile: if force_date_joined is None: date_joined = timezone_now() else: date_joined = force_date_joined email = UserManager.normalize_email(email) extra_kwargs = {} if force_id is not None: extra_kwargs["id"] = force_id user_profile = UserProfile( is_staff=False, is_active=active, full_name=full_name, last_login=date_joined, date_joined=date_joined, realm=realm, is_bot=bool(bot_type), bot_type=bot_type, bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy, tos_version=tos_version, timezone=timezone, tutorial_status=tutorial_status, enter_sends=enter_sends, onboarding_steps=orjson.dumps([]).decode(), default_language=realm.default_language, delivery_email=email, **extra_kwargs, ) if bot_type or not active: password = None if user_profile.email_address_is_realm_public(): # If emails are visible to everyone, we can set this here and save a DB query user_profile.email = get_display_email_address(user_profile) user_profile.set_password(password) user_profile.api_key = generate_api_key() return user_profile
def do_change_user_delivery_email(user_profile: UserProfile, new_email: str) -> None: delete_user_profile_caches([user_profile]) user_profile.delivery_email = new_email if user_profile.email_address_is_realm_public(): user_profile.email = new_email user_profile.save(update_fields=["email", "delivery_email"]) else: user_profile.save(update_fields=["delivery_email"]) # We notify just the target user (and eventually org admins, only # when email_address_visibility=EMAIL_ADDRESS_VISIBILITY_ADMINS) # about their new delivery email, since that field is private. payload = dict(user_id=user_profile.id, delivery_email=new_email) event = dict(type="realm_user", op="update", person=payload) transaction.on_commit( lambda: send_event(user_profile.realm, event, [user_profile.id])) if user_profile.avatar_source == UserProfile.AVATAR_FROM_GRAVATAR: # If the user is using Gravatar to manage their email address, # their Gravatar just changed, and we need to notify other # clients. notify_avatar_url_change(user_profile) if user_profile.email_address_is_realm_public(): # Additionally, if we're also changing the publicly visible # email, we send a new_email event as well. send_user_email_update_event(user_profile) event_time = timezone_now() RealmAuditLog.objects.create( realm=user_profile.realm, acting_user=user_profile, modified_user=user_profile, event_type=RealmAuditLog.USER_EMAIL_CHANGED, event_time=event_time, )