2017-07-21 10:57:55 +02:00
|
|
|
|
|
|
|
import sys
|
2017-11-16 00:43:27 +01:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
from typing import Any, Dict, List
|
|
|
|
|
|
|
|
from django.conf import settings
|
2017-07-21 10:57:55 +02:00
|
|
|
|
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2017-11-16 00:43:27 +01:00
|
|
|
from zerver.lib.soft_deactivation import do_soft_activate_users, \
|
|
|
|
do_soft_deactivate_users, get_users_for_soft_deactivation, logger
|
|
|
|
from zerver.models import Realm, UserProfile
|
2017-07-21 10:57:55 +02:00
|
|
|
|
|
|
|
class Command(ZulipBaseCommand):
|
|
|
|
help = """Soft activate/deactivate users. Users are recognised by there emails here."""
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2017-08-22 02:43:03 +02:00
|
|
|
self.add_realm_args(parser)
|
2017-07-21 10:57:55 +02:00
|
|
|
parser.add_argument('-d', '--deactivate',
|
|
|
|
dest='deactivate',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='Used to deactivate user/users.')
|
|
|
|
parser.add_argument('-a', '--activate',
|
|
|
|
dest='activate',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='Used to activate user/users.')
|
|
|
|
parser.add_argument('--inactive-for',
|
|
|
|
type=int,
|
|
|
|
default=28,
|
2017-10-27 02:36:54 +02:00
|
|
|
help='Number of days of inactivity before soft-deactivation')
|
2017-07-21 10:57:55 +02:00
|
|
|
parser.add_argument('users', metavar='<users>', type=str, nargs='*', default=[],
|
2017-10-27 02:36:54 +02:00
|
|
|
help="A list of user emails to soft activate/deactivate.")
|
2017-07-21 10:57:55 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: str) -> None:
|
2017-08-22 02:46:19 +02:00
|
|
|
if settings.STAGING:
|
|
|
|
print('This is a Staging server. Suppressing management command.')
|
|
|
|
sys.exit(0)
|
|
|
|
|
2017-09-26 22:23:11 +02:00
|
|
|
realm = self.get_realm(options)
|
|
|
|
user_emails = options['users']
|
|
|
|
activate = options['activate']
|
|
|
|
deactivate = options['deactivate']
|
2017-09-26 01:23:44 +02:00
|
|
|
|
2017-08-22 02:43:03 +02:00
|
|
|
filter_kwargs = {} # type: Dict[str, Realm]
|
2017-09-26 01:23:44 +02:00
|
|
|
if realm is not None:
|
2017-08-22 02:43:03 +02:00
|
|
|
filter_kwargs = dict(realm=realm)
|
|
|
|
|
2017-07-21 10:57:55 +02:00
|
|
|
if activate:
|
|
|
|
if not user_emails:
|
|
|
|
print('You need to specify at least one user to use the activate option.')
|
2017-10-06 17:48:43 +02:00
|
|
|
self.print_help("./manage.py", "soft_deactivate_users")
|
2017-07-21 10:57:55 +02:00
|
|
|
sys.exit(1)
|
2017-08-22 02:43:03 +02:00
|
|
|
|
|
|
|
users_to_activate = UserProfile.objects.filter(
|
|
|
|
email__in=user_emails,
|
|
|
|
**filter_kwargs
|
|
|
|
)
|
|
|
|
users_to_activate = list(users_to_activate)
|
|
|
|
|
2017-08-16 05:59:44 +02:00
|
|
|
if len(users_to_activate) != len(user_emails):
|
|
|
|
user_emails_found = [user.email for user in users_to_activate]
|
|
|
|
for user in user_emails:
|
|
|
|
if user not in user_emails_found:
|
2017-11-09 11:45:56 +01:00
|
|
|
raise Exception('User with email %s was not found. '
|
|
|
|
'Check if the email is correct.' % (user))
|
2017-08-22 02:43:03 +02:00
|
|
|
|
2017-08-16 06:15:56 +02:00
|
|
|
users_activated = do_soft_activate_users(users_to_activate)
|
|
|
|
logger.info('Soft Reactivated %d user(s)' % (len(users_activated)))
|
2017-07-21 10:57:55 +02:00
|
|
|
elif deactivate:
|
|
|
|
if user_emails:
|
2017-08-22 02:43:03 +02:00
|
|
|
users_to_deactivate = UserProfile.objects.filter(
|
|
|
|
email__in=user_emails,
|
|
|
|
**filter_kwargs
|
|
|
|
)
|
|
|
|
users_to_deactivate = list(users_to_deactivate)
|
|
|
|
|
2017-08-16 05:59:44 +02:00
|
|
|
if len(users_to_deactivate) != len(user_emails):
|
|
|
|
user_emails_found = [user.email for user in users_to_deactivate]
|
|
|
|
for user in user_emails:
|
|
|
|
if user not in user_emails_found:
|
2017-10-27 02:47:30 +02:00
|
|
|
raise Exception('User with email %s was not found. '
|
|
|
|
'Check if the email is correct.' % (user,))
|
2017-08-16 05:59:44 +02:00
|
|
|
print('Soft deactivating forcefully...')
|
2017-07-21 10:57:55 +02:00
|
|
|
else:
|
2017-09-26 01:23:44 +02:00
|
|
|
if realm is not None:
|
2017-08-22 02:43:03 +02:00
|
|
|
filter_kwargs = dict(user_profile__realm=realm)
|
2017-11-09 11:45:56 +01:00
|
|
|
users_to_deactivate = get_users_for_soft_deactivation(int(options['inactive_for']),
|
|
|
|
filter_kwargs)
|
2017-07-21 10:57:55 +02:00
|
|
|
|
2017-08-16 05:55:38 +02:00
|
|
|
if users_to_deactivate:
|
2017-08-16 06:15:56 +02:00
|
|
|
users_deactivated = do_soft_deactivate_users(users_to_deactivate)
|
|
|
|
logger.info('Soft Deactivated %d user(s)' % (len(users_deactivated)))
|
2017-07-21 10:57:55 +02:00
|
|
|
else:
|
2017-10-06 17:48:43 +02:00
|
|
|
self.print_help("./manage.py", "soft_deactivate_users")
|
2017-07-21 10:57:55 +02:00
|
|
|
sys.exit(1)
|