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
|
|
|
|
2013-02-12 17:08:21 +01:00
|
|
|
from optparse import make_option
|
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.actions import do_remove_subscription
|
2015-10-13 22:54:35 +02:00
|
|
|
from zerver.models import Realm, UserProfile, get_realm, get_stream, \
|
2013-03-28 20:20:31 +01:00
|
|
|
get_user_profile_by_email
|
2013-02-12 17:08:21 +01:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = """Remove some or all users in a realm from a stream."""
|
|
|
|
|
|
|
|
option_list = BaseCommand.option_list + (
|
|
|
|
make_option('-d', '--domain',
|
|
|
|
dest='domain',
|
|
|
|
type='str',
|
|
|
|
help='The name of the realm in which you are removing people.'),
|
|
|
|
make_option('-s', '--stream',
|
|
|
|
dest='stream',
|
|
|
|
type='str',
|
|
|
|
help='A stream name.'),
|
|
|
|
make_option('-u', '--users',
|
|
|
|
dest='users',
|
|
|
|
type='str',
|
|
|
|
help='A comma-separated list of email addresses.'),
|
|
|
|
make_option('-a', '--all-users',
|
|
|
|
dest='all_users',
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help='Remove all users in this realm from this stream.'),
|
|
|
|
)
|
|
|
|
|
|
|
|
def handle(self, **options):
|
|
|
|
if options["domain"] is None or options["stream"] is None or \
|
|
|
|
(options["users"] is None and options["all_users"] is None):
|
2015-10-04 18:56:26 +02:00
|
|
|
self.print_help("python2.7 manage.py", "remove_users_from_stream")
|
2013-02-12 17:08:21 +01:00
|
|
|
exit(1)
|
|
|
|
|
2015-10-13 22:54:35 +02:00
|
|
|
realm = get_realm(options["domain"])
|
2013-02-12 17:08:21 +01:00
|
|
|
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:
|
2013-03-28 20:20:31 +01:00
|
|
|
user_profiles.append(get_user_profile_by_email(email))
|
2013-02-12 17:08:21 +01:00
|
|
|
|
|
|
|
for user_profile in user_profiles:
|
|
|
|
did_remove = do_remove_subscription(user_profile, stream)
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%s %s from %s" % (
|
2013-02-12 17:08:21 +01:00
|
|
|
"Removed" if did_remove else "Couldn't remove",
|
2015-11-01 17:11:06 +01:00
|
|
|
user_profile.email, stream_name))
|