2009-03-24 04:22:56 +08:00
|
|
|
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
|
2008-07-19 07:54:34 +08:00
|
|
|
from django.contrib.admin.options import ModelAdmin, HORIZONTAL, VERTICAL
|
|
|
|
from django.contrib.admin.options import StackedInline, TabularInline
|
|
|
|
from django.contrib.admin.sites import AdminSite, site
|
2009-03-19 00:55:59 +08:00
|
|
|
from django.utils.importlib import import_module
|
2008-07-19 07:54:34 +08:00
|
|
|
|
2008-12-24 02:25:24 +08:00
|
|
|
# A flag to tell us if autodiscover is running. autodiscover will set this to
|
|
|
|
# True while running, and False when it finishes.
|
|
|
|
LOADING = False
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def autodiscover():
|
|
|
|
"""
|
2008-12-24 02:25:24 +08:00
|
|
|
Auto-discover INSTALLED_APPS admin.py modules and fail silently when
|
2008-07-19 07:54:34 +08:00
|
|
|
not present. This forces an import on them to register any admin bits they
|
|
|
|
may want.
|
|
|
|
"""
|
2008-12-24 02:25:24 +08:00
|
|
|
# Bail out if autodiscover didn't finish loading from a previous call so
|
2009-04-01 00:07:07 +08:00
|
|
|
# that we avoid running autodiscover again when the URLconf is loaded by
|
2008-12-24 02:25:24 +08:00
|
|
|
# the exception handler to resolve the handler500 view. This prevents an
|
|
|
|
# admin.py module with errors from re-registering models and raising a
|
|
|
|
# spurious AlreadyRegistered exception (see #8245).
|
|
|
|
global LOADING
|
|
|
|
if LOADING:
|
|
|
|
return
|
|
|
|
LOADING = True
|
|
|
|
|
2008-08-02 07:31:20 +08:00
|
|
|
import imp
|
2008-07-19 07:54:34 +08:00
|
|
|
from django.conf import settings
|
2008-08-27 00:18:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
for app in settings.INSTALLED_APPS:
|
2008-08-27 00:18:20 +08:00
|
|
|
# For each app, we need to look for an admin.py inside that app's
|
|
|
|
# package. We can't use os.path here -- recall that modules may be
|
|
|
|
# imported different ways (think zip files) -- so we need to get
|
|
|
|
# the app's __path__ and look for admin.py on that path.
|
|
|
|
|
|
|
|
# Step 1: find out the app's __path__ Import errors here will (and
|
|
|
|
# should) bubble up, but a missing __path__ (which is legal, but weird)
|
|
|
|
# fails silently -- apps that do weird things with __path__ might
|
|
|
|
# need to roll their own admin registration.
|
2010-01-11 02:56:53 +08:00
|
|
|
mod = import_module(app)
|
2008-08-27 00:18:20 +08:00
|
|
|
try:
|
2010-01-11 02:56:53 +08:00
|
|
|
app_path = mod.__path__
|
2008-08-27 00:18:20 +08:00
|
|
|
except AttributeError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Step 2: use imp.find_module to find the app's admin.py. For some
|
|
|
|
# reason imp.find_module raises ImportError if the app can't be found
|
|
|
|
# but doesn't actually try to import the module. So skip this app if
|
|
|
|
# its admin.py doesn't exist
|
2008-07-19 07:54:34 +08:00
|
|
|
try:
|
2008-08-27 00:18:20 +08:00
|
|
|
imp.find_module('admin', app_path)
|
2008-07-19 07:54:34 +08:00
|
|
|
except ImportError:
|
2008-08-02 07:29:25 +08:00
|
|
|
continue
|
2008-08-27 00:18:20 +08:00
|
|
|
|
|
|
|
# Step 3: import the app's admin file. If this has errors we want them
|
|
|
|
# to bubble up.
|
2009-03-19 00:55:59 +08:00
|
|
|
import_module("%s.admin" % app)
|
2008-12-24 02:25:24 +08:00
|
|
|
# autodiscover was successful, reset loading flag.
|
|
|
|
LOADING = False
|