Commit c5eac3a2 authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

Fleshed out AppConfig objects.

Marginally improved creation of AppConfig stubs for non-installed apps.
parent b55282b9
Loading
Loading
Loading
Loading
+24 −3
Original line number Diff line number Diff line
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
+10 −12
Original line number Diff line number Diff line
@@ -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.