Files
edx-platform/common/djangoapps/student/management/commands/bulk_update_email.py
Ned Batchelder d9dd10dc97 style: code cleanups from Steven Burch (#29292)
* chore: update deprecated import from collections

* chore: remove outdated imports from markdown library

as it hasn't been supported since 2.0.3 and we're on 3.x.
This was deprecated at least as early as 2012!

* docs: add docstring and remove lint-amnesty to markdown plugin

* chore: remove deprecated etree import

* style: remove unnecessary-comprehension for sets

* style: resolve a number of amnestied pylint complaints

Co-authored-by: stvn <stvn@mit.edu>
2021-11-10 07:11:57 -08:00

72 lines
2.1 KiB
Python

"""
Management command to bulk update many user's email addresses
"""
import csv
import logging
from os import path
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth import get_user_model
logger = logging.getLogger('common.djangoapps.student.management.commands.bulk_update_email')
class Command(BaseCommand):
"""
Management command to bulk update many user's email addresses
"""
help = """
Change the email address of each user specified in the csv file
csv file is expected to have one row per user with the format:
current_email_address, new_email_address
Example:
$ ... bulk_update_email csv_file_path
"""
def add_arguments(self, parser):
""" Add argument to the command parser. """
parser.add_argument(
'--csv_file_path',
required=True,
help='Csv file path'
)
def handle(self, *args, **options):
""" Main handler for the command."""
file_path = options['csv_file_path']
if not path.isfile(file_path):
raise CommandError('File not found.')
with open(file_path) as csv_file:
csv_reader = csv.reader(csv_file)
email_mappings = list(csv_reader)
successful_updates = []
failed_updates = []
for (current_email, new_email) in email_mappings:
try:
user = get_user_model().objects.get(email=current_email)
user.email = new_email
user.save()
successful_updates.append(new_email)
except Exception: # pylint: disable=broad-except
logger.exception('Unable to update account %s', current_email)
failed_updates.append(current_email)
logger.info(
'Successfully updated %s accounts. Failed to update %s accounts',
len(successful_updates),
len(failed_updates)
)
if (failed_updates): # lint-amnesty, pylint: disable=superfluous-parens
exit(-1) # lint-amnesty, pylint: disable=consider-using-sys-exit