Ticket #11957: 11957-r12936.diff

File 11957-r12936.diff, 2.7 KB (added by Ramiro Morales, 15 years ago)

carljm patch updated to r12936

  • django/contrib/admin/__init__.py

    diff --git a/django/contrib/admin/__init__.py b/django/contrib/admin/__init__.py
    a b  
    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    """
    139    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:
     
    5441
    5542        # Step 3: import the app's admin file. If this has errors we want them
    5643        # to bubble up.
    57         import_module("%s.admin" % app)
    58     # autodiscover was successful, reset loading flag.
    59     LOADING = False
     44        try:
     45            model_registry_before_import = copy.copy(site._registry)
     46            import_module("%s.admin" % app)
     47        except:
     48            # Reset the model registry to the state before the last import as this import will have to reoccur
     49            # on the next request and this could raise NotRegistered and AlreadyRegistered exceptions (see #8245).
     50            site._registry = model_registry_before_import
     51            raise
  • tests/regressiontests/bug8245/tests.py

    diff --git a/tests/regressiontests/bug8245/tests.py b/tests/regressiontests/bug8245/tests.py
    a b  
    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