Fixed #34542 -- Made createsuperuser handle required blank fields in non-interactive mode.

This commit is contained in:
Mateusz Więckowski 2023-07-31 18:10:32 +02:00 committed by Mariusz Felisiak
parent 0f3b1a783d
commit 5aa4c0b675
2 changed files with 35 additions and 1 deletions

View File

@ -219,11 +219,15 @@ class Command(BaseCommand):
for field_name in self.UserModel.REQUIRED_FIELDS:
env_var = "DJANGO_SUPERUSER_" + field_name.upper()
value = options[field_name] or os.environ.get(env_var)
field = self.UserModel._meta.get_field(field_name)
if not value:
if field.blank and (
options[field_name] == "" or os.environ.get(env_var) == ""
):
continue
raise CommandError(
"You must use --%s with --noinput." % field_name
)
field = self.UserModel._meta.get_field(field_name)
user_data[field_name] = field.clean(value, None)
if field.many_to_many and isinstance(user_data[field_name], str):
user_data[field_name] = [

View File

@ -968,6 +968,36 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
stderr=new_io,
)
def test_blank_email_allowed_non_interactive(self):
new_io = StringIO()
call_command(
"createsuperuser",
email="",
username="joe",
interactive=False,
stdout=new_io,
stderr=new_io,
)
self.assertEqual(new_io.getvalue().strip(), "Superuser created successfully.")
u = User.objects.get(username="joe")
self.assertEqual(u.email, "")
@mock.patch.dict(os.environ, {"DJANGO_SUPERUSER_EMAIL": ""})
def test_blank_email_allowed_non_interactive_environment_variable(self):
new_io = StringIO()
call_command(
"createsuperuser",
username="joe",
interactive=False,
stdout=new_io,
stderr=new_io,
)
self.assertEqual(new_io.getvalue().strip(), "Superuser created successfully.")
u = User.objects.get(username="joe")
self.assertEqual(u.email, "")
def test_password_validation_bypass(self):
"""
Password validation can be bypassed by entering 'y' at the prompt.