1
0
mirror of https://github.com/django/django.git synced 2025-07-04 17:59:13 +00:00

[soc2010/app-loading] use app label instead of fqn

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/app-loading@13568 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Arthur Koziel 2010-08-10 01:01:14 +00:00
parent 56d7859e25
commit 9f8478ff31
3 changed files with 11 additions and 29 deletions

View File

@ -22,10 +22,6 @@ class App(object):
""" """
def __init__(self, name): def __init__(self, name):
self.name = name self.name = name
try:
self.label = name.rsplit('.', 1)[1]
except IndexError:
self.label = name
# errors raised when trying to import the app # errors raised when trying to import the app
self.errors = [] self.errors = []
self.models = [] self.models = []
@ -109,7 +105,11 @@ class AppCache(object):
# check if an app instance with that name already exists # check if an app instance with that name already exists
app_instance = self.find_app(app_name) app_instance = self.find_app(app_name)
if not app_instance: if not app_instance:
app_instance = app_class(app_name) if '.' in app_name:
app_instance_name = app_name.rsplit('.', 1)[1]
else:
app_instance_name = app_name
app_instance = app_class(app_instance_name)
self.app_instances.append(app_instance) self.app_instances.append(app_instance)
try: try:
@ -142,6 +142,8 @@ class AppCache(object):
def find_app(self, name): def find_app(self, name):
"Returns the App instance that matches name" "Returns the App instance that matches name"
if '.' in name:
name = name.rsplit('.', 1)[1]
for app in self.app_instances: for app in self.app_instances:
if app.name == name: if app.name == name:
return app return app
@ -259,18 +261,7 @@ class AppCache(object):
""" """
Register a set of models as belonging to an app. Register a set of models as belonging to an app.
""" """
# Check if there is an existing app instance app_instance = self.find_app(app_label)
# If there are more than one app instance with the
# app_label, an MultipleInstancesReturned is raised
app_instances = [app for app in self.app_instances\
if app.label == app_label]
if len(app_instances) > 1:
raise MultipleInstancesReturned
else:
try:
app_instance = app_instances[0]
except IndexError:
app_instance = None
# Create a new App instance if the ModelBase tries to register # Create a new App instance if the ModelBase tries to register
# an app that isn't listed in INSTALLED_APPS # an app that isn't listed in INSTALLED_APPS
@ -297,3 +288,5 @@ class AppCache(object):
model_dict[model_name] = model model_dict[model_name] = model
app_instance.models.append(model) app_instance.models.append(model)
self._get_models_cache.clear() self._get_models_cache.clear()
cache = AppCache()

View File

@ -1,6 +1,7 @@
"Utilities for loading models and the modules that contain them." "Utilities for loading models and the modules that contain them."
from django.conf import settings from django.conf import settings
from django.core.apps import AppCache, cache
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
from django.utils.importlib import import_module from django.utils.importlib import import_module
@ -15,9 +16,6 @@ import threading
__all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models', __all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models',
'load_app', 'app_cache_ready') 'load_app', 'app_cache_ready')
from django.core.apps import AppCache
cache = AppCache()
# These methods were always module level, so are kept that way for backwards # These methods were always module level, so are kept that way for backwards
# compatibility. # compatibility.
get_apps = cache.get_apps get_apps = cache.get_apps

View File

@ -282,15 +282,6 @@ class RegisterModelsTests(AppCacheTestCase):
self.assertEqual(app.name, 'model_app') self.assertEqual(app.name, 'model_app')
self.assertEqual(app.models[0].__name__, 'Person') self.assertEqual(app.models[0].__name__, 'Person')
def test_multiple_apps_with_same_label(self):
"""
Test that an exception is raised when the function gets passed an
app label but there are multiple app instances with that label
"""
cache.load_app('model_app')
self.assertRaises(MultipleInstancesReturned, cache.load_app,
'same_label.model_app')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()