|
| 1 | +from django.conf import settings |
| 2 | +from django.core.exceptions import ImproperlyConfigured |
| 3 | +from django.utils.datastructures import SortedDict |
| 4 | +from django.utils.importlib import import_module |
| 5 | +from django.utils.module_loading import module_has_submodule |
| 6 | + |
| 7 | +import imp |
| 8 | +import sys |
| 9 | +import os |
| 10 | +import threading |
| 11 | + |
| 12 | + |
| 13 | +class MultipleInstancesReturned(Exception): |
| 14 | + "The function returned multiple App instances with the same label" |
| 15 | + pass |
| 16 | + |
1 | 17 | class App(object):
|
2 |
| - def __init__(self, label): |
3 |
| - if '.' in label: |
4 |
| - label = label.split('.')[-1] |
5 |
| - self.label = label |
| 18 | + """ |
| 19 | + An App in Django is a python package that: |
| 20 | + - is listen in the INSTALLED_APPS setting |
| 21 | + - has a models.py file that with class(es) subclassing ModelBase |
| 22 | + """ |
| 23 | + def __init__(self, name): |
| 24 | + self.name = name |
| 25 | + try: |
| 26 | + self.label = name.rsplit('.', 1)[1] |
| 27 | + except IndexError: |
| 28 | + self.label = name |
6 | 29 | # errors raised when trying to import the app
|
7 | 30 | self.errors = []
|
8 | 31 | self.models = []
|
9 | 32 | self.models_module = None
|
10 | 33 |
|
11 | 34 | def __repr__(self):
|
12 |
| - return '<App: %s>' % self.label |
| 35 | + return '<App: %s>' % self.name |
| 36 | + |
| 37 | +class AppCache(object): |
| 38 | + """ |
| 39 | + A cache that stores installed applications and their models. Used to |
| 40 | + provide reverse-relations and for app introspection (e.g. admin). |
| 41 | + """ |
| 42 | + # Use the Borg pattern to share state between all instances. Details at |
| 43 | + # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531. |
| 44 | + __shared_state = dict( |
| 45 | + # List of App instances |
| 46 | + app_instances = [], |
| 47 | + |
| 48 | + # Mapping of app_labels to a dictionary of model names to model code. |
| 49 | + app_models = SortedDict(), |
| 50 | + |
| 51 | + # -- Everything below here is only used when populating the cache -- |
| 52 | + loaded = False, |
| 53 | + handled = {}, |
| 54 | + postponed = [], |
| 55 | + nesting_level = 0, |
| 56 | + write_lock = threading.RLock(), |
| 57 | + _get_models_cache = {}, |
| 58 | + ) |
| 59 | + |
| 60 | + def __init__(self): |
| 61 | + self.__dict__ = self.__shared_state |
| 62 | + |
| 63 | + def _populate(self): |
| 64 | + """ |
| 65 | + Fill in all the cache information. This method is threadsafe, in the |
| 66 | + sense that every caller will see the same state upon return, and if the |
| 67 | + cache is already initialised, it does no work. |
| 68 | + """ |
| 69 | + if self.loaded: |
| 70 | + return |
| 71 | + self.write_lock.acquire() |
| 72 | + try: |
| 73 | + if self.loaded: |
| 74 | + return |
| 75 | + for app_name in settings.INSTALLED_APPS: |
| 76 | + if app_name in self.handled: |
| 77 | + continue |
| 78 | + self.load_app(app_name, True) |
| 79 | + if not self.nesting_level: |
| 80 | + for app_name in self.postponed: |
| 81 | + self.load_app(app_name) |
| 82 | + self.loaded = True |
| 83 | + finally: |
| 84 | + self.write_lock.release() |
| 85 | + |
| 86 | + def load_app(self, app_name, can_postpone=False): |
| 87 | + """ |
| 88 | + Loads the app with the provided fully qualified name, and returns the |
| 89 | + model module. |
| 90 | + """ |
| 91 | + self.handled[app_name] = None |
| 92 | + self.nesting_level += 1 |
| 93 | + |
| 94 | + try: |
| 95 | + app_module = import_module(app_name) |
| 96 | + except ImportError: |
| 97 | + # If the import fails, we assume it was because an path to a |
| 98 | + # class was passed (e.g. "foo.bar.MyApp") |
| 99 | + # We split the app_name by the rightmost dot to get the path |
| 100 | + # and classname, and then try importing it again |
| 101 | + if not '.' in app_name: |
| 102 | + raise |
| 103 | + app_name, app_classname = app_name.rsplit('.', 1) |
| 104 | + app_module = import_module(app_name) |
| 105 | + app_class = getattr(app_module, app_classname) |
| 106 | + else: |
| 107 | + app_class = App |
| 108 | + |
| 109 | + # check if an app instance with that name already exists |
| 110 | + app_instance = self.find_app(app_name) |
| 111 | + if not app_instance: |
| 112 | + app_instance = app_class(app_name) |
| 113 | + self.app_instances.append(app_instance) |
| 114 | + |
| 115 | + try: |
| 116 | + models = import_module('.models', app_name) |
| 117 | + except ImportError: |
| 118 | + self.nesting_level -= 1 |
| 119 | + # If the app doesn't have a models module, we can just ignore the |
| 120 | + # ImportError and return no models for it. |
| 121 | + if not module_has_submodule(app_module, 'models'): |
| 122 | + return None |
| 123 | + # But if the app does have a models module, we need to figure out |
| 124 | + # whether to suppress or propagate the error. If can_postpone is |
| 125 | + # True then it may be that the package is still being imported by |
| 126 | + # Python and the models module isn't available yet. So we add the |
| 127 | + # app to the postponed list and we'll try it again after all the |
| 128 | + # recursion has finished (in populate). If can_postpone is False |
| 129 | + # then it's time to raise the ImportError. |
| 130 | + else: |
| 131 | + if can_postpone: |
| 132 | + self.postponed.append(app_name) |
| 133 | + return None |
| 134 | + else: |
| 135 | + raise |
| 136 | + |
| 137 | + self.nesting_level -= 1 |
| 138 | + app = self.find_app(app_name.split('.')[-1]) |
| 139 | + if app and models is not app.models_module: |
| 140 | + app.models_module = models |
| 141 | + return models |
| 142 | + |
| 143 | + def find_app(self, name): |
| 144 | + "Returns the App instance that matches name" |
| 145 | + for app in self.app_instances: |
| 146 | + if app.name == name: |
| 147 | + return app |
| 148 | + |
| 149 | + def create_app(self, name): |
| 150 | + """create an app instance""" |
| 151 | + name = name.split('.')[-1] |
| 152 | + app = self.find_app(name) |
| 153 | + if not app: |
| 154 | + app = App(name) |
| 155 | + self.app_instances.append(app) |
| 156 | + return app |
| 157 | + |
| 158 | + def app_cache_ready(self): |
| 159 | + """ |
| 160 | + Returns true if the model cache is fully populated. |
| 161 | +
|
| 162 | + Useful for code that wants to cache the results of get_models() for |
| 163 | + themselves once it is safe to do so. |
| 164 | + """ |
| 165 | + return self.loaded |
| 166 | + |
| 167 | + def get_apps(self): |
| 168 | + "Returns a list of all installed modules that contain models." |
| 169 | + self._populate() |
| 170 | + |
| 171 | + # Ensure the returned list is always in the same order (with new apps |
| 172 | + # added at the end). This avoids unstable ordering on the admin app |
| 173 | + # list page, for example. |
| 174 | + return [app.models_module for app in self.app_instances\ |
| 175 | + if app.models_module] |
| 176 | + |
| 177 | + def get_app(self, app_label, emptyOK=False): |
| 178 | + """ |
| 179 | + Returns the module containing the models for the given app_label. If |
| 180 | + the app has no models in it and 'emptyOK' is True, returns None. |
| 181 | + """ |
| 182 | + self._populate() |
| 183 | + self.write_lock.acquire() |
| 184 | + try: |
| 185 | + for app_name in settings.INSTALLED_APPS: |
| 186 | + if app_label == app_name.split('.')[-1]: |
| 187 | + mod = self.load_app(app_name, False) |
| 188 | + if mod is None: |
| 189 | + if emptyOK: |
| 190 | + return None |
| 191 | + else: |
| 192 | + return mod |
| 193 | + raise ImproperlyConfigured("App with label %s could not be found" % app_label) |
| 194 | + finally: |
| 195 | + self.write_lock.release() |
| 196 | + |
| 197 | + def get_app_errors(self): |
| 198 | + "Returns the map of known problems with the INSTALLED_APPS." |
| 199 | + self._populate() |
| 200 | + errors = {} |
| 201 | + for app in self.app_instances: |
| 202 | + if app.errors: |
| 203 | + errors.update({app.label: app.errors}) |
| 204 | + return errors |
| 205 | + |
| 206 | + def get_models(self, app_mod=None, include_auto_created=False, include_deferred=False): |
| 207 | + """ |
| 208 | + Given a module containing models, returns a list of the models. |
| 209 | + Otherwise returns a list of all installed models. |
| 210 | +
|
| 211 | + By default, auto-created models (i.e., m2m models without an |
| 212 | + explicit intermediate table) are not included. However, if you |
| 213 | + specify include_auto_created=True, they will be. |
| 214 | +
|
| 215 | + By default, models created to satisfy deferred attribute |
| 216 | + queries are *not* included in the list of models. However, if |
| 217 | + you specify include_deferred, they will be. |
| 218 | + """ |
| 219 | + cache_key = (app_mod, include_auto_created, include_deferred) |
| 220 | + try: |
| 221 | + return self._get_models_cache[cache_key] |
| 222 | + except KeyError: |
| 223 | + pass |
| 224 | + self._populate() |
| 225 | + if app_mod: |
| 226 | + app_label = app_mod.__name__.split('.')[-2] |
| 227 | + app = self.find_app(app_label) |
| 228 | + if app: |
| 229 | + app_list = [app] |
| 230 | + else: |
| 231 | + app_list = self.app_instances |
| 232 | + model_list = [] |
| 233 | + for app in app_list: |
| 234 | + models = app.models |
| 235 | + model_list.extend( |
| 236 | + model for model in models |
| 237 | + if ((not model._deferred or include_deferred) |
| 238 | + and (not model._meta.auto_created or include_auto_created)) |
| 239 | + ) |
| 240 | + self._get_models_cache[cache_key] = model_list |
| 241 | + return model_list |
| 242 | + |
| 243 | + def get_model(self, app_label, model_name, seed_cache=True): |
| 244 | + """ |
| 245 | + Returns the model matching the given app_label and case-insensitive |
| 246 | + model_name. |
| 247 | +
|
| 248 | + Returns None if no model is found. |
| 249 | + """ |
| 250 | + if seed_cache: |
| 251 | + self._populate() |
| 252 | + app = self.find_app(app_label) |
| 253 | + if app: |
| 254 | + for model in app.models: |
| 255 | + if model_name.lower() == model._meta.object_name.lower(): |
| 256 | + return model |
| 257 | + |
| 258 | + def register_models(self, app_label, *models): |
| 259 | + """ |
| 260 | + Register a set of models as belonging to an app. |
| 261 | + """ |
| 262 | + # Check if there is an existing app instance |
| 263 | + # If there are more than one app instance with the |
| 264 | + # app_label, an MultipleInstancesReturned is raised |
| 265 | + app_instances = [app for app in self.app_instances\ |
| 266 | + if app.label == app_label] |
| 267 | + if len(app_instances) > 1: |
| 268 | + raise MultipleInstancesReturned |
| 269 | + else: |
| 270 | + try: |
| 271 | + app_instance = app_instances[0] |
| 272 | + except IndexError: |
| 273 | + app_instance = None |
| 274 | + |
| 275 | + # Create a new App instance if the ModelBase tries to register |
| 276 | + # an app that isn't listed in INSTALLED_APPS |
| 277 | + if not app_instance: |
| 278 | + app_instance = App(app_label) |
| 279 | + self.app_instances.append(app_instance) |
| 280 | + |
| 281 | + for model in models: |
| 282 | + # Store as 'name: model' pair in a dictionary |
| 283 | + # in the models list of the App instance |
| 284 | + model_name = model._meta.object_name.lower() |
| 285 | + model_dict = self.app_models.setdefault(app_label, SortedDict()) |
| 286 | + if model_name in model_dict: |
| 287 | + # The same model may be imported via different paths (e.g. |
| 288 | + # appname.models and project.appname.models). We use the source |
| 289 | + # filename as a means to detect identity. |
| 290 | + fname1 = os.path.abspath(sys.modules[model.__module__].__file__) |
| 291 | + fname2 = os.path.abspath(sys.modules[model_dict[model_name].__module__].__file__) |
| 292 | + # Since the filename extension could be .py the first time and |
| 293 | + # .pyc or .pyo the second time, ignore the extension when |
| 294 | + # comparing. |
| 295 | + if os.path.splitext(fname1)[0] == os.path.splitext(fname2)[0]: |
| 296 | + continue |
| 297 | + model_dict[model_name] = model |
| 298 | + app_instance.models.append(model) |
| 299 | + self._get_models_cache.clear() |
0 commit comments