Simplified handling of available_apps slightly.

It feels more natural for self.available_apps to contain app names (like
INSTALLED_APPS) than app labels, and this is easy to implement now.
This commit is contained in:
Aymeric Augustin 2013-12-13 20:51:21 +01:00
parent 259cd3cd41
commit e85932b54e
1 changed files with 17 additions and 10 deletions

View File

@ -35,7 +35,7 @@ def _initialize():
# Pending lookups for lazy relations # Pending lookups for lazy relations
pending_lookups={}, pending_lookups={},
# List of app_labels that allows restricting the set of apps. # Set of app names. Allows restricting the set of installed apps.
# Used by TransactionTestCase.available_apps for performance reasons. # Used by TransactionTestCase.available_apps for performance reasons.
available_apps=None, available_apps=None,
@ -165,7 +165,7 @@ class BaseAppCache(object):
for app_config in self.app_configs.values(): for app_config in self.app_configs.values():
if only_installed and not app_config.installed: if only_installed and not app_config.installed:
continue continue
if self.available_apps is not None and app_config.label not in self.available_apps: if self.available_apps is not None and app_config.name not in self.available_apps:
continue continue
yield app_config yield app_config
@ -185,7 +185,7 @@ class BaseAppCache(object):
app_config = self.app_configs.get(app_label) app_config = self.app_configs.get(app_label)
if app_config is None or (only_installed and not app_config.installed): if app_config is None or (only_installed and not app_config.installed):
raise LookupError("No app with label %r." % app_label) raise LookupError("No app with label %r." % app_label)
if self.available_apps is not None and app_config.label not in self.available_apps: if self.available_apps is not None and app_config.name not in self.available_apps:
raise UnavailableApp("App with label %r isn't available." % app_label) raise UnavailableApp("App with label %r isn't available." % app_label)
return app_config return app_config
@ -239,7 +239,10 @@ class BaseAppCache(object):
try: try:
model_list = self._get_models_cache[cache_key] model_list = self._get_models_cache[cache_key]
if self.available_apps is not None and only_installed: if self.available_apps is not None and only_installed:
model_list = [m for m in model_list if m._meta.app_label in self.available_apps] model_list = [
m for m in model_list
if self.app_configs[m._meta.app_label].name in self.available_apps
]
return model_list return model_list
except KeyError: except KeyError:
pass pass
@ -266,7 +269,10 @@ class BaseAppCache(object):
) )
self._get_models_cache[cache_key] = model_list self._get_models_cache[cache_key] = model_list
if self.available_apps is not None and only_installed: if self.available_apps is not None and only_installed:
model_list = [m for m in model_list if m._meta.app_label in self.available_apps] model_list = [
m for m in model_list
if self.app_configs[m._meta.app_label].name in self.available_apps
]
return model_list return model_list
def get_model(self, app_label, model_name, def get_model(self, app_label, model_name,
@ -289,7 +295,7 @@ class BaseAppCache(object):
if app_config is not None and not app_config.installed: if app_config is not None and not app_config.installed:
return None return None
if (self.available_apps is not None if (self.available_apps is not None
and app_label not in self.available_apps): and app_config.name not in self.available_apps):
raise UnavailableApp("App with label %s isn't available." % app_label) raise UnavailableApp("App with label %s isn't available." % app_label)
try: try:
return self.app_configs[app_label].models[model_name.lower()] return self.app_configs[app_label].models[model_name.lower()]
@ -325,11 +331,12 @@ class BaseAppCache(object):
self._get_models_cache.clear() self._get_models_cache.clear()
def set_available_apps(self, available): def set_available_apps(self, available):
if not set(available).issubset(set(settings.INSTALLED_APPS)): available = set(available)
extra = set(available) - set(settings.INSTALLED_APPS) installed = set(settings.INSTALLED_APPS)
if not available.issubset(installed):
raise ValueError("Available apps isn't a subset of installed " raise ValueError("Available apps isn't a subset of installed "
"apps, extra apps: " + ", ".join(extra)) "apps, extra apps: %s" % ", ".join(available - installed))
self.available_apps = set(app.rsplit('.', 1)[-1] for app in available) self.available_apps = available
def unset_available_apps(self): def unset_available_apps(self):
self.available_apps = None self.available_apps = None