Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

actions: Extract get_active_bots_owned_by_user function.

Mateusz Mandera20f99f429dff
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py
index 0bb0af7d1db..fdd853b0e99 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 f2be870d0e0..973a90a95e2 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 8d21124415c..661d7f74f57 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(),
)
)