Author: gwilson
Date: 2008-12-23 12:25:24 -0600 (Tue, 23 Dec 2008)
New Revision: 9680
Added:
django/trunk/tests/regressiontests/bug8245/
django/trunk/tests/regressiontests/bug8245/__init__.py
django/trunk/tests/regressiontests/bug8245/admin.py
django/trunk/tests/regressiontests/bug8245/models.py
django/trunk/tests/regressiontests/bug8245/tests.py
Modified:
django/trunk/django/contrib/admin/__init__.py
Log:
Fixed #8245 -- Added a LOADING flag to autodiscover to prevent an admin.py
module with errors from raising a spurious AlreadyRegistered exception in a
subsequent call to autodiscover.
Modified: django/trunk/django/contrib/admin/__init__.py
===================================================================
--- django/trunk/django/contrib/admin/__init__.py 2008-12-23 05:52:57 UTC
(rev 9679)
+++ django/trunk/django/contrib/admin/__init__.py 2008-12-23 18:25:24 UTC
(rev 9680)
@@ -2,12 +2,26 @@
from django.contrib.admin.options import StackedInline, TabularInline
from django.contrib.admin.sites import AdminSite, site
+# A flag to tell us if autodiscover is running. autodiscover will set this to
+# True while running, and False when it finishes.
+LOADING = False
+
def autodiscover():
"""
- Auto-discover INSTALLED_APPS admin.py modules and fail silently when
+ Auto-discover INSTALLED_APPS admin.py modules and fail silently when
not present. This forces an import on them to register any admin bits they
may want.
"""
+ # Bail out if autodiscover didn't finish loading from a previous call so
+ # that we avoid running autodiscover again when the URLConf is loaded by
+ # the exception handler to resolve the handler500 view. This prevents an
+ # admin.py module with errors from re-registering models and raising a
+ # spurious AlreadyRegistered exception (see #8245).
+ global LOADING
+ if LOADING:
+ return
+ LOADING = True
+
import imp
from django.conf import settings
@@ -38,3 +52,5 @@
# Step 3: import the app's admin file. If this has errors we want them
# to bubble up.
__import__("%s.admin" % app)
+ # autodiscover was successful, reset loading flag.
+ LOADING = False
Added: django/trunk/tests/regressiontests/bug8245/__init__.py
===================================================================
Added: django/trunk/tests/regressiontests/bug8245/admin.py
===================================================================
--- django/trunk/tests/regressiontests/bug8245/admin.py
(rev 0)
+++ django/trunk/tests/regressiontests/bug8245/admin.py 2008-12-23 18:25:24 UTC
(rev 9680)
@@ -0,0 +1,7 @@
+from django.contrib import admin
+
+from models import Story
+
+
+admin.site.register(Story)
+raise Exception("Bad admin module")
Added: django/trunk/tests/regressiontests/bug8245/models.py
===================================================================
--- django/trunk/tests/regressiontests/bug8245/models.py
(rev 0)
+++ django/trunk/tests/regressiontests/bug8245/models.py 2008-12-23
18:25:24 UTC (rev 9680)
@@ -0,0 +1,4 @@
+from django.db import models
+
+class Story(models.Model):
+ title = models.CharField(max_length=10)
Added: django/trunk/tests/regressiontests/bug8245/tests.py
===================================================================
--- django/trunk/tests/regressiontests/bug8245/tests.py
(rev 0)
+++ django/trunk/tests/regressiontests/bug8245/tests.py 2008-12-23 18:25:24 UTC
(rev 9680)
@@ -0,0 +1,23 @@
+from unittest import TestCase
+
+from django.contrib import admin
+
+
+class Bug8245Test(TestCase):
+ """
+ Test for bug #8245 - don't raise an AlreadyRegistered exception when using
+ autodiscover() and an admin.py module contains an error.
+ """
+
+ def test_bug_8245(self):
+ # The first time autodiscover is called, we should get our real error.
+ try:
+ admin.autodiscover()
+ except Exception, e:
+ self.failUnlessEqual(str(e), "Bad admin module")
+ else:
+ self.fail(
+ 'autodiscover should have raised a "Bad admin module" error.')
+ # Calling autodiscover again should bail out early and not raise an
+ # AlreadyRegistered error.
+ admin.autodiscover()
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---