| 1 |
"Utilities for loading models and the modules that contain them." |
|---|
| 2 |
|
|---|
| 3 |
from django.conf import settings |
|---|
| 4 |
from django.core.exceptions import ImproperlyConfigured |
|---|
| 5 |
import sys |
|---|
| 6 |
import os |
|---|
| 7 |
|
|---|
| 8 |
__all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models') |
|---|
| 9 |
|
|---|
| 10 |
_app_list = [] # Cache of installed apps. |
|---|
| 11 |
# Entry is not placed in app_list cache until entire app is loaded. |
|---|
| 12 |
_app_models = {} # Dictionary of models against app label |
|---|
| 13 |
# Each value is a dictionary of model name: model class |
|---|
| 14 |
# Applabel and Model entry exists in cache when individual model is loaded. |
|---|
| 15 |
_app_errors = {} # Dictionary of errors that were experienced when loading the INSTALLED_APPS |
|---|
| 16 |
# Key is the app_name of the model, value is the exception that was raised |
|---|
| 17 |
# during model loading. |
|---|
| 18 |
_loaded = False # Has the contents of settings.INSTALLED_APPS been loaded? |
|---|
| 19 |
# i.e., has get_apps() been called? |
|---|
| 20 |
|
|---|
| 21 |
def get_apps(): |
|---|
| 22 |
"Returns a list of all installed modules that contain models." |
|---|
| 23 |
global _app_list |
|---|
| 24 |
global _loaded |
|---|
| 25 |
if not _loaded: |
|---|
| 26 |
_loaded = True |
|---|
| 27 |
for app_name in settings.INSTALLED_APPS: |
|---|
| 28 |
try: |
|---|
| 29 |
load_app(app_name) |
|---|
| 30 |
except Exception, e: |
|---|
| 31 |
# Problem importing the app |
|---|
| 32 |
_app_errors[app_name] = e |
|---|
| 33 |
return _app_list |
|---|
| 34 |
|
|---|
| 35 |
def get_app(app_label, emptyOK=False): |
|---|
| 36 |
"Returns the module containing the models for the given app_label. If the app has no models in it and 'emptyOK' is True, returns None." |
|---|
| 37 |
get_apps() # Run get_apps() to populate the _app_list cache. Slightly hackish. |
|---|
| 38 |
for app_name in settings.INSTALLED_APPS: |
|---|
| 39 |
if app_label == app_name.split('.')[-1]: |
|---|
| 40 |
mod = load_app(app_name) |
|---|
| 41 |
if mod is None: |
|---|
| 42 |
if emptyOK: |
|---|
| 43 |
return None |
|---|
| 44 |
else: |
|---|
| 45 |
return mod |
|---|
| 46 |
raise ImproperlyConfigured, "App with label %s could not be found" % app_label |
|---|
| 47 |
|
|---|
| 48 |
def load_app(app_name): |
|---|
| 49 |
"Loads the app with the provided fully qualified name, and returns the model module." |
|---|
| 50 |
global _app_list |
|---|
| 51 |
mod = __import__(app_name, {}, {}, ['models']) |
|---|
| 52 |
if not hasattr(mod, 'models'): |
|---|
| 53 |
return None |
|---|
| 54 |
if mod.models not in _app_list: |
|---|
| 55 |
_app_list.append(mod.models) |
|---|
| 56 |
return mod.models |
|---|
| 57 |
|
|---|
| 58 |
def get_app_errors(): |
|---|
| 59 |
"Returns the map of known problems with the INSTALLED_APPS" |
|---|
| 60 |
global _app_errors |
|---|
| 61 |
get_apps() # Run get_apps() to populate the _app_list cache. Slightly hackish. |
|---|
| 62 |
return _app_errors |
|---|
| 63 |
|
|---|
| 64 |
def get_models(app_mod=None): |
|---|
| 65 |
""" |
|---|
| 66 |
Given a module containing models, returns a list of the models. Otherwise |
|---|
| 67 |
returns a list of all installed models. |
|---|
| 68 |
""" |
|---|
| 69 |
app_list = get_apps() # Run get_apps() to populate the _app_list cache. Slightly hackish. |
|---|
| 70 |
if app_mod: |
|---|
| 71 |
return _app_models.get(app_mod.__name__.split('.')[-2], {}).values() |
|---|
| 72 |
else: |
|---|
| 73 |
model_list = [] |
|---|
| 74 |
for app_mod in app_list: |
|---|
| 75 |
model_list.extend(get_models(app_mod)) |
|---|
| 76 |
return model_list |
|---|
| 77 |
|
|---|
| 78 |
def get_model(app_label, model_name, seed_cache=True): |
|---|
| 79 |
""" |
|---|
| 80 |
Returns the model matching the given app_label and case-insensitive |
|---|
| 81 |
model_name. |
|---|
| 82 |
|
|---|
| 83 |
Returns None if no model is found. |
|---|
| 84 |
""" |
|---|
| 85 |
if seed_cache: |
|---|
| 86 |
get_apps() |
|---|
| 87 |
try: |
|---|
| 88 |
model_dict = _app_models[app_label] |
|---|
| 89 |
except KeyError: |
|---|
| 90 |
return None |
|---|
| 91 |
|
|---|
| 92 |
try: |
|---|
| 93 |
return model_dict[model_name.lower()] |
|---|
| 94 |
except KeyError: |
|---|
| 95 |
return None |
|---|
| 96 |
|
|---|
| 97 |
def register_models(app_label, *models): |
|---|
| 98 |
""" |
|---|
| 99 |
Register a set of models as belonging to an app. |
|---|
| 100 |
""" |
|---|
| 101 |
for model in models: |
|---|
| 102 |
# Store as 'name: model' pair in a dictionary |
|---|
| 103 |
# in the _app_models dictionary |
|---|
| 104 |
model_name = model._meta.object_name.lower() |
|---|
| 105 |
model_dict = _app_models.setdefault(app_label, {}) |
|---|
| 106 |
if model_name in model_dict: |
|---|
| 107 |
# The same model may be imported via different paths (e.g. |
|---|
| 108 |
# appname.models and project.appname.models). We use the source |
|---|
| 109 |
# filename as a means to detect identity. |
|---|
| 110 |
fname1 = os.path.abspath(sys.modules[model.__module__].__file__) |
|---|
| 111 |
fname2 = os.path.abspath(sys.modules[model_dict[model_name].__module__].__file__) |
|---|
| 112 |
# Since the filename extension could be .py the first time and .pyc |
|---|
| 113 |
# or .pyo the second time, ignore the extension when comparing. |
|---|
| 114 |
if os.path.splitext(fname1)[0] == os.path.splitext(fname2)[0]: |
|---|
| 115 |
continue |
|---|
| 116 |
model_dict[model_name] = model |
|---|