Fixed #28591 -- Added an error message for createsuperuser --username= (blank).

This commit is contained in:
Hasan Ramezani 2017-09-12 23:17:07 +04:30 committed by Tim Graham
parent 22ff4f81b1
commit 6aec130a4c
2 changed files with 20 additions and 1 deletions

View File

@ -68,6 +68,7 @@ class Command(BaseCommand):
# Same as user_data but with foreign keys as fake model instances # Same as user_data but with foreign keys as fake model instances
# instead of raw IDs. # instead of raw IDs.
fake_user_data = {} fake_user_data = {}
verbose_field_name = self.username_field.verbose_name
# Do quick and dirty validation if --noinput # Do quick and dirty validation if --noinput
if not options['interactive']: if not options['interactive']:
@ -96,7 +97,6 @@ class Command(BaseCommand):
raise NotRunningInTTYException("Not running in a TTY") raise NotRunningInTTYException("Not running in a TTY")
# Get a username # Get a username
verbose_field_name = self.username_field.verbose_name
while username is None: while username is None:
input_msg = capfirst(verbose_field_name) input_msg = capfirst(verbose_field_name)
if default_username: if default_username:
@ -121,6 +121,9 @@ class Command(BaseCommand):
self.stderr.write("Error: That %s is already taken." % verbose_field_name) self.stderr.write("Error: That %s is already taken." % verbose_field_name)
username = None username = None
if not username:
raise CommandError('%s cannot be blank.' % capfirst(verbose_field_name))
for field_name in self.UserModel.REQUIRED_FIELDS: for field_name in self.UserModel.REQUIRED_FIELDS:
field = self.UserModel._meta.get_field(field_name) field = self.UserModel._meta.get_field(field_name)
user_data[field_name] = options[field_name] user_data[field_name] = options[field_name]

View File

@ -523,6 +523,22 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
test(self) test(self)
def test_blank_username(self):
"""Creation fails if --username is blank."""
new_io = StringIO()
def test(self):
with self.assertRaisesMessage(CommandError, 'Username cannot be blank.'):
call_command(
'createsuperuser',
username='',
stdin=MockTTY(),
stdout=new_io,
stderr=new_io,
)
test(self)
def test_invalid_username(self): def test_invalid_username(self):
"""Creation fails if the username fails validation.""" """Creation fails if the username fails validation."""
user_field = User._meta.get_field(User.USERNAME_FIELD) user_field = User._meta.get_field(User.USERNAME_FIELD)