from django.core.management.base import BaseCommand
from purist.models import User
import time


class Command(BaseCommand):
    help = "Populate User.email (recovery email) with User.get_identity() for \
    users that did not add a recovery email a the time of registration."

    def add_arguments(self, parser):
        parser.add_argument('--wait',
                            type=int,
                            help='Number of seconds to wait between users. \
                            Default is 0.',
                            default=0)

    def handle(self, *args, **options):
        wait_time = options['wait']
        usrs_empty = User.objects.filter(email='')

        self.stdout.write('Processing users with no recovery email')
        for user in usrs_empty:
            user.email = user.get_identity()
            user.save()
            self.stdout.write(' User {}:'.format(user.username))
            self.stdout.write('   New recovery email: {}'.format(
                user.email))
            time.sleep(wait_time)
