-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2547543
commit 2eb8d3f
Showing
8 changed files
with
104 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import logging | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.core.management.base import BaseCommand | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CommonCreateUserCommand(BaseCommand): | ||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--username", | ||
type=str, | ||
help="Username of the superuser (default: email)", | ||
required=False, | ||
) | ||
parser.add_argument( | ||
"--first_name", | ||
type=str, | ||
help="First name of the superuser", | ||
required=False, | ||
) | ||
parser.add_argument( | ||
"--last_name", | ||
type=str, | ||
help="Last name of the superuser", | ||
required=False, | ||
) | ||
|
||
@classmethod | ||
def _create_user( | ||
cls, | ||
admin_email: str, | ||
password: str, | ||
is_superuser: bool, | ||
is_staff: bool, | ||
username: str = None, | ||
first_name: str = "Admin", | ||
last_name: str = "Admin", | ||
): | ||
if not password: | ||
raise ValueError("Password is required. Please set the proper variables.") | ||
|
||
user_model = get_user_model() | ||
|
||
if user_model.objects.filter(email=admin_email).exists(): | ||
logger.warning("Super admin already exists") | ||
return None | ||
|
||
user = user_model( | ||
email=admin_email, | ||
username=username or admin_email, | ||
first_name=first_name, | ||
last_name=last_name, | ||
is_active=True, | ||
is_superuser=is_superuser, | ||
is_staff=is_staff, | ||
) | ||
user.set_password(password) | ||
|
||
user.save() | ||
|
||
logger.info("Super admin created successfully") | ||
|
||
return user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,27 @@ | ||
import logging | ||
|
||
import environ | ||
from django.contrib.auth import get_user_model | ||
from django.core.management.base import BaseCommand | ||
from django.conf import settings | ||
|
||
from ._private.seed_user import CommonCreateUserCommand | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
class Command(CommonCreateUserCommand): | ||
help = "Command to create a superuser" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--email", | ||
type=str, | ||
help="Email of the superuser", | ||
required=True, | ||
) | ||
parser.add_argument( | ||
"--username", | ||
type=str, | ||
help="Username of the superuser (default: email)", | ||
required=False, | ||
) | ||
parser.add_argument( | ||
"--first_name", | ||
type=str, | ||
help="First name of the superuser", | ||
required=False, | ||
) | ||
parser.add_argument( | ||
"--last_name", | ||
type=str, | ||
help="Last name of the superuser", | ||
required=False, | ||
) | ||
|
||
def handle(self, *args, **kwargs): | ||
user_model = get_user_model() | ||
env = environ.Env() | ||
|
||
admin_email = kwargs.get("email") | ||
admin_username = kwargs.get("username") or admin_email | ||
admin_first_name = kwargs.get("first_name") or "" | ||
admin_last_name = kwargs.get("last_name") or "" | ||
|
||
if user_model.objects.filter(email=admin_email).exists(): | ||
logger.info("Super admin already exists") | ||
return 0 | ||
kwargs["last_name"] = "Super" | ||
kwargs["first_name"] = "User" | ||
|
||
superuser = user_model( | ||
email=admin_email, | ||
username=admin_username, | ||
first_name=admin_first_name, | ||
last_name=admin_last_name, | ||
is_active=True, | ||
self._create_user( | ||
admin_email=settings.DJANGO_ADMIN_EMAIL, | ||
password=settings.DJANGO_ADMIN_PASSWORD, | ||
is_superuser=True, | ||
is_staff=True, | ||
first_name=kwargs.get("first_name", ""), | ||
last_name=kwargs.get("last_name", ""), | ||
) | ||
superuser.set_password(env.str("DJANGO_ADMIN_PASSWORD")) | ||
|
||
superuser.save() | ||
|
||
logger.info("Super admin created successfully") | ||
|
||
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters