Files
zulip/zerver/management/commands/add_users_to_streams.py
Jack Weatherilt 3396cfc2ef refactoring: Replaced occurences of create_stream_if_needed.
Issue #2088 asked for a wrapper to be created for
`create_stream_if_needed` (called `ensure_stream`) for the 25 times that
`create_stream_if_needed` is called and ignores whether the stream was
created. This commit replaces relevant occurences of
`create_stream_if_needed` with `ensure_stream`, including imports.

The changes weren't significant enough to add any tests or do any
additional manual testing.

The refactoring intended to make the API easier to use in most cases.
The majority of uses of `create_stream_if_needed` ignored the second
parameter.

Fixes: #2088.
2018-03-21 16:47:36 -07:00

38 lines
1.5 KiB
Python

from typing import Any
from django.core.management.base import CommandParser
from zerver.lib.actions import bulk_add_subscriptions, ensure_stream
from zerver.lib.management import ZulipBaseCommand
class Command(ZulipBaseCommand):
help = """Add some or all users in a realm to a set of streams."""
def add_arguments(self, parser: CommandParser) -> None:
self.add_realm_args(parser, True)
self.add_user_list_args(parser, all_users_help="Add all users in realm to these streams.")
parser.add_argument(
'-s', '--streams',
dest='streams',
type=str,
required=True,
help='A comma-separated list of stream names.')
def handle(self, **options: Any) -> None:
realm = self.get_realm(options)
assert realm is not None # Should be ensured by parser
user_profiles = self.get_users(options, realm)
stream_names = set([stream.strip() for stream in options["streams"].split(",")])
for stream_name in set(stream_names):
for user_profile in user_profiles:
stream = ensure_stream(realm, stream_name)
_ignore, already_subscribed = bulk_add_subscriptions([stream], [user_profile])
was_there_already = user_profile.id in {tup[0].id for tup in already_subscribed}
print("%s %s to %s" % (
"Already subscribed" if was_there_already else "Subscribed",
user_profile.email, stream_name))