From 20f99f429dff04970384472098428bedbc7e3f0d Mon Sep 17 00:00:00 2001 From: Mateusz Mandera Date: Mon, 10 May 2021 22:57:20 +0200 Subject: [PATCH] actions: Extract get_active_bots_owned_by_user function. --- zerver/lib/actions.py | 8 +++++--- zerver/management/commands/deactivate_user.py | 9 ++------- zerver/management/commands/delete_user.py | 9 ++------- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 0bb0af7d1d..fdd853b0e9 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -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) diff --git a/zerver/management/commands/deactivate_user.py b/zerver/management/commands/deactivate_user.py index f2be870d0e..973a90a95e 100644 --- a/zerver/management/commands/deactivate_user.py +++ b/zerver/management/commands/deactivate_user.py @@ -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(), ) ) diff --git a/zerver/management/commands/delete_user.py b/zerver/management/commands/delete_user.py index 8d21124415..661d7f74f5 100644 --- a/zerver/management/commands/delete_user.py +++ b/zerver/management/commands/delete_user.py @@ -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(), ) )