From a7639722f57eebc71cd396512e1fe5c86bebbf15 Mon Sep 17 00:00:00 2001 From: Baptiste Mispelon Date: Mon, 17 Feb 2014 04:55:03 +0100 Subject: [PATCH] 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. --- .../management/commands/createsuperuser.py | 25 +++++++++++++++---- 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 ac2835d6b31..9d18dd430d9 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 f8af6829b54..ff70d19bc1f 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')