mirror of
https://github.com/django/django.git
synced 2025-07-04 09:49:12 +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:
parent
56d7859e25
commit
9f8478ff31
@ -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()
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user