2013-04-23 18:51:17 +02:00
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2017-07-08 21:42:50 +02:00
|
|
|
from django.core.management.base import CommandParser
|
2013-01-09 17:00:17 +01:00
|
|
|
|
2017-11-16 00:43:27 +01:00
|
|
|
from zerver.lib.actions import bulk_add_subscriptions, create_stream_if_needed
|
2017-07-07 20:35:31 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2013-01-09 17:00:17 +01:00
|
|
|
|
2017-07-07 20:35:31 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2013-01-09 17:00:17 +01:00
|
|
|
help = """Add some or all users in a realm to a set of streams."""
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
2017-08-19 21:23:50 +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="Add all users in realm to these streams.")
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'-s', '--streams',
|
|
|
|
dest='streams',
|
|
|
|
type=str,
|
2017-08-19 21:23:50 +02:00
|
|
|
required=True,
|
2016-11-03 10:22:19 +01:00
|
|
|
help='A comma-separated list of stream names.')
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, **options: Any) -> None:
|
2017-08-19 21:23:50 +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:23:50 +02:00
|
|
|
user_profiles = self.get_users(options, realm)
|
2013-01-09 17:00:17 +01:00
|
|
|
stream_names = set([stream.strip() for stream in options["streams"].split(",")])
|
|
|
|
|
|
|
|
for stream_name in set(stream_names):
|
|
|
|
for user_profile in user_profiles:
|
2017-08-19 21:23:50 +02:00
|
|
|
stream, _ = create_stream_if_needed(realm, stream_name)
|
2016-10-20 01:10:15 +02:00
|
|
|
_ignore, already_subscribed = bulk_add_subscriptions([stream], [user_profile])
|
|
|
|
was_there_already = user_profile.id in {tup[0].id for tup in already_subscribed}
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%s %s to %s" % (
|
2016-10-20 01:10:15 +02:00
|
|
|
"Already subscribed" if was_there_already else "Subscribed",
|
2015-11-01 17:11:06 +01:00
|
|
|
user_profile.email, stream_name))
|