Fixed #19745 -- Forced resolution of verbose names in createsupersuser

Thanks Baptiste Mispelon for the report and Preston Holmes for the review.
This commit is contained in:
Claude Paroz 2013-02-06 10:02:41 +01:00
parent ea425ebcb2
commit 2390fe3f4f
2 changed files with 20 additions and 9 deletions

View File

@ -11,7 +11,7 @@ from django.contrib.auth.management import get_default_username
from django.core import exceptions from django.core import exceptions
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from django.db import DEFAULT_DB_ALIAS from django.db import DEFAULT_DB_ALIAS
from django.utils.encoding import force_str from django.utils.encoding import force_str, force_text
from django.utils.six.moves import input from django.utils.six.moves import input
from django.utils.text import capfirst from django.utils.text import capfirst
@ -80,9 +80,10 @@ class Command(BaseCommand):
try: try:
# Get a username # Get a username
verbose_field_name = force_text(self.username_field.verbose_name)
while username is None: while username is None:
if not username: if not username:
input_msg = capfirst(self.username_field.verbose_name) input_msg = capfirst(verbose_field_name)
if default_username: if default_username:
input_msg = "%s (leave blank to use '%s')" % ( input_msg = "%s (leave blank to use '%s')" % (
input_msg, default_username) input_msg, default_username)
@ -102,14 +103,14 @@ class Command(BaseCommand):
pass pass
else: else:
self.stderr.write("Error: That %s is already taken." % self.stderr.write("Error: That %s is already taken." %
self.username_field.verbose_name) verbose_field_name)
username = None username = None
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.get(field_name) user_data[field_name] = options.get(field_name)
while user_data[field_name] is None: while user_data[field_name] is None:
raw_value = input(force_str('%s: ' % capfirst(field.verbose_name))) raw_value = input(force_str('%s: ' % capfirst(force_text(field.verbose_name))))
try: try:
user_data[field_name] = field.clean(raw_value, None) user_data[field_name] = field.clean(raw_value, None)
except exceptions.ValidationError as e: except exceptions.ValidationError as e:

View File

@ -12,6 +12,7 @@ from django.core.exceptions import ImproperlyConfigured
from django.core.management import call_command from django.core.management import call_command
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings from django.test.utils import override_settings
from django.utils.encoding import force_str
from django.utils.six import StringIO from django.utils.six import StringIO
@ -30,8 +31,13 @@ def mock_inputs(inputs):
# prompt should be encoded in Python 2. This line will raise an # prompt should be encoded in Python 2. This line will raise an
# Exception if prompt contains unencoded non-ascii on Python 2. # Exception if prompt contains unencoded non-ascii on Python 2.
prompt = str(prompt) prompt = str(prompt)
if str('leave blank to use') in prompt: assert str('__proxy__') not in prompt
return inputs['username'] response = ''
for key, val in inputs.items():
if force_str(key) in prompt.lower():
response = val
break
return response
old_getpass = createsuperuser.getpass old_getpass = createsuperuser.getpass
old_input = createsuperuser.input old_input = createsuperuser.input
@ -178,16 +184,20 @@ class BasicTestCase(TestCase):
u = User.objects.get(username="nolocale@somewhere.org") u = User.objects.get(username="nolocale@somewhere.org")
self.assertEqual(u.email, 'nolocale@somewhere.org') self.assertEqual(u.email, 'nolocale@somewhere.org')
@mock_inputs({'password': "nopasswd", 'username': 'foo'}) @mock_inputs({
'password': "nopasswd",
'uživatel': 'foo', # username (cz)
'email': 'nolocale@somewhere.org'})
def test_createsuperuser_non_ascii_verbose_name(self): def test_createsuperuser_non_ascii_verbose_name(self):
# Aliased so the string doesn't get extracted
from django.utils.translation import ugettext_lazy as ulazy
username_field = User._meta.get_field('username') username_field = User._meta.get_field('username')
old_verbose_name = username_field.verbose_name old_verbose_name = username_field.verbose_name
username_field.verbose_name = 'uživatel' username_field.verbose_name = ulazy('uživatel')
new_io = StringIO() new_io = StringIO()
try: try:
call_command("createsuperuser", call_command("createsuperuser",
interactive=True, interactive=True,
email="nolocale@somewhere.org",
stdout=new_io stdout=new_io
) )
finally: finally: