~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to django/contrib/admin/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
from django.contrib.admin.options import ModelAdmin, HORIZONTAL, VERTICAL
3
3
from django.contrib.admin.options import StackedInline, TabularInline
4
4
from django.contrib.admin.sites import AdminSite, site
5
 
from django.utils.importlib import import_module
6
5
 
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
6
 
11
7
def autodiscover():
12
8
    """
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 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
13
 
27
 
    import imp
 
14
    import copy
28
15
    from django.conf import settings
 
16
    from django.utils.importlib import import_module
 
17
    from django.utils.module_loading import module_has_submodule
29
18
 
30
19
    for app in settings.INSTALLED_APPS:
31
 
        # For each app, we need to look for an admin.py inside that app's
32
 
        # package. We can't use os.path here -- recall that modules may be
33
 
        # imported different ways (think zip files) -- so we need to get
34
 
        # the app's __path__ and look for admin.py on that path.
35
 
 
36
 
        # Step 1: find out the app's __path__ Import errors here will (and
37
 
        # should) bubble up, but a missing __path__ (which is legal, but weird)
38
 
        # fails silently -- apps that do weird things with __path__ might
39
 
        # need to roll their own admin registration.
40
 
        try:
41
 
            app_path = import_module(app).__path__
42
 
        except AttributeError:
43
 
            continue
44
 
 
45
 
        # Step 2: use imp.find_module to find the app's admin.py. For some
46
 
        # reason imp.find_module raises ImportError if the app can't be found
47
 
        # but doesn't actually try to import the module. So skip this app if
48
 
        # its admin.py doesn't exist
49
 
        try:
50
 
            imp.find_module('admin', app_path)
51
 
        except ImportError:
52
 
            continue
53
 
 
54
 
        # Step 3: import the app's admin file. If this has errors we want them
55
 
        # to bubble up.
56
 
        import_module("%s.admin" % app)
57
 
    # autodiscover was successful, reset loading flag.
58
 
    LOADING = False
 
20
        mod = import_module(app)
 
21
        # Attempt to import the app's admin module.
 
22
        try:
 
23
            before_import_registry = copy.copy(site._registry)
 
24
            import_module('%s.admin' % app)
 
25
        except:
 
26
            # Reset the model registry to the state before the last import as
 
27
            # this import will have to reoccur on the next request and this
 
28
            # could raise NotRegistered and AlreadyRegistered exceptions
 
29
            # (see #8245).
 
30
            site._registry = before_import_registry
 
31
 
 
32
            # Decide whether to bubble up this error. If the app just
 
33
            # doesn't have an admin module, we can ignore the error
 
34
            # attempting to import it, otherwise we want it to bubble up.
 
35
            if module_has_submodule(mod, 'admin'):
 
36
                raise