diff options
| author | Justin Bronn <jbronn@gmail.com> | 2010-02-23 05:52:37 +0000 |
|---|---|---|
| committer | Justin Bronn <jbronn@gmail.com> | 2010-02-23 05:52:37 +0000 |
| commit | 1d5165e3be03ffb2b886b34cf0c1377ce958d12d (patch) | |
| tree | 6342b2924993e863cfacc70cfa093576afd23d73 | |
| parent | 5546430304f1310e76b217bb3b6e3597d9b87359 (diff) | |
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
| -rw-r--r-- | AUTHORS | 2 | ||||
| -rw-r--r-- | django/contrib/auth/models.py | 13 | ||||
| -rw-r--r-- | django/contrib/auth/tests/__init__.py | 1 | ||||
| -rw-r--r-- | django/contrib/auth/tests/models.py | 35 |
4 files changed, 49 insertions, 2 deletions
@@ -385,6 +385,7 @@ answer newbie questions, and generally made Django that much better: Brian Ray <http://brianray.chipy.org/> remco@diji.biz Marc Remolt <m.remolt@webmasters.de> + Bruno Renié <buburno@gmail.com> David Reynolds <david@reynoldsfamily.org.uk> rhettg@gmail.com ricardojbarrios@gmail.com @@ -504,7 +505,6 @@ answer newbie questions, and generally made Django that much better: Cheng Zhang Glenn Maynard <glenn@zewt.org> bthomas - Bruno Renié <buburno@gmail.com> 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) |
