From 9f8478ff31298d6f77b47aafd5cd654f856798e6 Mon Sep 17 00:00:00 2001 From: Arthur Koziel Date: Tue, 10 Aug 2010 01:01:14 +0000 Subject: [PATCH] [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 --- django/core/apps.py | 27 ++++++++++----------------- django/db/models/loading.py | 4 +--- tests/appcachetests/runtests.py | 9 --------- 3 files changed, 11 insertions(+), 29 deletions(-) diff --git a/django/core/apps.py b/django/core/apps.py index 15d5e17946..850621fc85 100644 --- a/django/core/apps.py +++ b/django/core/apps.py @@ -22,10 +22,6 @@ class App(object): """ def __init__(self, name): self.name = name - try: - self.label = name.rsplit('.', 1)[1] - except IndexError: - self.label = name # errors raised when trying to import the app self.errors = [] self.models = [] @@ -109,7 +105,11 @@ class AppCache(object): # check if an app instance with that name already exists app_instance = self.find_app(app_name) 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) try: @@ -142,6 +142,8 @@ class AppCache(object): def find_app(self, name): "Returns the App instance that matches name" + if '.' in name: + name = name.rsplit('.', 1)[1] for app in self.app_instances: if app.name == name: return app @@ -259,18 +261,7 @@ class AppCache(object): """ Register a set of models as belonging to an app. """ - # Check if there is an existing app instance - # 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 + app_instance = self.find_app(app_label) # Create a new App instance if the ModelBase tries to register # an app that isn't listed in INSTALLED_APPS @@ -297,3 +288,5 @@ class AppCache(object): model_dict[model_name] = model app_instance.models.append(model) self._get_models_cache.clear() + +cache = AppCache() diff --git a/django/db/models/loading.py b/django/db/models/loading.py index 48e5236905..1dadb2280f 100644 --- a/django/db/models/loading.py +++ b/django/db/models/loading.py @@ -1,6 +1,7 @@ "Utilities for loading models and the modules that contain them." from django.conf import settings +from django.core.apps import AppCache, cache from django.core.exceptions import ImproperlyConfigured from django.utils.datastructures import SortedDict from django.utils.importlib import import_module @@ -15,9 +16,6 @@ import threading __all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models', '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 # compatibility. get_apps = cache.get_apps diff --git a/tests/appcachetests/runtests.py b/tests/appcachetests/runtests.py index 0d05702d06..83730cc4af 100644 --- a/tests/appcachetests/runtests.py +++ b/tests/appcachetests/runtests.py @@ -282,15 +282,6 @@ class RegisterModelsTests(AppCacheTestCase): self.assertEqual(app.name, 'model_app') 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__': unittest.main()