Skip to content

Commit

Permalink
Make django.createsuperuser try to grab username and email from git…
Browse files Browse the repository at this point in the history
… config
  • Loading branch information
TheSuperiorStanislav committed Sep 5, 2024
1 parent 703cbb1 commit 63cd59a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ We follow [Semantic Versions](https://semver.org/).

## unreleased

- Make `django.createsuperuser` try to grab username and email from git config

## 1.2.1

- Restore check for main containers in `docker.up`
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,10 @@ Create superuser.

Settings:

* `default_superuser_email` default email of superuser (Default: `root@localhost`)
* `default_superuser_username` default username of superuser (Default: `root`)
* `default_superuser_email` default email of superuser.
if empty, will try to grab it from git config, before resorting to default (Default: `root@localhost`)
* `default_superuser_username` default username of superuser
if empty, will try to grab it from git config, before resorting to default (Default: `root`)
* `default_superuser_password` default password of superuser (Default: `root`)
* `verbose_email_name` verbose name for `email` field (Default: `Email address`)
* `verbose_username_name` verbose name for `username` field (Default: `Username`)
Expand Down
47 changes: 42 additions & 5 deletions saritasa_invocations/django.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import collections.abc
import contextlib
import os
import pathlib

Expand Down Expand Up @@ -146,23 +147,59 @@ def createsuperuser(
password: str = "",
) -> None:
"""Create superuser."""
printing.print_success("Django: Create superuser")
config = _config.Config.from_context(context)
email_source = "cmd"
username_source = "cmd"
password_source = "cmd" # noqa: S105
if not email:
with contextlib.suppress(invoke.Failure):
output = context.run(
"git config user.email",
echo=False,
hide="out",
)
if output and (email := output.stdout.replace(" ", "").strip()):
email_source = "git"
else:
email = config.django.default_superuser_email
email_source = "config"
if not username:
with contextlib.suppress(invoke.Failure):
output = context.run(
"git config user.name",
echo=False,
hide="out",
)
if output and (username := output.stdout.replace(" ", "").strip()):
username_source = "git"
else:
username = config.django.default_superuser_username
username_source = "config"
if not password:
password_source = "config" # noqa: S105
password = config.django.default_superuser_password

printing.print_success(
"Django: Creating superuser with the following ->\n"
f"Email: {email} from {email_source}\n"
f"Username: {username} from {username_source}\n"
f"Password: {password} from {password_source}",
)

responder_email = invoke.FailingResponder(
pattern=rf"{config.django.verbose_email_name}.*: ",
response=(email or config.django.default_superuser_email) + "\n",
response=f"{email}\n",
sentinel="That Email address is already taken.",
)
responder_user_name = invoke.Responder(
pattern=rf"{config.django.verbose_username_name}.*: ",
response=(username or config.django.default_superuser_username) + "\n",
response=f"{username}\n",
)
password_pattern = config.django.verbose_password_name
responder_password = invoke.Responder(
pattern=rf"({password_pattern}: )|({password_pattern} \(again\): )",
response=(password or config.django.default_superuser_password) + "\n",
response=f"{password}\n",
)

try:
manage(
context,
Expand Down

0 comments on commit 63cd59a

Please sign in to comment.