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 8e8414c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 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
19 changes: 19 additions & 0 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 @@ -148,11 +149,29 @@ def createsuperuser(
"""Create superuser."""
printing.print_success("Django: Create superuser")
config = _config.Config.from_context(context)
if not email:
with contextlib.suppress(invoke.Failure):
output = context.run(
"git config user.email",
echo=False,
hide="out",
)
if output:
email = output.stdout.replace(" ", "")
responder_email = invoke.FailingResponder(
pattern=rf"{config.django.verbose_email_name}.*: ",
response=(email or config.django.default_superuser_email) + "\n",
sentinel="That Email address is already taken.",
)
if not username:
with contextlib.suppress(invoke.Failure):
output = context.run(
"git config user.name",
echo=False,
hide="out",
)
if output:
username = output.stdout.replace(" ", "")
responder_user_name = invoke.Responder(
pattern=rf"{config.django.verbose_username_name}.*: ",
response=(username or config.django.default_superuser_username) + "\n",
Expand Down

0 comments on commit 8e8414c

Please sign in to comment.