Ticket #11957: 11957_r11594.diff

File 11957_r11594.diff, 2.9 KB (added by Carl Meyer, 15 years ago)

correct the fix for 8254 to avoid mysteriously-disappearing admin.py

  • django/contrib/admin/__init__.py

     
    44from django.contrib.admin.sites import AdminSite, site
    55from django.utils.importlib import import_module
    66
    7 # A flag to tell us if autodiscover is running.  autodiscover will set this to
    8 # True while running, and False when it finishes.
    9 LOADING = False
    10 
    117def autodiscover():
    128    """
    13     Auto-discover INSTALLED_APPS admin.py modules and fail silently when
     9    Auto-discover INSTALLED_APPS admin.py modules and fail silently when 
    1410    not present. This forces an import on them to register any admin bits they
    1511    may want.
    1612    """
    17     # Bail out if autodiscover didn't finish loading from a previous call so
    18     # that we avoid running autodiscover again when the URLconf is loaded by
    19     # the exception handler to resolve the handler500 view.  This prevents an
    20     # admin.py module with errors from re-registering models and raising a
    21     # spurious AlreadyRegistered exception (see #8245).
    22     global LOADING
    23     if LOADING:
    24         return
    25     LOADING = True
    26 
    2713    import imp
     14    import copy
    2815    from django.conf import settings
    2916
    3017    for app in settings.INSTALLED_APPS:
     
    5340
    5441        # Step 3: import the app's admin file. If this has errors we want them
    5542        # 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

     
    1818        else:
    1919            self.fail(
    2020                '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.')
Back to Top