Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #8245 -- Added a LOADING flag to autodiscover to prevent an adm…

…in.py module with errors from raising a spurious AlreadyRegistered exception in a subsequent call to autodiscover.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9680 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 9af56803f53a4e41bb826d4e293ef8650ad9f13a 1 parent f0d44e4
Gary Wilson Jr. authored December 23, 2008
18  django/contrib/admin/__init__.py
@@ -2,12 +2,26 @@
2 2
 from django.contrib.admin.options import StackedInline, TabularInline
3 3
 from django.contrib.admin.sites import AdminSite, site
4 4
 
  5
+# A flag to tell us if autodiscover is running.  autodiscover will set this to
  6
+# True while running, and False when it finishes.
  7
+LOADING = False
  8
+
5 9
 def autodiscover():
6 10
     """
7  
-    Auto-discover INSTALLED_APPS admin.py modules and fail silently when 
  11
+    Auto-discover INSTALLED_APPS admin.py modules and fail silently when
8 12
     not present. This forces an import on them to register any admin bits they
9 13
     may want.
10 14
     """
  15
+    # Bail out if autodiscover didn't finish loading from a previous call so
  16
+    # that we avoid running autodiscover again when the URLConf is loaded by
  17
+    # the exception handler to resolve the handler500 view.  This prevents an
  18
+    # admin.py module with errors from re-registering models and raising a
  19
+    # spurious AlreadyRegistered exception (see #8245).
  20
+    global LOADING
  21
+    if LOADING:
  22
+        return
  23
+    LOADING = True
  24
+
11 25
     import imp
12 26
     from django.conf import settings
13 27
 
@@ -38,3 +52,5 @@ def autodiscover():
38 52
         # Step 3: import the app's admin file. If this has errors we want them
39 53
         # to bubble up.
40 54
         __import__("%s.admin" % app)
  55
+    # autodiscover was successful, reset loading flag.
  56
+    LOADING = False
0  tests/regressiontests/bug8245/__init__.py
No changes.
7  tests/regressiontests/bug8245/admin.py
... ...
@@ -0,0 +1,7 @@
  1
+from django.contrib import admin
  2
+
  3
+from models import Story
  4
+
  5
+
  6
+admin.site.register(Story)
  7
+raise Exception("Bad admin module")
4  tests/regressiontests/bug8245/models.py
... ...
@@ -0,0 +1,4 @@
  1
+from django.db import models
  2
+
  3
+class Story(models.Model):
  4
+    title = models.CharField(max_length=10)
23  tests/regressiontests/bug8245/tests.py
... ...
@@ -0,0 +1,23 @@
  1
+from unittest import TestCase
  2
+
  3
+from django.contrib import admin
  4
+
  5
+
  6
+class Bug8245Test(TestCase):
  7
+    """
  8
+    Test for bug #8245 - don't raise an AlreadyRegistered exception when using
  9
+    autodiscover() and an admin.py module contains an error.
  10
+    """
  11
+
  12
+    def test_bug_8245(self):
  13
+        # The first time autodiscover is called, we should get our real error.
  14
+        try:
  15
+            admin.autodiscover()
  16
+        except Exception, e:
  17
+            self.failUnlessEqual(str(e), "Bad admin module")
  18
+        else:
  19
+            self.fail(
  20
+                'autodiscover should have raised a "Bad admin module" error.')
  21
+        # Calling autodiscover again should bail out early and not raise an
  22
+        # AlreadyRegistered error.
  23
+        admin.autodiscover()

0 notes on commit 9af5680

Please sign in to comment.
Something went wrong with that request. Please try again.