diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2013-12-12 23:33:09 +0100 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2013-12-17 10:17:44 +0100 |
| commit | c5eac3a2f69acf12e794f6c9a3418c9051ce16ed (patch) | |
| tree | 07144472ceb249539fb7ea7555b3a0606839625a | |
| parent | b55282b98bf7883490fcc586d501e1b36c7a1c9c (diff) | |
Fleshed out AppConfig objects.
Marginally improved creation of AppConfig stubs for non-installed apps.
| -rw-r--r-- | django/apps/base.py | 27 | ||||
| -rw-r--r-- | django/apps/cache.py | 22 |
2 files changed, 34 insertions, 15 deletions
diff --git a/django/apps/base.py b/django/apps/base.py index 588709be9f..cd8b008017 100644 --- a/django/apps/base.py +++ b/django/apps/base.py @@ -1,25 +1,46 @@ from collections import OrderedDict +from django.utils._os import upath + class AppConfig(object): """ Class representing a Django application and its configuration. """ - def __init__(self, label, models_module=None, installed=True): + def __init__(self, name, app_module, models_module): + # Full Python path to the application eg. 'django.contrib.admin'. + # This is the value that appears in INSTALLED_APPS. + self.name = name + # Last component of the Python path to the application eg. 'admin'. - self.label = label + # This value must be unique across a Django project. + self.label = name.rpartition(".")[2] + + # Root module eg. <module 'django.contrib.admin' from + # 'django/contrib/admin/__init__.pyc'>. + self.app_module = app_module # Module containing models eg. <module 'django.contrib.admin.models' # from 'django/contrib/admin/models.pyc'>. self.models_module = models_module # Mapping of lower case model names to model classes. + # Populated by AppCache.register_models(). self.models = OrderedDict() # Whether the app is in INSTALLED_APPS or was automatically created # when one of its models was imported. - self.installed = installed + self.installed = app_module is not None + + # Filesystem path to the application directory eg. + # u'/usr/lib/python2.7/dist-packages/django/contrib/admin'. + # This is a unicode object on Python 2 and a str on Python 3. + self.path = upath(app_module.__path__[0]) if app_module is not None else None + + @classmethod + def _stub(cls, label): + return cls(label, None, None) def __repr__(self): return '<AppConfig: %s>' % self.label diff --git a/django/apps/cache.py b/django/apps/cache.py index b3991e5e34..182d89cc01 100644 --- a/django/apps/cache.py +++ b/django/apps/cache.py @@ -133,16 +133,15 @@ class BaseAppCache(object): raise self.nesting_level -= 1 - label = self._label_for(models_module) - try: - app_config = self.app_configs[label] - except KeyError: - self.app_configs[label] = AppConfig( - label=label, models_module=models_module) - else: - if not app_config.installed: - app_config.models_module = models_module - app_config.installed = True + + app_config = AppConfig( + name=app_name, app_module=app_module, models_module=models_module) + # If a stub config existed for this app, preserve models registry. + old_app_config = self.app_configs.get(app_config.label) + if old_app_config is not None: + app_config.models = old_app_config.models + self.app_configs[app_config.label] = app_config + return models_module def app_cache_ready(self): @@ -318,8 +317,7 @@ class BaseAppCache(object): try: app_config = self.app_configs[app_label] except KeyError: - app_config = AppConfig( - label=app_label, installed=False) + app_config = AppConfig._stub(app_label) self.app_configs[app_label] = app_config for model in models: # Add the model to the app_config's models dictionary. |
