Commit 5270dade authored by Michal Čihař's avatar Michal Čihař

Add --check to user import

Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 548ca19d
...@@ -159,13 +159,16 @@ importuserdata <file.json> ...@@ -159,13 +159,16 @@ importuserdata <file.json>
Imports userdata from file created by :djadmin:`dumpuserdata` Imports userdata from file created by :djadmin:`dumpuserdata`
importusers <file.json> importusers --checck <file.json>
----------------------- --------------------------------
.. django-admin:: importusers .. django-admin:: importusers
Imports users from JSON dump of Django auth_users database. Imports users from JSON dump of Django auth_users database.
With ``--check`` it will just check whether given file can be imported and
report possible conflicts on usernames or emails.
You can dump users from existing Django installation using: You can dump users from existing Django installation using:
.. code-block:: sh .. code-block:: sh
......
...@@ -20,12 +20,20 @@ ...@@ -20,12 +20,20 @@
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import User from django.contrib.auth.models import User
from optparse import make_option
import json import json
class Command(BaseCommand): class Command(BaseCommand):
help = 'imports users from JSON dump of database' help = 'imports users from JSON dump of database'
args = '<json-file>' args = '<json-file>'
option_list = BaseCommand.option_list + (
make_option(
'--check',
action='store_true',
help='Only check import, do not actually create users'
),
)
def handle(self, *args, **options): def handle(self, *args, **options):
''' '''
...@@ -41,14 +49,23 @@ class Command(BaseCommand): ...@@ -41,14 +49,23 @@ class Command(BaseCommand):
if 'fields' in line: if 'fields' in line:
line = line['fields'] line = line['fields']
if 'is_active' in line and not line['is_active']:
continue
if not line['email'] or not line['username']:
self.stderr.write(
'Skipping {}, has blank username or email'.format(line)
)
continue
if User.objects.filter(username=line['username']).exists(): if User.objects.filter(username=line['username']).exists():
self.stdout.write( self.stderr.write(
'Skipping {}, username exists'.format(line['username']) 'Skipping {}, username exists'.format(line['username'])
) )
continue continue
if User.objects.filter(email=line['email']).exists(): if User.objects.filter(email=line['email']).exists():
self.stdout.write( self.stderr.write(
'Skipping {}, email exists'.format(line['email']) 'Skipping {}, email exists'.format(line['email'])
) )
continue continue
...@@ -61,6 +78,7 @@ class Command(BaseCommand): ...@@ -61,6 +78,7 @@ class Command(BaseCommand):
else: else:
full_name = line['first_name'] full_name = line['first_name']
if not options['check']:
User.objects.create( User.objects.create(
username=line['username'], username=line['username'],
first_name=full_name, first_name=full_name,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment