--- a/web/lib/django/contrib/admin/__init__.py Wed May 19 17:43:59 2010 +0200
+++ b/web/lib/django/contrib/admin/__init__.py Tue May 25 02:43:45 2010 +0200
@@ -2,11 +2,7 @@
from django.contrib.admin.options import ModelAdmin, HORIZONTAL, VERTICAL
from django.contrib.admin.options import StackedInline, TabularInline
from django.contrib.admin.sites import AdminSite, site
-from django.utils.importlib import import_module
-# 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():
"""
@@ -14,45 +10,27 @@
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
+ import copy
from django.conf import settings
+ from django.utils.importlib import import_module
+ from django.utils.module_loading import module_has_submodule
for app in settings.INSTALLED_APPS:
- # For each app, we need to look for an admin.py inside that app's
- # package. We can't use os.path here -- recall that modules may be
- # imported different ways (think zip files) -- so we need to get
- # the app's __path__ and look for admin.py on that path.
-
- # Step 1: find out the app's __path__ Import errors here will (and
- # should) bubble up, but a missing __path__ (which is legal, but weird)
- # fails silently -- apps that do weird things with __path__ might
- # need to roll their own admin registration.
+ mod = import_module(app)
+ # Attempt to import the app's admin module.
try:
- app_path = import_module(app).__path__
- except AttributeError:
- continue
+ before_import_registry = copy.copy(site._registry)
+ import_module('%s.admin' % app)
+ except:
+ # Reset the model registry to the state before the last import as
+ # this import will have to reoccur on the next request and this
+ # could raise NotRegistered and AlreadyRegistered exceptions
+ # (see #8245).
+ site._registry = before_import_registry
- # Step 2: use imp.find_module to find the app's admin.py. For some
- # reason imp.find_module raises ImportError if the app can't be found
- # but doesn't actually try to import the module. So skip this app if
- # its admin.py doesn't exist
- try:
- imp.find_module('admin', app_path)
- except ImportError:
- continue
-
- # Step 3: import the app's admin file. If this has errors we want them
- # to bubble up.
- import_module("%s.admin" % app)
- # autodiscover was successful, reset loading flag.
- LOADING = False
+ # Decide whether to bubble up this error. If the app just
+ # doesn't have an admin module, we can ignore the error
+ # attempting to import it, otherwise we want it to bubble up.
+ if module_has_submodule(mod, 'admin'):
+ raise