
from django.core.management import BaseCommand
from django.contrib.auth.models import User
from django.db.models.signals import pre_save, post_save
import sys

class Command(BaseCommand):
    help = "Turn user into an administrator <user_id> is a numeric user id of the account"
    args = '<user id>'

    def add_arguments(self, parser):
        parser.add_argument('user_id', help="the numeric user_id of the existing user that shall become an admin")
        parser.add_argument('--noinput', action='store_false', dest='interactive', default=True,
            help='Tells to NOT prompt the user for input of any kind.')

    def get_user(self, uid_str):
        try:
            uid = int(uid_str)
            return User.objects.get(id=uid)
        except User.DoesNotExist:
            print('sorry there is no user with id=%d' % uid)
            sys.exit(1)
        except ValueError:
            print('user id must be integer, have %s' % uid_str)
            sys.exit(1)

    #def parse_arguments(self, arguments):
    #    if len(arguments) != 1:
    #        print('argument for this command id <user_id>')
    #        sys.exit(1)
    #    self.user = self.get_user(arguments[0])

    def confirm_action(self):
        u = self.user
        print('')
        prompt = 'Do you really wish to make user (id=%d, name=%s) a site administrator? yes/no: ' \
                % (u.id, u.username)
        str = input(prompt)
        if str != 'yes':
            print('action canceled')
            sys.exit(1)

    def remove_signals(self):
        pre_save.receivers = []
        post_save.receivers = []

    def handle(self, *arguments, **options):
        #destroy pre_save and post_save signals
        #self.parse_arguments(arguments)
        if options.get('user_id') is None:
            print('argument for this command id <user_id>')
            sys.exit(1)
        self.user = self.get_user(options.get('user_id'))

        if options.get('interactive') is True:
            self.confirm_action()

        self.remove_signals()

        self.user.is_active = True
        self.user.set_status('d')
        self.user.is_staff = True
        self.user.is_superuser = True
        self.user.save()
