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
|
2013-08-05 15:34:24 +02:00
|
|
|
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.actions 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
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2020-09-02 21:24:05 +02:00
|
|
|
parser.add_argument('data_file', metavar='<data file>',
|
2015-08-21 02:10:41 +02:00
|
|
|
help="file containing rows of the form <email>,<desired name>")
|
2017-07-08 12:39:10 +02:00
|
|
|
self.add_realm_args(parser, True)
|
2013-08-05 15:34:24 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: str) -> None:
|
2015-08-21 02:10:41 +02: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")
|