Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Fixed #19745 -- Forced resolution of verbose names in creates…

…upersuser

Thanks Baptiste Mispelon for the report and Preston Holmes for the review.
Backport of 2390fe3 from master.
  • Loading branch information...
commit 933e956ba4b1ad5f8823f0a294ffc795e949edf1 1 parent f58aae9
Claude Paroz authored
9  django/contrib/auth/management/commands/createsuperuser.py
@@ -11,7 +11,7 @@
11 11
 from django.core import exceptions
12 12
 from django.core.management.base import BaseCommand, CommandError
13 13
 from django.db import DEFAULT_DB_ALIAS
14  
-from django.utils.encoding import force_str
  14
+from django.utils.encoding import force_str, force_text
15 15
 from django.utils.six.moves import input
16 16
 from django.utils.text import capfirst
17 17
 
@@ -80,9 +80,10 @@ def handle(self, *args, **options):
80 80
             try:
81 81
 
82 82
                 # Get a username
  83
+                verbose_field_name = force_text(self.username_field.verbose_name)
83 84
                 while username is None:
84 85
                     if not username:
85  
-                        input_msg = capfirst(self.username_field.verbose_name)
  86
+                        input_msg = capfirst(verbose_field_name)
86 87
                         if default_username:
87 88
                             input_msg = "%s (leave blank to use '%s')" % (
88 89
                                 input_msg, default_username)
@@ -102,14 +103,14 @@ def handle(self, *args, **options):
102 103
                         pass
103 104
                     else:
104 105
                         self.stderr.write("Error: That %s is already taken." %
105  
-                                self.username_field.verbose_name)
  106
+                                verbose_field_name)
106 107
                         username = None
107 108
 
108 109
                 for field_name in self.UserModel.REQUIRED_FIELDS:
109 110
                     field = self.UserModel._meta.get_field(field_name)
110 111
                     user_data[field_name] = options.get(field_name)
111 112
                     while user_data[field_name] is None:
112  
-                        raw_value = input(force_str('%s: ' % capfirst(field.verbose_name)))
  113
+                        raw_value = input(force_str('%s: ' % capfirst(force_text(field.verbose_name))))
113 114
                         try:
114 115
                             user_data[field_name] = field.clean(raw_value, None)
115 116
                         except exceptions.ValidationError as e:
20  django/contrib/auth/tests/basic.py
@@ -12,6 +12,7 @@
12 12
 from django.core.management import call_command
13 13
 from django.test import TestCase
14 14
 from django.test.utils import override_settings
  15
+from django.utils.encoding import force_str
15 16
 from django.utils.six import StringIO
16 17
 
17 18
 
@@ -30,8 +31,13 @@ def mock_input(prompt):
30 31
                 # prompt should be encoded in Python 2. This line will raise an
31 32
                 # Exception if prompt contains unencoded non-ascii on Python 2.
32 33
                 prompt = str(prompt)
33  
-                if str('leave blank to use') in prompt:
34  
-                    return inputs['username']
  34
+                assert str('__proxy__') not in prompt
  35
+                response = ''
  36
+                for key, val in inputs.items():
  37
+                    if force_str(key) in prompt.lower():
  38
+                        response = val
  39
+                        break
  40
+                return response
35 41
 
36 42
             old_getpass = createsuperuser.getpass
37 43
             old_input = createsuperuser.input
@@ -178,16 +184,20 @@ def test_createsuperuser_nolocale(self):
178 184
         u = User.objects.get(username="nolocale@somewhere.org")
179 185
         self.assertEqual(u.email, 'nolocale@somewhere.org')
180 186
 
181  
-    @mock_inputs({'password': "nopasswd", 'username': 'foo'})
  187
+    @mock_inputs({
  188
+        'password': "nopasswd",
  189
+        'uživatel': 'foo',  # username (cz)
  190
+        'email': 'nolocale@somewhere.org'})
182 191
     def test_createsuperuser_non_ascii_verbose_name(self):
  192
+        # Aliased so the string doesn't get extracted
  193
+        from django.utils.translation import ugettext_lazy as ulazy
183 194
         username_field = User._meta.get_field('username')
184 195
         old_verbose_name = username_field.verbose_name
185  
-        username_field.verbose_name = 'uživatel'
  196
+        username_field.verbose_name = ulazy('uživatel')
186 197
         new_io = StringIO()
187 198
         try:
188 199
             call_command("createsuperuser",
189 200
                 interactive=True,
190  
-                email="nolocale@somewhere.org",
191 201
                 stdout=new_io
192 202
             )
193 203
         finally:

0 notes on commit 933e956

Please sign in to comment.
Something went wrong with that request. Please try again.