2013-04-23 18:51:17 +02:00
|
|
|
|
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
|
|
|
|
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',
|
|
|
|
dest='stream',
|
2017-08-19 21:36:51 +02:00
|
|
|
required=True,
|
2016-11-03 10:22:19 +01:00
|
|
|
type=str,
|
|
|
|
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)
|
|
|
|
|
2018-03-14 00:13:21 +01:00
|
|
|
result = bulk_remove_subscriptions(user_profiles, [stream], self.get_client())
|
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:
|
|
|
|
print("%s was not subscribed" % (user_profile.email,))
|
|
|
|
else:
|
|
|
|
print("Removed %s from %s" % (user_profile.email, stream_name))
|