actions: Extract get_active_bots_owned_by_user function.

This commit is contained in:
Mateusz Mandera 2021-05-10 22:57:20 +02:00 committed by Tim Abbott
parent 8261f7e801
commit 20f99f429d
3 changed files with 9 additions and 17 deletions

View File

@ -1137,6 +1137,10 @@ def change_user_is_active(user_profile: UserProfile, value: bool) -> None:
Subscription.objects.filter(user_profile=user_profile).update(is_user_active=value)
def get_active_bots_owned_by_user(user_profile: UserProfile) -> QuerySet:
return UserProfile.objects.filter(is_bot=True, is_active=True, bot_owner=user_profile)
def do_deactivate_user(
user_profile: UserProfile, _cascade: bool = True, *, acting_user: Optional[UserProfile]
) -> None:
@ -1147,9 +1151,7 @@ def do_deactivate_user(
# We need to deactivate bots before the target user, to ensure
# that a failure partway through this function cannot result
# in only the user being deactivated.
bot_profiles = UserProfile.objects.filter(
is_bot=True, is_active=True, bot_owner=user_profile
)
bot_profiles = get_active_bots_owned_by_user(user_profile)
for profile in bot_profiles:
do_deactivate_user(profile, _cascade=False, acting_user=acting_user)

View File

@ -1,10 +1,9 @@
from argparse import ArgumentParser
from typing import Any
from zerver.lib.actions import do_deactivate_user
from zerver.lib.actions import do_deactivate_user, get_active_bots_owned_by_user
from zerver.lib.management import CommandError, ZulipBaseCommand
from zerver.lib.sessions import user_sessions
from zerver.models import UserProfile
class Command(ZulipBaseCommand):
@ -34,11 +33,7 @@ class Command(ZulipBaseCommand):
print(
"{} has {} active bots that will also be deactivated.".format(
user_profile.delivery_email,
UserProfile.objects.filter(
is_bot=True,
is_active=True,
bot_owner=user_profile,
).count(),
get_active_bots_owned_by_user(user_profile).count(),
)
)

View File

@ -1,9 +1,8 @@
from argparse import ArgumentParser
from typing import Any
from zerver.lib.actions import do_delete_user
from zerver.lib.actions import do_delete_user, get_active_bots_owned_by_user
from zerver.lib.management import CommandError, ZulipBaseCommand
from zerver.models import UserProfile
class Command(ZulipBaseCommand):
@ -54,11 +53,7 @@ This will:
print(
"{} has {} active bots that will be deactivated as a result of the user's deletion.".format(
user_profile.delivery_email,
UserProfile.objects.filter(
is_bot=True,
is_active=True,
bot_owner=user_profile,
).count(),
get_active_bots_owned_by_user(user_profile).count(),
)
)