From 2390fe3f4f8f52e24157d79b0c60247207c9716f Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 6 Feb 2013 10:02:41 +0100 Subject: [PATCH] Fixed #19745 -- Forced resolution of verbose names in createsupersuser Thanks Baptiste Mispelon for the report and Preston Holmes for the review. --- .../management/commands/createsuperuser.py | 9 +++++---- django/contrib/auth/tests/basic.py | 20 ++++++++++++++----- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/django/contrib/auth/management/commands/createsuperuser.py b/django/contrib/auth/management/commands/createsuperuser.py index 4169da248c..b3cd6f9653 100644 --- a/django/contrib/auth/management/commands/createsuperuser.py +++ b/django/contrib/auth/management/commands/createsuperuser.py @@ -11,7 +11,7 @@ from django.contrib.auth.management import get_default_username from django.core import exceptions from django.core.management.base import BaseCommand, CommandError 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.text import capfirst @@ -80,9 +80,10 @@ class Command(BaseCommand): try: # Get a username + verbose_field_name = force_text(self.username_field.verbose_name) while username is None: if not username: - input_msg = capfirst(self.username_field.verbose_name) + input_msg = capfirst(verbose_field_name) if default_username: input_msg = "%s (leave blank to use '%s')" % ( input_msg, default_username) @@ -102,14 +103,14 @@ class Command(BaseCommand): pass else: self.stderr.write("Error: That %s is already taken." % - self.username_field.verbose_name) + verbose_field_name) username = None for field_name in self.UserModel.REQUIRED_FIELDS: field = self.UserModel._meta.get_field(field_name) user_data[field_name] = options.get(field_name) 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: user_data[field_name] = field.clean(raw_value, None) except exceptions.ValidationError as e: diff --git a/django/contrib/auth/tests/basic.py b/django/contrib/auth/tests/basic.py index 8627329870..03af1fd7bb 100644 --- a/django/contrib/auth/tests/basic.py +++ b/django/contrib/auth/tests/basic.py @@ -12,6 +12,7 @@ from django.core.exceptions import ImproperlyConfigured from django.core.management import call_command from django.test import TestCase from django.test.utils import override_settings +from django.utils.encoding import force_str 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 # Exception if prompt contains unencoded non-ascii on Python 2. prompt = str(prompt) - if str('leave blank to use') in prompt: - return inputs['username'] + assert str('__proxy__') not in prompt + response = '' + for key, val in inputs.items(): + if force_str(key) in prompt.lower(): + response = val + break + return response old_getpass = createsuperuser.getpass old_input = createsuperuser.input @@ -178,16 +184,20 @@ class BasicTestCase(TestCase): u = User.objects.get(username="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): + # Aliased so the string doesn't get extracted + from django.utils.translation import ugettext_lazy as ulazy username_field = User._meta.get_field('username') old_verbose_name = username_field.verbose_name - username_field.verbose_name = 'uživatel' + username_field.verbose_name = ulazy('uživatel') new_io = StringIO() try: call_command("createsuperuser", interactive=True, - email="nolocale@somewhere.org", stdout=new_io ) finally: