1 from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME |
1 from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME |
2 from django.contrib.admin.options import ModelAdmin, HORIZONTAL, VERTICAL |
2 from django.contrib.admin.options import ModelAdmin, HORIZONTAL, VERTICAL |
3 from django.contrib.admin.options import StackedInline, TabularInline |
3 from django.contrib.admin.options import StackedInline, TabularInline |
4 from django.contrib.admin.sites import AdminSite, site |
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 def autodiscover(): |
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 not present. This forces an import on them to register any admin bits they |
10 not present. This forces an import on them to register any admin bits they |
15 may want. |
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 from django.conf import settings |
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 for app in settings.INSTALLED_APPS: |
19 for app in settings.INSTALLED_APPS: |
31 # For each app, we need to look for an admin.py inside that app's |
20 mod = import_module(app) |
32 # package. We can't use os.path here -- recall that modules may be |
21 # Attempt to import the app's admin module. |
33 # imported different ways (think zip files) -- so we need to get |
22 try: |
34 # the app's __path__ and look for admin.py on that path. |
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 |
35 |
31 |
36 # Step 1: find out the app's __path__ Import errors here will (and |
32 # Decide whether to bubble up this error. If the app just |
37 # should) bubble up, but a missing __path__ (which is legal, but weird) |
33 # doesn't have an admin module, we can ignore the error |
38 # fails silently -- apps that do weird things with __path__ might |
34 # attempting to import it, otherwise we want it to bubble up. |
39 # need to roll their own admin registration. |
35 if module_has_submodule(mod, 'admin'): |
40 try: |
36 raise |
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 |
|