Pārlūkot izejas kodu

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
Justin Bronn 15 gadi atpakaļ
vecāks
revīzija
1d5165e3be

+ 1 - 1
AUTHORS

@@ -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:
 

+ 12 - 1
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):

+ 1 - 0
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'
 

+ 35 - 0
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)