diff options
| author | Baptiste Mispelon <bmispelon@gmail.com> | 2014-02-17 04:55:03 +0100 |
|---|---|---|
| committer | Baptiste Mispelon <bmispelon@gmail.com> | 2014-02-17 04:58:31 +0100 |
| commit | a7639722f57eebc71cd396512e1fe5c86bebbf15 (patch) | |
| tree | ed1ee501160d8d6b8787333a27388c04eb420e63 | |
| parent | 81cd3a74609086a34ced5a5af65768f58cee2c87 (diff) | |
Fixed #7423 -- Skip superuser creation when not running in a TTY.
Thanks to trac user galaxy4sale for the original report
and to AeroNotix for the patch.
| -rw-r--r-- | django/contrib/auth/management/commands/createsuperuser.py | 25 | ||||
| -rw-r--r-- | django/contrib/auth/tests/test_management.py | 21 |
2 files changed, 41 insertions, 5 deletions
diff --git a/django/contrib/auth/management/commands/createsuperuser.py b/django/contrib/auth/management/commands/createsuperuser.py index ac2835d6b3..9d18dd430d 100644 --- a/django/contrib/auth/management/commands/createsuperuser.py +++ b/django/contrib/auth/management/commands/createsuperuser.py @@ -17,6 +17,10 @@ from django.utils.six.moves import input from django.utils.text import capfirst +class NotRunningInTTYException(Exception): + pass + + class Command(BaseCommand): def __init__(self, *args, **kwargs): @@ -80,6 +84,9 @@ class Command(BaseCommand): default_username = get_default_username() try: + if not self.stdin.isatty(): + raise NotRunningInTTYException("Not running in a TTY") + # Get a username verbose_field_name = self.username_field.verbose_name while username is None: @@ -136,8 +143,16 @@ class Command(BaseCommand): self.stderr.write("\nOperation cancelled.") sys.exit(1) - user_data[self.UserModel.USERNAME_FIELD] = username - user_data['password'] = password - self.UserModel._default_manager.db_manager(database).create_superuser(**user_data) - if verbosity >= 1: - self.stdout.write("Superuser created successfully.") + except NotRunningInTTYException: + self.stdout.write( + "Superuser creation skipped due to not running in a TTY. " + "You can run `manage.py createsuperuser` in your project " + "to create one manually." + ) + + if username: + user_data[self.UserModel.USERNAME_FIELD] = username + user_data['password'] = password + self.UserModel._default_manager.db_manager(database).create_superuser(**user_data) + if verbosity >= 1: + self.stdout.write("Superuser created successfully.") diff --git a/django/contrib/auth/tests/test_management.py b/django/contrib/auth/tests/test_management.py index f8af6829b5..ff70d19bc1 100644 --- a/django/contrib/auth/tests/test_management.py +++ b/django/contrib/auth/tests/test_management.py @@ -191,6 +191,27 @@ class CreatesuperuserManagementCommandTestCase(TestCase): self.assertEqual(CustomUser._default_manager.count(), 0) + def test_skip_if_not_in_TTY(self): + """ + If the command is not called from a TTY, it should be skipped and a + message should be displayed (#7423). + """ + class FakeStdin(object): + """A fake stdin object that has isatty() return False.""" + def isatty(self): + return False + + out = StringIO() + call_command( + "createsuperuser", + stdin=FakeStdin(), + stdout=out, + interactive=True, + ) + + self.assertEqual(User._default_manager.count(), 0) + self.assertIn("Superuser creation skipped", out.getvalue()) + class CustomUserModelValidationTestCase(TestCase): @override_settings(AUTH_USER_MODEL='auth.CustomUserNonListRequiredFields') |
