Commit 8662654d authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

Removed module-level functions for the app cache.

Since the original ones in django.db.models.loading were kept only for
backwards compatibility, there's no need to recreate them. However, many
internals of Django still relied on them.

They were also imported in django.db.models. They never appear in the
documentation, except a quick mention of get_models and get_app in the
1.2 release notes to document an edge case in GIS. I don't think that
makes them a public API.

This commit doesn't change the overall amount of global state but
clarifies that it's tied to the app_cache object instead of hiding it
behind half a dozen functions.
parent 33455133
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
from .cache import app_cache, UnavailableApp    # NOQA
+1 −18
Original line number Diff line number Diff line
@@ -12,8 +12,6 @@ from django.utils.module_loading import module_has_submodule
from django.utils._os import upath
from django.utils import six

__all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models',
        'load_app', 'app_cache_ready')

MODELS_MODULE_NAME = 'models'

@@ -361,19 +359,4 @@ class AppCache(BaseAppCache):
        self.__dict__ = self.__shared_state


cache = AppCache()


# These methods were always module level, so are kept that way for backwards
# compatibility.
get_apps = cache.get_apps
get_app_package = cache.get_app_package
get_app_path = cache.get_app_path
get_app_paths = cache.get_app_paths
get_app = cache.get_app
get_app_errors = cache.get_app_errors
get_models = cache.get_models
get_model = cache.get_model
register_models = cache.register_models
load_app = cache.load_app
app_cache_ready = cache.app_cache_ready
app_cache = AppCache()
+2 −1
Original line number Diff line number Diff line
from django.apps import app_cache
from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.db.models.fields import FieldDoesNotExist
@@ -17,7 +18,7 @@ class BaseValidator(object):
    def __init__(self):
        # Before we can introspect models, they need to be fully loaded so that
        # inter-relations are set up correctly. We force that here.
        models.get_apps()
        app_cache.get_apps()

    def validate(self, cls, model):
        for m in dir(self):
+4 −3
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import os
import re

from django import template
from django.apps import app_cache
from django.conf import settings
from django.contrib import admin
from django.contrib.admin.views.decorators import staff_member_required
@@ -182,7 +183,7 @@ class ModelIndexView(BaseAdminDocsView):
    template_name = 'admin_doc/model_index.html'

    def get_context_data(self, **kwargs):
        m_list = [m._meta for m in models.get_models()]
        m_list = [m._meta for m in app_cache.get_models()]
        kwargs.update({'models': m_list})
        return super(ModelIndexView, self).get_context_data(**kwargs)

@@ -193,11 +194,11 @@ class ModelDetailView(BaseAdminDocsView):
    def get_context_data(self, **kwargs):
        # Get the model class.
        try:
            app_mod = models.get_app(self.kwargs['app_label'])
            app_mod = app_cache.get_app(self.kwargs['app_label'])
        except ImproperlyConfigured:
            raise Http404(_("App %r not found") % self.kwargs['app_label'])
        model = None
        for m in models.get_models(app_mod):
        for m in app_cache.get_models(app_mod):
            if m._meta.model_name == self.kwargs['model_name']:
                model = m
                break
+2 −2
Original line number Diff line number Diff line
@@ -123,13 +123,13 @@ def get_user_model():
    """
    Returns the User model that is active in this project.
    """
    from django.db.models import get_model
    from django.apps import app_cache

    try:
        app_label, model_name = settings.AUTH_USER_MODEL.split('.')
    except ValueError:
        raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form 'app_label.model_name'")
    user_model = get_model(app_label, model_name)
    user_model = app_cache.get_model(app_label, model_name)
    if user_model is None:
        raise ImproperlyConfigured("AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL)
    return user_model
Loading