Skip to content

Commit 1d5165e

Browse files
committed
Fixed #12776 -- User.get_profile now raises SiteProfileNotAvailable instead of AttributeError in certain circumstances. Thanks, Bruno Renié.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12506 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 5546430 commit 1d5165e

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

AUTHORS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ answer newbie questions, and generally made Django that much better:
385385
Brian Ray <http://brianray.chipy.org/>
386386
387387
Marc Remolt <[email protected]>
388+
Bruno Renié <[email protected]>
388389
David Reynolds <[email protected]>
389390
390391
@@ -504,7 +505,6 @@ answer newbie questions, and generally made Django that much better:
504505
Cheng Zhang
505506
Glenn Maynard <[email protected]>
506507
bthomas
507-
Bruno Renié <[email protected]>
508508

509509
A big THANK YOU goes to:
510510

django/contrib/auth/models.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,21 @@ def get_profile(self):
336336
if not hasattr(self, '_profile_cache'):
337337
from django.conf import settings
338338
if not getattr(settings, 'AUTH_PROFILE_MODULE', False):
339-
raise SiteProfileNotAvailable
339+
raise SiteProfileNotAvailable('You need to set AUTH_PROFILE_MO'
340+
'DULE in your project settings')
340341
try:
341342
app_label, model_name = settings.AUTH_PROFILE_MODULE.split('.')
343+
except ValueError:
344+
raise SiteProfileNotAvailable('app_label and model_name should'
345+
' be separated by a dot in the AUTH_PROFILE_MODULE set'
346+
'ting')
347+
348+
try:
342349
model = models.get_model(app_label, model_name)
350+
if model is None:
351+
raise SiteProfileNotAvailable('Unable to load the profile '
352+
'model, check AUTH_PROFILE_MODULE in your project sett'
353+
'ings')
343354
self._profile_cache = model._default_manager.using(self._state.db).get(user__id__exact=self.id)
344355
self._profile_cache.user = self
345356
except (ImportError, ImproperlyConfigured):

django/contrib/auth/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import RemoteUserTest, RemoteUserNoCreateTest, RemoteUserCustomTest
77
from django.contrib.auth.tests.auth_backends import BackendTest, RowlevelBackendTest, AnonymousUserBackendTest, NoAnonymousUserBackendTest
88
from django.contrib.auth.tests.tokens import TOKEN_GENERATOR_TESTS
9+
from django.contrib.auth.tests.models import ProfileTestCase
910

1011
# The password for the fixture data users is 'password'
1112

django/contrib/auth/tests/models.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from django.conf import settings
2+
from django.test import TestCase
3+
from django.contrib.auth.models import User, SiteProfileNotAvailable
4+
5+
class ProfileTestCase(TestCase):
6+
fixtures = ['authtestdata.json']
7+
def setUp(self):
8+
"""Backs up the AUTH_PROFILE_MODULE"""
9+
self.old_AUTH_PROFILE_MODULE = getattr(settings,
10+
'AUTH_PROFILE_MODULE', None)
11+
12+
def tearDown(self):
13+
"""Restores the AUTH_PROFILE_MODULE -- if it was not set it is deleted,
14+
otherwise the old value is restored"""
15+
if self.old_AUTH_PROFILE_MODULE is None and \
16+
hasattr(settings, 'AUTH_PROFILE_MODULE'):
17+
del settings.AUTH_PROFILE_MODULE
18+
19+
if self.old_AUTH_PROFILE_MODULE is not None:
20+
settings.AUTH_PROFILE_MODULE = self.old_AUTH_PROFILE_MODULE
21+
22+
def test_site_profile_not_available(self):
23+
# calling get_profile without AUTH_PROFILE_MODULE set
24+
if hasattr(settings, 'AUTH_PROFILE_MODULE'):
25+
del settings.AUTH_PROFILE_MODULE
26+
user = User.objects.get(username='testclient')
27+
self.assertRaises(SiteProfileNotAvailable, user.get_profile)
28+
29+
# Bad syntax in AUTH_PROFILE_MODULE:
30+
settings.AUTH_PROFILE_MODULE = 'foobar'
31+
self.assertRaises(SiteProfileNotAvailable, user.get_profile)
32+
33+
# module that doesn't exist
34+
settings.AUTH_PROFILE_MODULE = 'foo.bar'
35+
self.assertRaises(SiteProfileNotAvailable, user.get_profile)

0 commit comments

Comments
 (0)