From c5eac3a2f69acf12e794f6c9a3418c9051ce16ed Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Thu, 12 Dec 2013 23:33:09 +0100 Subject: [PATCH] Fleshed out AppConfig objects. Marginally improved creation of AppConfig stubs for non-installed apps. --- django/apps/base.py | 27 ++++++++++++++++++++++++--- 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. . + self.app_module = app_module # Module containing models eg. . 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 '' % 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.