|
1 from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME |
|
2 from django.contrib.admin.options import ModelAdmin, HORIZONTAL, VERTICAL |
|
3 from django.contrib.admin.options import StackedInline, TabularInline |
|
4 from django.contrib.admin.sites import AdminSite, site |
|
5 from django.utils.importlib import import_module |
|
6 |
|
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 |
|
11 def autodiscover(): |
|
12 """ |
|
13 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 |
|
15 may want. |
|
16 """ |
|
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 |
|
27 import imp |
|
28 from django.conf import settings |
|
29 |
|
30 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 |