Exemplo n.º 1
0
    def handle(self, *args, **options):
        """Create admin account with admin password.

        This is useful mostly for setup inside appliances, when user wants to be able to
        login remotely and change password then.
        """
        email = options["email"]
        if not email:
            email = "*****@*****.**"
            self.stdout.write(
                f"Blank e-mail for admin, using {email} instead!")
        try:
            user = User.objects.filter(
                Q(username=options["username"]) | Q(email=email)).get()
        except User.DoesNotExist:
            user = None
        except User.MultipleObjectsReturned:
            raise CommandError("Multiple users matched given parameters!")

        if user and not options["update"]:
            raise CommandError(
                "User exists, specify --update to update existing")

        if options["no_password"]:
            password = None
        elif options["password"]:
            password = options["password"]
        else:
            password = make_password(13)
            self.stdout.write(f"Using generated password: {password}")

        if user and options["update"]:
            self.stdout.write(f"Updating user {user.username}")
            user.email = email
            if password is not None and not user.check_password(password):
                user.set_password(password)
        else:
            self.stdout.write("Creating user {}".format(options["username"]))
            user = User.objects.create_user(options["username"], email,
                                            password)
        user.full_name = options["name"]
        user.is_superuser = True
        user.is_active = True
        user.save()
Exemplo n.º 2
0
    def handle(self, *args, **options):
        """Create admin account with admin password.

        This is useful mostly for setup inside appliances, when user wants
        to be able to login remotely and change password then.
        """
        try:
            user = User.objects.filter(
                Q(username=options['username'])
                | Q(email=options['email'])).get()
        except User.DoesNotExist:
            user = None
        except User.MultipleObjectsReturned:
            raise CommandError('Multiple users matched given parameters!')

        if user and not options['update']:
            raise CommandError(
                'User exists, specify --update to update existing')

        if options['no_password']:
            password = None
        elif options['password']:
            password = options['password']
        else:
            password = make_password(13)
            self.stdout.write('Using generated password: {}'.format(password))

        if user and options['update']:
            self.stdout.write('Updating user {}'.format(user.username))
            user.email = options['email']
            if password is not None and not user.check_password(password):
                user.set_password(password)
        else:
            self.stdout.write('Creating user {}'.format(options['username']))
            user = User.objects.create_user(options['username'],
                                            options['email'], password)
        user.full_name = options['name']
        user.is_superuser = True
        user.is_active = True
        user.save()