def do_regenerate_api_key(user_profile: UserProfile, acting_user: UserProfile) -> str:
    old_api_key = user_profile.api_key
    new_api_key = generate_api_key()
    user_profile.api_key = new_api_key
    user_profile.save(update_fields=['api_key'])
    cache_delete(user_profile_by_api_key_cache_key(old_api_key))
    event_time = timezone_now()
    RealmAuditLog.objects.create(realm=user_profile.realm, acting_user=acting_user, modified_user=user_profile, event_type=RealmAuditLog.USER_API_KEY_CHANGED, event_time=event_time)
    if user_profile.is_bot:
        send_event(user_profile.realm, dict(type='realm_bot', op='update', bot=dict(user_id=user_profile.id, api_key=new_api_key)), bot_owner_user_ids(user_profile))
    event = {'type': 'clear_push_device_tokens', 'user_profile_id': user_profile.id}
    queue_json_publish('deferred_work', event)
    return new_api_key
def do_regenerate_api_key(user_profile: UserProfile, acting_user: UserProfile) -> str:
    old_api_key = user_profile.api_key
    new_api_key = generate_api_key()
    user_profile.api_key = new_api_key
    user_profile.save(update_fields=['api_key'])
    cache_delete(user_profile_by_api_key_cache_key(old_api_key))
    event_time = timezone_now()
    RealmAuditLog.objects.create(realm=user_profile.realm, acting_user=acting_user, modified_user=user_profile, event_type=RealmAuditLog.USER_API_KEY_CHANGED, event_time=event_time)
    if user_profile.is_bot:
        send_event(user_profile.realm, dict(type='realm_bot', op='update', bot=dict(user_id=user_profile.id, api_key=new_api_key)), bot_owner_user_ids(user_profile))
    event = {'type': 'clear_push_device_tokens', 'user_profile_id': user_profile.id}
    queue_json_publish('deferred_work', event)
    return new_api_key