Ticket #11957: 11957_r11594.diff
File 11957_r11594.diff, 2.9 KB (added by , 15 years ago) |
---|
-
django/contrib/admin/__init__.py
4 4 from django.contrib.admin.sites import AdminSite, site 5 5 from django.utils.importlib import import_module 6 6 7 # A flag to tell us if autodiscover is running. autodiscover will set this to8 # True while running, and False when it finishes.9 LOADING = False10 11 7 def autodiscover(): 12 8 """ 13 Auto-discover INSTALLED_APPS admin.py modules and fail silently when 9 Auto-discover INSTALLED_APPS admin.py modules and fail silently when 14 10 not present. This forces an import on them to register any admin bits they 15 11 may want. 16 12 """ 17 # Bail out if autodiscover didn't finish loading from a previous call so18 # that we avoid running autodiscover again when the URLconf is loaded by19 # the exception handler to resolve the handler500 view. This prevents an20 # admin.py module with errors from re-registering models and raising a21 # spurious AlreadyRegistered exception (see #8245).22 global LOADING23 if LOADING:24 return25 LOADING = True26 27 13 import imp 14 import copy 28 15 from django.conf import settings 29 16 30 17 for app in settings.INSTALLED_APPS: … … 53 40 54 41 # Step 3: import the app's admin file. If this has errors we want them 55 42 # to bubble up. 56 import_module("%s.admin" % app) 57 # autodiscover was successful, reset loading flag. 58 LOADING = False 43 try: 44 model_registry_before_import = copy.copy(site._registry) 45 __import__("%s.admin" % app) 46 except: 47 # Reset the model registry to the state before the last import as this import will have to reoccur 48 # on the next request and this could raise NotRegistered and AlreadyRegistered exceptions (see #8245). 49 site._registry = model_registry_before_import 50 raise -
tests/regressiontests/bug8245/tests.py
18 18 else: 19 19 self.fail( 20 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() 21 # Calling autodiscover again should raise the very same error it did the # first time, not an AlreadyRegistered error. 22 try: 23 admin.autodiscover() 24 except Exception, e: 25 self.failUnlessEqual(str(e), "Bad admin module") 26 else: 27 self.fail( 28 'autodiscover should have raised a "Bad admin module" error.')