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 21:42:50 +02:00
|
|
|
from django.core.management.base import CommandParser
|
2013-01-09 17:00:17 +01:00
|
|
|
|
2016-10-20 01:10:15 +02:00
|
|
|
from zerver.lib.actions import create_stream_if_needed, bulk_add_subscriptions
|
2017-07-07 20:35:31 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2017-07-08 21:42:50 +02:00
|
|
|
from zerver.models import UserProfile
|
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."""
|
|
|
|
|
2016-11-03 10:22:19 +01:00
|
|
|
def add_arguments(self, parser):
|
|
|
|
# type: (CommandParser) -> None
|
2017-07-07 20:35:31 +02:00
|
|
|
self.add_realm_args(parser)
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'-s', '--streams',
|
|
|
|
dest='streams',
|
|
|
|
type=str,
|
|
|
|
help='A comma-separated list of stream names.')
|
|
|
|
|
|
|
|
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='Add all users in this realm to these streams.')
|
2013-01-09 17:00:17 +01:00
|
|
|
|
|
|
|
def handle(self, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (**Any) -> None
|
2017-07-07 20:35:31 +02:00
|
|
|
if options["streams"] is None or \
|
2017-07-07 21:05:43 +02:00
|
|
|
(options["users"] is None and not options["all_users"]):
|
2016-11-22 01:44:16 +01:00
|
|
|
self.print_help("./manage.py", "add_users_to_streams")
|
2013-01-09 17:00:17 +01:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
stream_names = set([stream.strip() for stream in options["streams"].split(",")])
|
2017-07-07 20:35:31 +02:00
|
|
|
realm = self.get_realm(options)
|
2013-01-09 17:00:17 +01:00
|
|
|
|
|
|
|
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-07 20:35:31 +02:00
|
|
|
user_profiles.append(self.get_user(email, realm))
|
2013-01-09 17:00:17 +01:00
|
|
|
|
|
|
|
for stream_name in set(stream_names):
|
|
|
|
for user_profile in user_profiles:
|
2013-02-12 16:56:37 +01:00
|
|
|
stream, _ = create_stream_if_needed(user_profile.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))
|