Commit 1716b7ce authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

Renamed AppCache to Apps.

Also renamed app_cache to apps and "app cache" to "app registry".

Deprecated AppCache.app_cache_ready() in favor of Apps.ready().
parent e9e522a8
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
from .base import AppConfig     # NOQA
from .cache import app_cache    # NOQA
from .registry import apps      # NOQA
+2 −2
Original line number Diff line number Diff line
@@ -97,8 +97,8 @@ class AppConfig(object):

    def import_models(self, all_models):
        # Dictionary of models for this app, stored in the 'all_models'
        # attribute of the AppCache this AppConfig is attached to. Injected as
        # a parameter because it may get populated before this method has run.
        # attribute of the Apps this AppConfig is attached to. Injected as a
        # parameter because it may get populated before this method has run.
        self.models = all_models

        if module_has_submodule(self.app_module, MODELS_MODULE_NAME):
+24 −17
Original line number Diff line number Diff line
@@ -14,25 +14,26 @@ from django.utils._os import upath
from .base import AppConfig


class AppCache(object):
class Apps(object):
    """
    A cache that stores installed applications and their models. Used to
    provide reverse-relations and for app introspection.
    A registry that stores the configuration of installed applications.

    It also keeps track of models eg. to provide reverse-relations.
    """

    def __init__(self, master=False):
        # Only one master of the app-cache may exist at a given time, and it
        # shall be the app_cache variable defined at the end of this module.
        if master and hasattr(sys.modules[__name__], 'app_cache'):
            raise RuntimeError("You may create only one master app cache.")
        # Only one master registry may exist at a given time, and it shall be
        # the apps variable defined at the end of this module.
        if master and hasattr(sys.modules[__name__], 'apps'):
            raise RuntimeError("You may create only one master registry.")

        # When master is set to False, the app cache isn't populated from
        # When master is set to False, the registry isn't populated from
        # INSTALLED_APPS and ignores the only_installed arguments to
        # get_model[s].
        self.master = master

        # Mapping of app labels => model names => model classes. Used to
        # register models before the app cache is populated and also for
        # register models before the registry is populated and also for
        # applications that aren't installed.
        self.all_models = defaultdict(OrderedDict)

@@ -43,7 +44,7 @@ class AppCache(object):
        # set_available_apps and set_installed_apps.
        self.stored_app_configs = []

        # Internal flags used when populating the master cache.
        # Internal flags used when populating the master registry.
        self._apps_loaded = not self.master
        self._models_loaded = not self.master

@@ -133,9 +134,9 @@ class AppCache(object):
                self.get_models.cache_clear()
                self._models_loaded = True

    def app_cache_ready(self):
    def ready(self):
        """
        Returns true if the model cache is fully populated.
        Returns True if the registry is fully populated.

        Useful for code that wants to cache the results of get_models() for
        themselves once it is safe to do so.
@@ -200,7 +201,7 @@ class AppCache(object):
        By default, models that aren't part of installed apps will *not*
        be included in the list of models. However, if you specify
        only_installed=False, they will be. If you're using a non-default
        AppCache, this argument does nothing - all models will be included.
        Apps, this argument does nothing - all models will be included.

        By default, models that have been swapped out will *not* be
        included in the list of models. However, if you specify
@@ -272,11 +273,11 @@ class AppCache(object):

    def has_app(self, app_name):
        """
        Checks whether an application with this name exists in the app cache.
        Checks whether an application with this name exists in the registry.

        app_name is the full name of the app eg. 'django.contrib.admin'.

        It's safe to call this method at import time, even while the app cache
        It's safe to call this method at import time, even while the registry
        is being populated. It returns None for apps that aren't loaded yet.
        """
        app_config = self.app_configs.get(app_name.rpartition(".")[2])
@@ -286,7 +287,7 @@ class AppCache(object):
        """
        Returns the model class if one is registered and None otherwise.

        It's safe to call this method at import time, even while the app cache
        It's safe to call this method at import time, even while the registry
        is being populated. It returns None for models that aren't loaded yet.
        """
        return self.all_models[app_label].get(model_name.lower())
@@ -371,6 +372,12 @@ class AppCache(object):
        self.get_models.cache_clear()
        return app_config.models_module

    def app_cache_ready(self):
        warnings.warn(
            "app_cache_ready() is deprecated.",
            PendingDeprecationWarning, stacklevel=2)
        return self.ready()

    def get_app(self, app_label):
        """
        Returns the module containing the models for the given app_label.
@@ -446,4 +453,4 @@ class AppCache(object):
            self.register_model(app_label, model)


app_cache = AppCache(master=True)
apps = Apps(master=True)
+6 −6
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@ from django.contrib.auth import logout as auth_logout, REDIRECT_FIELD_NAME
from django.contrib.contenttypes import views as contenttype_views
from django.views.decorators.csrf import csrf_protect
from django.db.models.base import ModelBase
from django.apps import app_cache
from django.apps import apps
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from django.core.urlresolvers import reverse, NoReverseMatch
from django.template.response import TemplateResponse
@@ -160,11 +160,11 @@ class AdminSite(object):
        The default implementation checks that admin and contenttypes apps are
        installed, as well as the auth context processor.
        """
        app_cache.populate_apps()
        if not app_cache.has_app('django.contrib.admin'):
        apps.populate_apps()
        if not apps.has_app('django.contrib.admin'):
            raise ImproperlyConfigured("Put 'django.contrib.admin' in your "
                "INSTALLED_APPS setting in order to use the admin application.")
        if not app_cache.has_app('django.contrib.contenttypes'):
        if not apps.has_app('django.contrib.contenttypes'):
            raise ImproperlyConfigured("Put 'django.contrib.contenttypes' in "
                "your INSTALLED_APPS setting in order to use the admin application.")
        if 'django.contrib.auth.context_processors.auth' not in settings.TEMPLATE_CONTEXT_PROCESSORS:
@@ -381,7 +381,7 @@ class AdminSite(object):
                        app_dict[app_label]['models'].append(model_dict)
                    else:
                        app_dict[app_label] = {
                            'name': app_cache.get_app_config(app_label).verbose_name,
                            'name': apps.get_app_config(app_label).verbose_name,
                            'app_label': app_label,
                            'app_url': reverse('admin:app_list', kwargs={'app_label': app_label}, current_app=self.name),
                            'has_module_perms': has_module_perms,
@@ -408,7 +408,7 @@ class AdminSite(object):

    def app_index(self, request, app_label, extra_context=None):
        user = request.user
        app_name = app_cache.get_app_config(app_label).verbose_name
        app_name = apps.get_app_config(app_label).verbose_name
        has_module_perms = user.has_module_perms(app_label)
        if not has_module_perms:
            raise PermissionDenied
+2 −2
Original line number Diff line number Diff line
from django.apps import app_cache
from django.apps import apps
from django.template import Library

register = Library()

if app_cache.has_app('django.contrib.staticfiles'):
if apps.has_app('django.contrib.staticfiles'):
    from django.contrib.staticfiles.templatetags.staticfiles import static
else:
    from django.templatetags.static import static
Loading