Last active
September 17, 2024 12:54
-
-
Save darwing1210/f65b3b32ea06938e71264e9b573b484f to your computer and use it in GitHub Desktop.
Django command to create users from a CSV file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
from django.core.management.base import BaseCommand, CommandError | |
from django.contrib.auth import get_user_model | |
from django.utils.crypto import get_random_string | |
class Command(BaseCommand): | |
"""manage.py import_users --csv='/Users/user/usuariosweb.csv' --encoding='iso-8859-1'""" | |
help = 'Imports users based on a CSV file' | |
def add_arguments(self, parser): | |
parser.add_argument( | |
'--csv', | |
action='store', | |
dest='file', | |
help='File path of CSV containing users list' | |
) | |
parser.add_argument( | |
'--encoding', | |
action='store', | |
dest='encoding', | |
help='encoding of file' | |
) | |
def handle(self, *args, **options): | |
User = get_user_model() | |
if options['file']: | |
with open(options['file'], encoding=options.get('encoding', 'utf-8')) as csvfile: | |
reader = csv.DictReader(csvfile) | |
created_users = existing_users = errors = 0 | |
users_with_errors = [] | |
for row in reader: | |
if row['usuario'] and row['mail']: | |
try: | |
user, created = User.objects.get_or_create( | |
username=row['usuario'], email=row['mail']) | |
if created: | |
user.first_name = row.get('nombre') | |
user.last_name = row.get('apellido') | |
user.is_active = True if row.get('habilitado') == 1 else False | |
user.save() | |
created_users += 1 | |
else: | |
existing_users += 1 | |
print('{0} - {1}'.format(row['usuario'], 'Created' if created else 'Exist')) | |
except Exception as e: | |
errors += 1 | |
users_with_errors.append(row['usuario']) | |
self.stdout.write(self.style.ERROR(e)) | |
self.stdout.write(self.style.SUCCESS( | |
'Successfully imported {0} users, ' | |
'{1} already exist, {2} users with errors'.format( | |
created_users, | |
existing_users, | |
errors | |
)) | |
) | |
if users_with_errors: | |
print('Users with errors: {0}'.format(','.join(users_with_errors))) | |
else: | |
self.stdout.write(self.style.ERROR('No file provided')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment