Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #19596 -- Use `_default_manager` instead of `objects` in the au…

…th app.

This is needed to support custom user models which don't define a manager
named `objects`.
  • Loading branch information...
commit cc4de61a2b36abf418d6f4c720d9e62c405e0612 1 parent 456f9b9
Florian Apolloner authored January 22, 2013
4  django/contrib/auth/backends.py
@@ -13,7 +13,7 @@ class ModelBackend(object):
13 13
     def authenticate(self, username=None, password=None):
14 14
         try:
15 15
             UserModel = get_user_model()
16  
-            user = UserModel.objects.get_by_natural_key(username)
  16
+            user = UserModel._default_manager.get_by_natural_key(username)
17 17
             if user.check_password(password):
18 18
                 return user
19 19
         except UserModel.DoesNotExist:
@@ -64,7 +64,7 @@ def has_module_perms(self, user_obj, app_label):
64 64
     def get_user(self, user_id):
65 65
         try:
66 66
             UserModel = get_user_model()
67  
-            return UserModel.objects.get(pk=user_id)
  67
+            return UserModel._default_manager.get(pk=user_id)
68 68
         except UserModel.DoesNotExist:
69 69
             return None
70 70
 
4  django/contrib/auth/forms.py
@@ -89,7 +89,7 @@ def clean_username(self):
89 89
         # but it sets a nicer error message than the ORM. See #13147.
90 90
         username = self.cleaned_data["username"]
91 91
         try:
92  
-            User.objects.get(username=username)
  92
+            User._default_manager.get(username=username)
93 93
         except User.DoesNotExist:
94 94
             return username
95 95
         raise forms.ValidationError(self.error_messages['duplicate_username'])
@@ -217,7 +217,7 @@ def clean_email(self):
217 217
         """
218 218
         UserModel = get_user_model()
219 219
         email = self.cleaned_data["email"]
220  
-        self.users_cache = UserModel.objects.filter(email__iexact=email)
  220
+        self.users_cache = UserModel._default_manager.filter(email__iexact=email)
221 221
         if not len(self.users_cache):
222 222
             raise forms.ValidationError(self.error_messages['unknown'])
223 223
         if not any(user.is_active for user in self.users_cache):
4  django/contrib/auth/handlers/modwsgi.py
@@ -18,7 +18,7 @@ def check_password(environ, username, password):
18 18
 
19 19
     try:
20 20
         try:
21  
-            user = UserModel.objects.get_by_natural_key(username)
  21
+            user = UserModel._default_manager.get_by_natural_key(username)
22 22
         except UserModel.DoesNotExist:
23 23
             return None
24 24
         if not user.is_active:
@@ -37,7 +37,7 @@ def groups_for_user(environ, username):
37 37
 
38 38
     try:
39 39
         try:
40  
-            user = UserModel.objects.get_by_natural_key(username)
  40
+            user = UserModel._default_manager.get_by_natural_key(username)
41 41
         except UserModel.DoesNotExist:
42 42
             return []
43 43
         try:
2  django/contrib/auth/management/__init__.py
@@ -174,7 +174,7 @@ def get_default_username(check_db=True):
174 174
     # Don't return the default username if it is already taken.
175 175
     if check_db and default_username:
176 176
         try:
177  
-            auth_app.User.objects.get(username=default_username)
  177
+            auth_app.User._default_manager.get(username=default_username)
178 178
         except auth_app.User.DoesNotExist:
179 179
             pass
180 180
         else:
2  django/contrib/auth/management/commands/changepassword.py
@@ -33,7 +33,7 @@ def handle(self, *args, **options):
33 33
         UserModel = get_user_model()
34 34
 
35 35
         try:
36  
-            u = UserModel.objects.using(options.get('database')).get(**{
  36
+            u = UserModel._default_manager.using(options.get('database')).get(**{
37 37
                     UserModel.USERNAME_FIELD: username
38 38
                 })
39 39
         except UserModel.DoesNotExist:
4  django/contrib/auth/management/commands/createsuperuser.py
@@ -95,7 +95,7 @@ def handle(self, *args, **options):
95 95
                         username = None
96 96
                         continue
97 97
                     try:
98  
-                        self.UserModel.objects.db_manager(database).get_by_natural_key(username)
  98
+                        self.UserModel._default_manager.db_manager(database).get_by_natural_key(username)
99 99
                     except self.UserModel.DoesNotExist:
100 100
                         pass
101 101
                     else:
@@ -134,6 +134,6 @@ def handle(self, *args, **options):
134 134
 
135 135
         user_data[self.UserModel.USERNAME_FIELD] = username
136 136
         user_data['password'] = password
137  
-        self.UserModel.objects.db_manager(database).create_superuser(**user_data)
  137
+        self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
138 138
         if verbosity >= 1:
139 139
             self.stdout.write("Superuser created successfully.")
22  django/contrib/auth/tests/auth_backends.py
@@ -34,7 +34,7 @@ def tearDown(self):
34 34
         ContentType.objects.clear_cache()
35 35
 
36 36
     def test_has_perm(self):
37  
-        user = self.UserModel.objects.get(pk=self.user.pk)
  37
+        user = self.UserModel._default_manager.get(pk=self.user.pk)
38 38
         self.assertEqual(user.has_perm('auth.test'), False)
39 39
         user.is_staff = True
40 40
         user.save()
@@ -53,14 +53,14 @@ def test_has_perm(self):
53 53
         self.assertEqual(user.has_perm('auth.test'), False)
54 54
 
55 55
     def test_custom_perms(self):
56  
-        user = self.UserModel.objects.get(pk=self.user.pk)
  56
+        user = self.UserModel._default_manager.get(pk=self.user.pk)
57 57
         content_type = ContentType.objects.get_for_model(Group)
58 58
         perm = Permission.objects.create(name='test', content_type=content_type, codename='test')
59 59
         user.user_permissions.add(perm)
60 60
         user.save()
61 61
 
62 62
         # reloading user to purge the _perm_cache
63  
-        user = self.UserModel.objects.get(pk=self.user.pk)
  63
+        user = self.UserModel._default_manager.get(pk=self.user.pk)
64 64
         self.assertEqual(user.get_all_permissions() == set(['auth.test']), True)
65 65
         self.assertEqual(user.get_group_permissions(), set([]))
66 66
         self.assertEqual(user.has_module_perms('Group'), False)
@@ -71,7 +71,7 @@ def test_custom_perms(self):
71 71
         perm = Permission.objects.create(name='test3', content_type=content_type, codename='test3')
72 72
         user.user_permissions.add(perm)
73 73
         user.save()
74  
-        user = self.UserModel.objects.get(pk=self.user.pk)
  74
+        user = self.UserModel._default_manager.get(pk=self.user.pk)
75 75
         self.assertEqual(user.get_all_permissions(), set(['auth.test2', 'auth.test', 'auth.test3']))
76 76
         self.assertEqual(user.has_perm('test'), False)
77 77
         self.assertEqual(user.has_perm('auth.test'), True)
@@ -81,7 +81,7 @@ def test_custom_perms(self):
81 81
         group.permissions.add(perm)
82 82
         group.save()
83 83
         user.groups.add(group)
84  
-        user = self.UserModel.objects.get(pk=self.user.pk)
  84
+        user = self.UserModel._default_manager.get(pk=self.user.pk)
85 85
         exp = set(['auth.test2', 'auth.test', 'auth.test3', 'auth.test_group'])
86 86
         self.assertEqual(user.get_all_permissions(), exp)
87 87
         self.assertEqual(user.get_group_permissions(), set(['auth.test_group']))
@@ -93,7 +93,7 @@ def test_custom_perms(self):
93 93
 
94 94
     def test_has_no_object_perm(self):
95 95
         """Regressiontest for #12462"""
96  
-        user = self.UserModel.objects.get(pk=self.user.pk)
  96
+        user = self.UserModel._default_manager.get(pk=self.user.pk)
97 97
         content_type = ContentType.objects.get_for_model(Group)
98 98
         perm = Permission.objects.create(name='test', content_type=content_type, codename='test')
99 99
         user.user_permissions.add(perm)
@@ -106,7 +106,7 @@ def test_has_no_object_perm(self):
106 106
 
107 107
     def test_get_all_superuser_permissions(self):
108 108
         "A superuser has all permissions. Refs #14795"
109  
-        user = self.UserModel.objects.get(pk=self.superuser.pk)
  109
+        user = self.UserModel._default_manager.get(pk=self.superuser.pk)
110 110
         self.assertEqual(len(user.get_all_permissions()), len(Permission.objects.all()))
111 111
 
112 112
 
@@ -151,13 +151,13 @@ class ExtensionUserModelBackendTest(BaseModelBackendTest, TestCase):
151 151
     UserModel = ExtensionUser
152 152
 
153 153
     def create_users(self):
154  
-        self.user = ExtensionUser.objects.create_user(
  154
+        self.user = ExtensionUser._default_manager.create_user(
155 155
             username='test',
156 156
             email='test@example.com',
157 157
             password='test',
158 158
             date_of_birth=date(2006, 4, 25)
159 159
         )
160  
-        self.superuser = ExtensionUser.objects.create_superuser(
  160
+        self.superuser = ExtensionUser._default_manager.create_superuser(
161 161
             username='test2',
162 162
             email='test2@example.com',
163 163
             password='test',
@@ -178,12 +178,12 @@ class CustomPermissionsUserModelBackendTest(BaseModelBackendTest, TestCase):
178 178
     UserModel = CustomPermissionsUser
179 179
 
180 180
     def create_users(self):
181  
-        self.user = CustomPermissionsUser.objects.create_user(
  181
+        self.user = CustomPermissionsUser._default_manager.create_user(
182 182
             email='test@example.com',
183 183
             password='test',
184 184
             date_of_birth=date(2006, 4, 25)
185 185
         )
186  
-        self.superuser = CustomPermissionsUser.objects.create_superuser(
  186
+        self.superuser = CustomPermissionsUser._default_manager.create_superuser(
187 187
             email='test2@example.com',
188 188
             password='test',
189 189
             date_of_birth=date(1976, 11, 8)
8  django/contrib/auth/tests/custom_user.py
@@ -42,7 +42,7 @@ class CustomUser(AbstractBaseUser):
42 42
     is_admin = models.BooleanField(default=False)
43 43
     date_of_birth = models.DateField()
44 44
 
45  
-    objects = CustomUserManager()
  45
+    custom_objects = CustomUserManager()
46 46
 
47 47
     USERNAME_FIELD = 'email'
48 48
     REQUIRED_FIELDS = ['date_of_birth']
@@ -88,7 +88,7 @@ def is_staff(self):
88 88
 class ExtensionUser(AbstractUser):
89 89
     date_of_birth = models.DateField()
90 90
 
91  
-    objects = UserManager()
  91
+    custom_objects = UserManager()
92 92
 
93 93
     REQUIRED_FIELDS = AbstractUser.REQUIRED_FIELDS + ['date_of_birth']
94 94
 
@@ -112,7 +112,7 @@ class CustomPermissionsUser(AbstractBaseUser, PermissionsMixin):
112 112
     email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
113 113
     date_of_birth = models.DateField()
114 114
 
115  
-    objects = CustomPermissionsUserManager()
  115
+    custom_objects = CustomPermissionsUserManager()
116 116
 
117 117
     USERNAME_FIELD = 'email'
118 118
     REQUIRED_FIELDS = ['date_of_birth']
@@ -136,7 +136,7 @@ class IsActiveTestUser1(AbstractBaseUser):
136 136
     """
137 137
     username = models.CharField(max_length=30, unique=True)
138 138
 
139  
-    objects = BaseUserManager()
  139
+    custom_objects = BaseUserManager()
140 140
 
141 141
     USERNAME_FIELD = 'username'
142 142
 
2  django/contrib/auth/tests/handlers.py
@@ -42,7 +42,7 @@ def test_check_password_custom_user(self):
42 42
         with custom user installed
43 43
         """
44 44
 
45  
-        CustomUser.objects.create_user('test@example.com', '1990-01-01', 'test')
  45
+        CustomUser._default_manager.create_user('test@example.com', '1990-01-01', 'test')
46 46
 
47 47
         # User not in database
48 48
         self.assertTrue(check_password({}, 'unknown', '') is None)
6  django/contrib/auth/tests/management.py
@@ -125,7 +125,7 @@ def test_email_in_username(self):
125 125
             email="joe@somewhere.org",
126 126
             stdout=new_io
127 127
         )
128  
-        u = User.objects.get(username="joe+admin@somewhere.org")
  128
+        u = User._default_manager.get(username="joe+admin@somewhere.org")
129 129
         self.assertEqual(u.email, 'joe@somewhere.org')
130 130
         self.assertFalse(u.has_usable_password())
131 131
 
@@ -145,7 +145,7 @@ def test_swappable_user(self):
145 145
         )
146 146
         command_output = new_io.getvalue().strip()
147 147
         self.assertEqual(command_output, 'Superuser created successfully.')
148  
-        u = CustomUser.objects.get(email="joe@somewhere.org")
  148
+        u = CustomUser._default_manager.get(email="joe@somewhere.org")
149 149
         self.assertEqual(u.date_of_birth, date(1976, 4, 1))
150 150
 
151 151
         # created password should be unusable
@@ -167,7 +167,7 @@ def test_swappable_user_missing_required_field(self):
167 167
                 skip_validation=True
168 168
             )
169 169
 
170  
-        self.assertEqual(CustomUser.objects.count(), 0)
  170
+        self.assertEqual(CustomUser._default_manager.count(), 0)
171 171
 
172 172
 
173 173
 class PermissionDuplicationTestCase(TestCase):
2  django/contrib/auth/tests/models.py
@@ -137,6 +137,6 @@ def test_is_active_field_default(self):
137 137
         user.is_active = False
138 138
         # there should be no problem saving - but the attribute is not saved
139 139
         user.save()
140  
-        user_fetched = UserModel.objects.get(pk=user.pk)
  140
+        user_fetched = UserModel._default_manager.get(pk=user.pk)
141 141
         # the attribute is always true for newly retrieved instance
142 142
         self.assertEqual(user_fetched.is_active, True)
2  django/contrib/auth/views.py
@@ -200,7 +200,7 @@ def password_reset_confirm(request, uidb36=None, token=None,
200 200
         post_reset_redirect = reverse('django.contrib.auth.views.password_reset_complete')
201 201
     try:
202 202
         uid_int = base36_to_int(uidb36)
203  
-        user = UserModel.objects.get(pk=uid_int)
  203
+        user = UserModel._default_manager.get(pk=uid_int)
204 204
     except (ValueError, OverflowError, UserModel.DoesNotExist):
205 205
         user = None
206 206
 

0 notes on commit cc4de61

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