2017-08-15 15:11:58 +02:00
|
|
|
|
2017-09-03 17:35:53 +02:00
|
|
|
import logging
|
2017-08-15 18:32:42 +02:00
|
|
|
import sys
|
2017-08-15 15:11:58 +02:00
|
|
|
|
2017-09-26 22:29:38 +02:00
|
|
|
from typing import Any, List, Optional, Text
|
2017-08-15 15:11:58 +02:00
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
from django.core.management.base import CommandError
|
2017-09-03 20:24:56 +02:00
|
|
|
from django.db import connection
|
2017-08-15 15:11:58 +02:00
|
|
|
|
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2017-09-01 13:15:32 +02:00
|
|
|
from zerver.lib.fix_unreads import fix
|
2017-08-29 18:12:15 +02:00
|
|
|
|
2017-08-15 15:11:58 +02:00
|
|
|
from zerver.models import (
|
2017-08-15 18:32:42 +02:00
|
|
|
Realm,
|
2017-08-15 15:11:58 +02:00
|
|
|
UserProfile
|
|
|
|
)
|
|
|
|
|
2017-09-03 17:35:53 +02:00
|
|
|
logging.getLogger('zulip.fix_unreads').setLevel(logging.INFO)
|
|
|
|
|
2017-08-15 15:11:58 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
|
|
|
help = """Fix problems related to unread counts."""
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2017-08-15 18:32:42 +02:00
|
|
|
parser.add_argument('emails',
|
|
|
|
metavar='<emails>',
|
|
|
|
type=str,
|
|
|
|
nargs='*',
|
2017-08-15 15:11:58 +02:00
|
|
|
help='email address to spelunk')
|
2017-08-15 18:32:42 +02:00
|
|
|
parser.add_argument('--all',
|
|
|
|
action='store_true',
|
|
|
|
dest='all',
|
|
|
|
default=False,
|
|
|
|
help='fix all users in specified realm')
|
2017-08-15 15:11:58 +02:00
|
|
|
self.add_realm_args(parser)
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def fix_all_users(self, realm: Realm) -> None:
|
2017-08-15 18:32:42 +02:00
|
|
|
user_profiles = list(UserProfile.objects.filter(
|
|
|
|
realm=realm,
|
|
|
|
is_bot=False
|
|
|
|
))
|
|
|
|
for user_profile in user_profiles:
|
|
|
|
fix(user_profile)
|
2017-09-03 20:24:56 +02:00
|
|
|
connection.commit()
|
2017-08-15 18:32:42 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def fix_emails(self, realm: Optional[Realm], emails: List[Text]) -> None:
|
2017-08-15 18:32:42 +02:00
|
|
|
|
|
|
|
for email in emails:
|
|
|
|
try:
|
|
|
|
user_profile = self.get_user(email, realm)
|
|
|
|
except CommandError:
|
|
|
|
print("e-mail %s doesn't exist in the realm %s, skipping" % (email, realm))
|
|
|
|
return
|
|
|
|
|
|
|
|
fix(user_profile)
|
2017-09-03 20:24:56 +02:00
|
|
|
connection.commit()
|
2017-08-15 18:32:42 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2017-08-15 15:11:58 +02:00
|
|
|
realm = self.get_realm(options)
|
2017-08-15 18:32:42 +02:00
|
|
|
|
|
|
|
if options['all']:
|
|
|
|
if realm is None:
|
|
|
|
print('You must specify a realm if you choose the --all option.')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
self.fix_all_users(realm)
|
2017-08-15 15:11:58 +02:00
|
|
|
return
|
|
|
|
|
2017-08-15 18:32:42 +02:00
|
|
|
self.fix_emails(realm, options['emails'])
|