from typing import Any
from django.contrib.auth import get_user_model
from django.core.management import call_command
from django.core.management.base import (
    BaseCommand,
    CommandError,
    CommandParser,
)
from .import_base import ImportUsersBase
[docs]
class Command(BaseCommand, ImportUsersBase):
    """Create a new user."""
    help = "Create new user"
[docs]
    def add_arguments(self, parser: CommandParser) -> None:
        parser.add_argument(
            "-g",
            action="store",
            dest="groups",
            default="",
            help="Add users to this group(s). Seperated by a colon e.g."
            "group1:group2:group3",
        )
        parser.add_argument(
            "-n",
            action="store",
            dest="name",
            default="",
            help="Sets the name of the user",
        )
        parser.add_argument(
            "-p",
            action="store",
            dest="password",
            const=None,
            nargs="?",
            help="Sets the password of the user",
        )
        parser.add_argument("email", help="The emails of the new user") 
[docs]
    def handle(self, *args: Any, **options: Any) -> None:  # noqa: ARG002
        # pylint: disable=invalid-name
        UserModel = get_user_model()
        if UserModel.objects.filter(email=options["email"]).exists():
            raise CommandError("User exists")
        res = {
            "Email": options["email"],
            "Name": options["name"],
            "Groups": options["groups"],
        }
        user = self.handle_user(res)
        if options["password"] is None:
            call_command(
                "changepassword", username=options["email"],
                stdout=self.stdout,
            )
        else:
            user.set_password(options["password"])
            user.is_active = True
            user.save()
        print("\033[92m" + "Successfully created a new user!" + "\033[0m")