2017-09-03 17:35:53 +02:00
|
|
|
import logging
|
2017-11-16 00:43:27 +01:00
|
|
|
from argparse import ArgumentParser
|
2018-05-10 19:30:04 +02:00
|
|
|
from typing import Any, List, Optional
|
2017-08-15 15:11:58 +02:00
|
|
|
|
2017-09-03 20:24:56 +02:00
|
|
|
from django.db import connection
|
2017-08-15 15:11:58 +02:00
|
|
|
|
2017-09-01 13:15:32 +02:00
|
|
|
from zerver.lib.fix_unreads import fix
|
2020-01-14 21:59:46 +01:00
|
|
|
from zerver.lib.management import CommandError, ZulipBaseCommand
|
2017-11-16 00:43:27 +01:00
|
|
|
from zerver.models import Realm, UserProfile
|
2017-08-15 15:11:58 +02:00
|
|
|
|
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>',
|
|
|
|
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',
|
|
|
|
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,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
is_bot=False,
|
2017-08-15 18:32:42 +02:00
|
|
|
))
|
|
|
|
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
|
|
|
|
2018-05-10 19:30:04 +02:00
|
|
|
def fix_emails(self, realm: Optional[Realm], emails: List[str]) -> None:
|
2017-08-15 18:32:42 +02:00
|
|
|
|
|
|
|
for email in emails:
|
|
|
|
try:
|
|
|
|
user_profile = self.get_user(email, realm)
|
|
|
|
except CommandError:
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f"e-mail {email} doesn't exist in the realm {realm}, skipping")
|
2017-08-15 18:32:42 +02:00
|
|
|
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:
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError('You must specify a realm if you choose the --all option.')
|
2017-08-15 18:32:42 +02:00
|
|
|
|
|
|
|
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'])
|