2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
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
|
|
|
|
from zerver.models import UserProfile, 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."""
|
|
|
|
|
2016-11-03 10:22:19 +01:00
|
|
|
def add_arguments(self, parser):
|
|
|
|
# type: (CommandParser) -> None
|
|
|
|
parser.add_argument('-s', '--stream',
|
|
|
|
dest='stream',
|
|
|
|
type=str,
|
|
|
|
help='A stream name.')
|
|
|
|
|
|
|
|
parser.add_argument('-u', '--users',
|
|
|
|
dest='users',
|
|
|
|
type=str,
|
|
|
|
help='A comma-separated list of email addresses.')
|
|
|
|
|
|
|
|
parser.add_argument('-a', '--all-users',
|
|
|
|
dest='all_users',
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help='Remove all users in this realm from this stream.')
|
2017-07-08 17:33:54 +02:00
|
|
|
self.add_realm_args(parser, True)
|
2013-02-12 17:08:21 +01:00
|
|
|
|
|
|
|
def handle(self, **options):
|
2017-02-19 03:39:27 +01:00
|
|
|
# type: (**Any) -> None
|
2017-07-08 17:33:54 +02:00
|
|
|
realm = self.get_realm(options)
|
|
|
|
|
|
|
|
if realm is None or options["stream"] is None or \
|
|
|
|
(options["users"] is None and not options["all_users"]):
|
2016-11-22 01:44:16 +01:00
|
|
|
self.print_help("./manage.py", "remove_users_from_stream")
|
2013-02-12 17:08:21 +01:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
stream_name = options["stream"].strip()
|
|
|
|
stream = get_stream(stream_name, realm)
|
|
|
|
|
|
|
|
if options["all_users"]:
|
|
|
|
user_profiles = UserProfile.objects.filter(realm=realm)
|
|
|
|
else:
|
|
|
|
emails = set([email.strip() for email in options["users"].split(",")])
|
|
|
|
user_profiles = []
|
|
|
|
for email in emails:
|
2017-07-08 17:33:54 +02:00
|
|
|
user_profiles.append(self.get_user(email, realm))
|
2013-02-12 17:08:21 +01:00
|
|
|
|
2016-10-20 17:53:53 +02:00
|
|
|
result = bulk_remove_subscriptions(user_profiles, [stream])
|
|
|
|
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))
|