def test_migrate_users(self): today = timezone.now() yesterday = today - datetime.timedelta(days=1) cool = Group.objects.create(name='Cool People') not_cool = Group.objects.create(name='Not So Cool') # create a user with "an alias email" alias = User.objects.create(username='******', email='*****@*****.**') # and give it some fancy permissions alias.is_staff = True alias.is_superuser = True alias.last_login = today alias.save() alias.groups.add(cool) alias.groups.add(not_cool) real = User.objects.create(username='******', email='*****@*****.**') real.last_login = yesterday real.save() # just one group real.groups.add(not_cool) alias2 = User.objects.create(username='******', email='*****@*****.**') combos = ( [alias.email.upper(), real.email.capitalize()], [alias2.email, '*****@*****.**'], ['*****@*****.**', '*****@*****.**'], ) out = StringIO() with redirect_stdout(out): migrate_users(combos) # Check the stdout blather assert re.findall( 'NEED TO MIGRATE [email protected]\s+TO [email protected]', out.getvalue()) assert re.findall( 'NEED TO MIGRATE [email protected]\s+TO [email protected]', out.getvalue()) # Should still only be 3 users assert User.objects.all().count() == 3 # It should have now "copied" all the good stuff of `alias` # over to `real`. alias = User.objects.get(id=alias.id) # reload assert not alias.is_active real = User.objects.get(id=real.id) assert real.is_staff assert real.is_superuser assert cool in real.groups.all() assert not_cool in real.groups.all() assert real.last_login == today # And the `alias2` user should just simply have its email changed. assert not User.objects.filter(email__iexact='*****@*****.**') assert User.objects.filter(email__iexact='*****@*****.**')
def handle(self, **options): first = True combos = [] with open(options['csvfile']) as f: reader = csv.reader(f) for row in reader: if first and not options['include_first_row']: first = False else: assert len(row) == 2, len(row) combos.append(row) migrate_users(combos, dry_run=options['dry_run'])
def test_migrate_users(self): today = timezone.now() yesterday = today - datetime.timedelta(days=1) cool = Group.objects.create(name='Cool People') not_cool = Group.objects.create(name='Not So Cool') # create a user with "an alias email" alias = User.objects.create(username='******', email='*****@*****.**') # and give it some fancy permissions alias.is_staff = True alias.is_superuser = True alias.last_login = today alias.save() alias.groups.add(cool) alias.groups.add(not_cool) real = User.objects.create(username='******', email='*****@*****.**') real.last_login = yesterday real.save() # just one group real.groups.add(not_cool) alias2 = User.objects.create(username='******', email='*****@*****.**') combos = ( [alias.email.upper(), real.email.capitalize()], [alias2.email, '*****@*****.**'], ['*****@*****.**', '*****@*****.**'], ) out = StringIO() with redirect_stdout(out): migrate_users(combos) # Check the stdout blather ok_(re.findall( 'NEED TO MIGRATE [email protected]\s+TO [email protected]', out.getvalue() )) ok_(re.findall( 'NEED TO MIGRATE [email protected]\s+TO [email protected]', out.getvalue() )) # Should still only be 3 users assert User.objects.all().count() == 3 # It should have now "copied" all the good stuff of `alias` # over to `real`. alias = User.objects.get(id=alias.id) # reload ok_(not alias.is_active) real = User.objects.get(id=real.id) ok_(real.is_staff) ok_(real.is_superuser) ok_(cool in real.groups.all()) ok_(not_cool in real.groups.all()) eq_(real.last_login, today) # And the `alias2` user should just simply have its email changed. ok_(not User.objects.filter(email__iexact='*****@*****.**')) ok_(User.objects.filter(email__iexact='*****@*****.**'))