From 1d5165e3be03ffb2b886b34cf0c1377ce958d12d Mon Sep 17 00:00:00 2001 From: Justin Bronn Date: Tue, 23 Feb 2010 05:52:37 +0000 Subject: [PATCH] =?UTF-8?q?Fixed=20#12776=20--=20`User.get=5Fprofile`=20no?= =?UTF-8?q?w=20raises=20`SiteProfileNotAvailable`=20instead=20of=20`Attrib?= =?UTF-8?q?uteError`=20in=20certain=20circumstances.=20=20Thanks,=20Bruno?= =?UTF-8?q?=20Reni=C3=A9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.djangoproject.com/svn/django/trunk@12506 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 2 +- django/contrib/auth/models.py | 13 +++++++++- django/contrib/auth/tests/__init__.py | 1 + django/contrib/auth/tests/models.py | 35 +++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 django/contrib/auth/tests/models.py diff --git a/AUTHORS b/AUTHORS index ed27c1ede6..1a659250d2 100644 --- a/AUTHORS +++ b/AUTHORS @@ -385,6 +385,7 @@ answer newbie questions, and generally made Django that much better: Brian Ray remco@diji.biz Marc Remolt + Bruno ReniƩ David Reynolds rhettg@gmail.com ricardojbarrios@gmail.com @@ -504,7 +505,6 @@ answer newbie questions, and generally made Django that much better: Cheng Zhang Glenn Maynard bthomas - Bruno ReniƩ A big THANK YOU goes to: diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py index da8b8a6b25..3bfd98270d 100644 --- a/django/contrib/auth/models.py +++ b/django/contrib/auth/models.py @@ -336,10 +336,21 @@ class User(models.Model): if not hasattr(self, '_profile_cache'): from django.conf import settings if not getattr(settings, 'AUTH_PROFILE_MODULE', False): - raise SiteProfileNotAvailable + raise SiteProfileNotAvailable('You need to set AUTH_PROFILE_MO' + 'DULE in your project settings') try: app_label, model_name = settings.AUTH_PROFILE_MODULE.split('.') + except ValueError: + raise SiteProfileNotAvailable('app_label and model_name should' + ' be separated by a dot in the AUTH_PROFILE_MODULE set' + 'ting') + + try: model = models.get_model(app_label, model_name) + if model is None: + raise SiteProfileNotAvailable('Unable to load the profile ' + 'model, check AUTH_PROFILE_MODULE in your project sett' + 'ings') self._profile_cache = model._default_manager.using(self._state.db).get(user__id__exact=self.id) self._profile_cache.user = self except (ImportError, ImproperlyConfigured): diff --git a/django/contrib/auth/tests/__init__.py b/django/contrib/auth/tests/__init__.py index 3d7c562c00..5b1f5a0de3 100644 --- a/django/contrib/auth/tests/__init__.py +++ b/django/contrib/auth/tests/__init__.py @@ -6,6 +6,7 @@ from django.contrib.auth.tests.remote_user \ import RemoteUserTest, RemoteUserNoCreateTest, RemoteUserCustomTest from django.contrib.auth.tests.auth_backends import BackendTest, RowlevelBackendTest, AnonymousUserBackendTest, NoAnonymousUserBackendTest from django.contrib.auth.tests.tokens import TOKEN_GENERATOR_TESTS +from django.contrib.auth.tests.models import ProfileTestCase # The password for the fixture data users is 'password' diff --git a/django/contrib/auth/tests/models.py b/django/contrib/auth/tests/models.py new file mode 100644 index 0000000000..754c6db7b4 --- /dev/null +++ b/django/contrib/auth/tests/models.py @@ -0,0 +1,35 @@ +from django.conf import settings +from django.test import TestCase +from django.contrib.auth.models import User, SiteProfileNotAvailable + +class ProfileTestCase(TestCase): + fixtures = ['authtestdata.json'] + def setUp(self): + """Backs up the AUTH_PROFILE_MODULE""" + self.old_AUTH_PROFILE_MODULE = getattr(settings, + 'AUTH_PROFILE_MODULE', None) + + def tearDown(self): + """Restores the AUTH_PROFILE_MODULE -- if it was not set it is deleted, + otherwise the old value is restored""" + if self.old_AUTH_PROFILE_MODULE is None and \ + hasattr(settings, 'AUTH_PROFILE_MODULE'): + del settings.AUTH_PROFILE_MODULE + + if self.old_AUTH_PROFILE_MODULE is not None: + settings.AUTH_PROFILE_MODULE = self.old_AUTH_PROFILE_MODULE + + def test_site_profile_not_available(self): + # calling get_profile without AUTH_PROFILE_MODULE set + if hasattr(settings, 'AUTH_PROFILE_MODULE'): + del settings.AUTH_PROFILE_MODULE + user = User.objects.get(username='testclient') + self.assertRaises(SiteProfileNotAvailable, user.get_profile) + + # Bad syntax in AUTH_PROFILE_MODULE: + settings.AUTH_PROFILE_MODULE = 'foobar' + self.assertRaises(SiteProfileNotAvailable, user.get_profile) + + # module that doesn't exist + settings.AUTH_PROFILE_MODULE = 'foo.bar' + self.assertRaises(SiteProfileNotAvailable, user.get_profile)