mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 14:03:30 +00:00
management: Move all_users option to ZulipBaseCommand.
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from django.core.exceptions import MultipleObjectsReturned
|
from django.core.exceptions import MultipleObjectsReturned
|
||||||
from django.core.management.base import BaseCommand, CommandError
|
from django.core.management.base import BaseCommand, CommandError
|
||||||
@@ -31,8 +33,8 @@ You can use the command list_realms to find ID of the realms in this server."""
|
|||||||
type=str,
|
type=str,
|
||||||
help=help)
|
help=help)
|
||||||
|
|
||||||
def add_user_list_args(self, parser, required=False, help=None):
|
def add_user_list_args(self, parser, required=False, help=None, all_users_arg=True, all_users_help=None):
|
||||||
# type: (ArgumentParser, bool, Optional[str]) -> None
|
# type: (ArgumentParser, bool, Optional[str], bool, Optional[str]) -> None
|
||||||
if help is None:
|
if help is None:
|
||||||
help = 'A comma-separated list of email addresses.'
|
help = 'A comma-separated list of email addresses.'
|
||||||
|
|
||||||
@@ -43,6 +45,17 @@ You can use the command list_realms to find ID of the realms in this server."""
|
|||||||
type=str,
|
type=str,
|
||||||
help=help)
|
help=help)
|
||||||
|
|
||||||
|
if all_users_arg:
|
||||||
|
if all_users_help is None:
|
||||||
|
all_users_help = "All users in realm."
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'-a', '--all-users',
|
||||||
|
dest='all_users',
|
||||||
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
help=all_users_help)
|
||||||
|
|
||||||
def get_realm(self, options):
|
def get_realm(self, options):
|
||||||
# type: (Dict[str, Any]) -> Optional[Realm]
|
# type: (Dict[str, Any]) -> Optional[Realm]
|
||||||
val = options["realm_id"]
|
val = options["realm_id"]
|
||||||
@@ -62,6 +75,19 @@ You can use the command list_realms to find ID of the realms in this server."""
|
|||||||
|
|
||||||
def get_users(self, options, realm):
|
def get_users(self, options, realm):
|
||||||
# type: (Dict[str, Any], Optional[Realm]) -> List[UserProfile]
|
# type: (Dict[str, Any], Optional[Realm]) -> List[UserProfile]
|
||||||
|
if "all_users" in options:
|
||||||
|
all_users = options["all_users"]
|
||||||
|
|
||||||
|
# User should pass either user list or all_users flag
|
||||||
|
if bool(options["users"]) == all_users:
|
||||||
|
raise CommandError("You can't use both -u/--users and -a/--all-users.")
|
||||||
|
|
||||||
|
if all_users and realm is None:
|
||||||
|
raise CommandError("The --all-users option requires a realm; please pass --realm.")
|
||||||
|
|
||||||
|
if all_users:
|
||||||
|
return UserProfile.objects.filter(realm=realm)
|
||||||
|
|
||||||
if options["users"] is None:
|
if options["users"] is None:
|
||||||
return []
|
return []
|
||||||
emails = set([email.strip() for email in options["users"].split(",")])
|
emails = set([email.strip() for email in options["users"].split(",")])
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class Command(ZulipBaseCommand):
|
|||||||
def add_arguments(self, parser):
|
def add_arguments(self, parser):
|
||||||
# type: (CommandParser) -> None
|
# type: (CommandParser) -> None
|
||||||
self.add_realm_args(parser, True)
|
self.add_realm_args(parser, True)
|
||||||
self.add_user_list_args(parser)
|
self.add_user_list_args(parser, all_users_help="Add all users in realm to these streams.")
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-s', '--streams',
|
'-s', '--streams',
|
||||||
@@ -24,28 +24,12 @@ class Command(ZulipBaseCommand):
|
|||||||
required=True,
|
required=True,
|
||||||
help='A comma-separated list of stream names.')
|
help='A comma-separated list of stream names.')
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
'-a', '--all-users',
|
|
||||||
dest='all_users',
|
|
||||||
action="store_true",
|
|
||||||
default=False,
|
|
||||||
help='Add all users in this realm to these streams.')
|
|
||||||
|
|
||||||
def handle(self, **options):
|
def handle(self, **options):
|
||||||
# type: (**Any) -> None
|
# type: (**Any) -> None
|
||||||
realm = self.get_realm(options)
|
realm = self.get_realm(options)
|
||||||
user_profiles = self.get_users(options, realm)
|
user_profiles = self.get_users(options, realm)
|
||||||
|
|
||||||
if bool(user_profiles) == options["all_users"]:
|
|
||||||
self.print_help("./manage.py", "add_users_to_streams")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
stream_names = set([stream.strip() for stream in options["streams"].split(",")])
|
stream_names = set([stream.strip() for stream in options["streams"].split(",")])
|
||||||
|
|
||||||
# If all_users flag is passed user list should not be passed and vice versa.
|
|
||||||
if options["all_users"]:
|
|
||||||
user_profiles = UserProfile.objects.filter(realm=realm)
|
|
||||||
|
|
||||||
for stream_name in set(stream_names):
|
for stream_name in set(stream_names):
|
||||||
for user_profile in user_profiles:
|
for user_profile in user_profiles:
|
||||||
stream, _ = create_stream_if_needed(realm, stream_name)
|
stream, _ = create_stream_if_needed(realm, stream_name)
|
||||||
|
|||||||
@@ -20,30 +20,16 @@ class Command(ZulipBaseCommand):
|
|||||||
type=str,
|
type=str,
|
||||||
help='A stream name.')
|
help='A stream name.')
|
||||||
|
|
||||||
parser.add_argument('-a', '--all-users',
|
|
||||||
dest='all_users',
|
|
||||||
action="store_true",
|
|
||||||
default=False,
|
|
||||||
help='Remove all users in this realm from this stream.')
|
|
||||||
|
|
||||||
self.add_realm_args(parser, True)
|
self.add_realm_args(parser, True)
|
||||||
self.add_user_list_args(parser)
|
self.add_user_list_args(parser, all_users_help='Remove all users in realm from this stream.')
|
||||||
|
|
||||||
def handle(self, **options):
|
def handle(self, **options):
|
||||||
# type: (**Any) -> None
|
# type: (**Any) -> None
|
||||||
realm = self.get_realm(options)
|
realm = self.get_realm(options)
|
||||||
user_profiles = self.get_users(options, realm)
|
user_profiles = self.get_users(options, realm)
|
||||||
|
|
||||||
if bool(user_profiles) == options["all_users"]:
|
|
||||||
self.print_help("./manage.py", "remove_users_from_stream")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
stream_name = options["stream"].strip()
|
stream_name = options["stream"].strip()
|
||||||
stream = get_stream(stream_name, realm)
|
stream = get_stream(stream_name, realm)
|
||||||
|
|
||||||
if options["all_users"]:
|
|
||||||
user_profiles = UserProfile.objects.filter(realm=realm)
|
|
||||||
|
|
||||||
result = bulk_remove_subscriptions(user_profiles, [stream])
|
result = bulk_remove_subscriptions(user_profiles, [stream])
|
||||||
not_subscribed = result[1]
|
not_subscribed = result[1]
|
||||||
not_subscribed_users = {tup[0] for tup in not_subscribed}
|
not_subscribed_users = {tup[0] for tup in not_subscribed}
|
||||||
|
|||||||
@@ -23,7 +23,8 @@ class Command(ZulipBaseCommand):
|
|||||||
"If you pass 'realm' will send to everyone on realm."
|
"If you pass 'realm' will send to everyone on realm."
|
||||||
"Don't forget to specify the realm using -r or --realm flag.")
|
"Don't forget to specify the realm using -r or --realm flag.")
|
||||||
self.add_user_list_args(parser,
|
self.add_user_list_args(parser,
|
||||||
help="Email addresses of user(s) to send password reset emails to.")
|
help="Email addresses of user(s) to send password reset emails to.",
|
||||||
|
all_users_arg=False)
|
||||||
self.add_realm_args(parser)
|
self.add_realm_args(parser)
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
@@ -33,9 +34,14 @@ class Command(ZulipBaseCommand):
|
|||||||
|
|
||||||
if bool(users) == bool(options["target"]):
|
if bool(users) == bool(options["target"]):
|
||||||
self.print_help("./manage.py", "send_password_reset_email")
|
self.print_help("./manage.py", "send_password_reset_email")
|
||||||
|
print(self.style.ERROR("Please pass either --target or --users."))
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
if options["target"] == "realm":
|
if options["target"] == "realm":
|
||||||
|
if realm is None:
|
||||||
|
self.print_help("./manage.py", "send_password_reset_email")
|
||||||
|
print(self.style.ERROR("Please pass the realm."))
|
||||||
|
exit(1)
|
||||||
users = UserProfile.objects.filter(realm=realm, is_active=True, is_bot=False,
|
users = UserProfile.objects.filter(realm=realm, is_active=True, is_bot=False,
|
||||||
is_mirror_dummy=False)
|
is_mirror_dummy=False)
|
||||||
elif options["target"] == "server":
|
elif options["target"] == "server":
|
||||||
|
|||||||
@@ -18,29 +18,13 @@ class Command(ZulipBaseCommand):
|
|||||||
|
|
||||||
self.add_user_list_args(parser,
|
self.add_user_list_args(parser,
|
||||||
help='Turn off digests for this comma-separated '
|
help='Turn off digests for this comma-separated '
|
||||||
'list of email addresses.')
|
'list of email addresses.',
|
||||||
|
all_users_help="Turn off digests for everyone in realm.")
|
||||||
parser.add_argument('-a', '--all-users',
|
|
||||||
dest='all_users',
|
|
||||||
action="store_true",
|
|
||||||
default=False,
|
|
||||||
help="Turn off digests for everyone in a realm. "
|
|
||||||
"Don't forget to specify the realm.")
|
|
||||||
|
|
||||||
def handle(self, **options):
|
def handle(self, **options):
|
||||||
# type: (**str) -> None
|
# type: (**str) -> None
|
||||||
realm = self.get_realm(options)
|
realm = self.get_realm(options)
|
||||||
user_profiles = self.get_users(options, realm)
|
user_profiles = self.get_users(options, realm)
|
||||||
all_users = options["all_users"]
|
|
||||||
|
|
||||||
# If all_users flag is passed user list should not be passed and vice versa.
|
|
||||||
# If all_users flag is passed it is manadatory to pass the realm.
|
|
||||||
if (bool(user_profiles) == all_users) or (all_users and not realm):
|
|
||||||
self.print_help("./manage.py", "turn_off_digests")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
if all_users:
|
|
||||||
user_profiles = UserProfile.objects.filter(realm=realm)
|
|
||||||
|
|
||||||
print("Turned off digest emails for:")
|
print("Turned off digest emails for:")
|
||||||
for user_profile in user_profiles:
|
for user_profile in user_profiles:
|
||||||
|
|||||||
Reference in New Issue
Block a user