Code

Ticket #15778: createsuperuser.diff

File createsuperuser.diff, 1.5 KB (added by hynekcer, 3 years ago)
Line 
1diff -r 614d8a1761f0 django/contrib/auth/management/commands/createsuperuser.py
2--- a/django/contrib/auth/management/commands/createsuperuser.py        Fri Apr 15 21:49:22 2011 +0000
3+++ b/django/contrib/auth/management/commands/createsuperuser.py        Sun Apr 17 02:16:03 2011 +0200
4@@ -3,8 +3,10 @@
5 """
6 
7 import getpass
8+import locale
9 import re
10 import sys
11+import unicodedata
12 from optparse import make_option
13 from django.contrib.auth.models import User
14 from django.core import exceptions
15@@ -58,11 +60,16 @@
16 
17         # Try to determine the current system user's username to use as a default.
18         try:
19-            default_username = getpass.getuser().replace(' ', '').lower()
20-        except (ImportError, KeyError):
21+            default_username = getpass.getuser().decode(locale.getdefaultlocale()[1])
22+            default_username = unicodedata.normalize('NFKD', default_username) \
23+                                    .encode('ascii', 'ignore').replace(' ', '').lower()
24+            if not RE_VALID_USERNAME.match(default_username):
25+                default_username = ''
26+        except (ImportError, KeyError, UnicodeDecodeError):
27             # KeyError will be raised by os.getpwuid() (called by getuser())
28             # if there is no corresponding entry in the /etc/passwd file
29             # (a very restricted chroot environment, for example).
30+            # UnicodeDecodeError - preventive treatment for non-latin Windows.
31             default_username = ''
32 
33         # Determine whether the default username is taken, so we don't display