2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2017-07-08 17:33:54 +02:00
|
|
|
from django.core.management.base import CommandParser
|
2013-02-12 17:08:21 +01:00
|
|
|
|
2016-10-20 17:53:53 +02:00
|
|
|
from zerver.lib.actions import bulk_remove_subscriptions
|
2017-07-08 17:33:54 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2017-11-02 10:19:24 +01:00
|
|
|
from zerver.models import get_stream
|
2013-02-12 17:08:21 +01:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-07-08 17:33:54 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2013-02-12 17:08:21 +01:00
|
|
|
help = """Remove some or all users in a realm from a stream."""
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
2016-11-03 10:22:19 +01:00
|
|
|
parser.add_argument('-s', '--stream',
|
2017-08-19 21:36:51 +02:00
|
|
|
required=True,
|
2016-11-03 10:22:19 +01:00
|
|
|
help='A stream name.')
|
|
|
|
|
2017-07-08 17:33:54 +02:00
|
|
|
self.add_realm_args(parser, True)
|
2017-08-24 21:46:13 +02:00
|
|
|
self.add_user_list_args(parser, all_users_help='Remove all users in realm from this stream.')
|
2013-02-12 17:08:21 +01:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, **options: Any) -> None:
|
2017-07-08 17:33:54 +02:00
|
|
|
realm = self.get_realm(options)
|
2017-09-26 01:25:39 +02:00
|
|
|
assert realm is not None # Should be ensured by parser
|
2017-08-19 21:36:51 +02:00
|
|
|
user_profiles = self.get_users(options, realm)
|
2013-02-12 17:08:21 +01:00
|
|
|
stream_name = options["stream"].strip()
|
|
|
|
stream = get_stream(stream_name, realm)
|
|
|
|
|
2020-06-29 13:15:10 +02:00
|
|
|
result = bulk_remove_subscriptions(user_profiles, [stream], self.get_client(), acting_user=None)
|
2016-10-20 17:53:53 +02:00
|
|
|
not_subscribed = result[1]
|
|
|
|
not_subscribed_users = {tup[0] for tup in not_subscribed}
|
|
|
|
|
2013-02-12 17:08:21 +01:00
|
|
|
for user_profile in user_profiles:
|
2016-10-20 17:53:53 +02:00
|
|
|
if user_profile in not_subscribed_users:
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f"{user_profile.delivery_email} was not subscribed")
|
2016-10-20 17:53:53 +02:00
|
|
|
else:
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f"Removed {user_profile.delivery_email} from {stream_name}")
|