2017-11-16 00:43:27 +01:00
|
|
|
from argparse import ArgumentParser
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2017-07-08 12:39:10 +02:00
|
|
|
from django.core.management.base import CommandError
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2013-08-05 15:34:24 +02:00
|
|
|
|
2022-04-14 23:49:26 +02:00
|
|
|
from zerver.actions.user_settings import do_change_full_name
|
2017-07-08 12:39:10 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2013-08-05 15:34:24 +02:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-07-08 12:39:10 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2015-08-21 02:10:41 +02:00
|
|
|
help = """Change the names for many users."""
|
2013-08-05 15:34:24 +02:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"data_file",
|
|
|
|
metavar="<data file>",
|
2021-02-12 08:19:30 +01:00
|
|
|
help="file containing rows of the form <email>,<desired name>",
|
|
|
|
)
|
2021-05-10 21:29:25 +02:00
|
|
|
self.add_realm_args(parser, required=True)
|
2013-08-05 15:34:24 +02:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: str) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
data_file = options["data_file"]
|
2017-07-08 12:39:10 +02:00
|
|
|
realm = self.get_realm(options)
|
2020-04-09 21:51:58 +02:00
|
|
|
with open(data_file) as f:
|
2013-08-05 15:34:24 +02:00
|
|
|
for line in f:
|
|
|
|
email, new_name = line.strip().split(",", 1)
|
|
|
|
|
|
|
|
try:
|
2017-07-08 12:39:10 +02:00
|
|
|
user_profile = self.get_user(email, realm)
|
2013-08-05 15:34:24 +02:00
|
|
|
old_name = user_profile.full_name
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f"{email}: {old_name} -> {new_name}")
|
2017-04-07 07:28:28 +02:00
|
|
|
do_change_full_name(user_profile, new_name, None)
|
2017-07-08 12:39:10 +02:00
|
|
|
except CommandError:
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f"e-mail {email} doesn't exist in the realm {realm}, skipping")
|