Code

Ticket #5605: lowercase-email.diff

File lowercase-email.diff, 2.3 KB (added by brodie, 6 years ago)

Updated to work with email addresses without at signs, including a test for that case

Line 
1Index: django/contrib/auth/models.py
2===================================================================
3--- django/contrib/auth/models.py       (revision 8145)
4+++ django/contrib/auth/models.py       (working copy)
5@@ -104,7 +104,12 @@
6     def create_user(self, username, email, password=None):
7         "Creates and saves a User with the given username, e-mail and password."
8         now = datetime.datetime.now()
9-        user = self.model(None, username, '', '', email.strip().lower(), 'placeholder', False, True, False, now, now)
10+        # Make only the domain lowercase.
11+        parts = email.strip().split('@', 1)
12+        if len(parts) > 1:
13+            parts[1] = parts[1].lower()
14+        email = '@'.join(parts)
15+        user = self.model(None, username, '', '', email, 'placeholder', False, True, False, now, now)
16         if password:
17             user.set_password(password)
18         else:
19Index: tests/regressiontests/auth_backends/tests.py
20===================================================================
21--- tests/regressiontests/auth_backends/tests.py        (revision 8145)
22+++ tests/regressiontests/auth_backends/tests.py        (working copy)
23@@ -75,4 +75,14 @@
24 False
25 >>> user.has_perms(['auth.test2', 'auth.test3'])
26 False
27+
28+# bug #5605, preserve the case of the user name (before the @ in the email address)
29+# when creating a user.
30+
31+>>> user = User.objects.create_user('test2', 'tesT@EXAMple.com', 'test')
32+>>> user.email
33+'tesT@example.com'
34+>>> user = User.objects.create_user('test3', 'tesT', 'test')
35+>>> user.email
36+'tesT'
37 """}
38Index: docs/authentication.txt
39===================================================================
40--- docs/authentication.txt     (revision 8145)
41+++ docs/authentication.txt     (working copy)
42@@ -168,8 +168,9 @@
43 The ``User`` model has a custom manager that has the following helper functions:
44 
45     * ``create_user(username, email, password=None)`` -- Creates, saves and
46-      returns a ``User``. The ``username``, ``email`` and ``password`` are set
47-      as given, and the ``User`` gets ``is_active=True``.
48+      returns a ``User``. The ``username`` and ``password`` are set as
49+      given, the domain portion of ``email`` gets lowercased, and the
50+      ``User`` gets ``is_active=True``.
51 
52       If no password is provided, ``set_unusable_password()`` will be called.
53