Skip to content

Commit

Permalink
Added get_app_paths() to the AppCache.
Browse files Browse the repository at this point in the history
This method is useful to discover files inside apps.
  • Loading branch information
aaugustin committed Jun 2, 2013
1 parent 09d0568 commit b55624a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion django/db/models/__init__.py
@@ -1,7 +1,7 @@
from functools import wraps

from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured
from django.db.models.loading import get_apps, get_app, get_models, get_model, register_models
from django.db.models.loading import get_apps, get_app_paths, get_app, get_models, get_model, register_models
from django.db.models.query import Q
from django.db.models.expressions import F
from django.db.models.manager import Manager
Expand Down
22 changes: 21 additions & 1 deletion django/db/models/loading.py
Expand Up @@ -130,7 +130,9 @@ def app_cache_ready(self):
return self.loaded

def get_apps(self):
"Returns a list of all installed modules that contain models."
"""
Returns a list of all installed modules that contain models.
"""
self._populate()

# Ensure the returned list is always in the same order (with new apps
Expand All @@ -140,6 +142,23 @@ def get_apps(self):
apps.sort()
return [elt[1] for elt in apps]

def get_app_paths(self):

This comment has been minimized.

Copy link
@timgraham

timgraham Jul 18, 2013

Member

This is a little confusing to me in that the paths it returns contain models.pyc or end with "/models"

I propose modifying it to return just the app paths so that callers don't have to call or.path.dirname on each path that's returned. Let me know if I'm missing something.

#1359

This comment has been minimized.

Copy link
@aaugustin

aaugustin Jul 19, 2013

Author Member

Please do.

This method is less useful than I originally envisioned because it returns a list based on the app cache, while most discoveries are based on INSTALLED_APPS, which may be a subset of the applications registered in the app cache.

"""
Returns a list of paths to all installed apps.
Useful for discovering files at conventional locations inside apps
(static files, templates, etc.)
"""
self._populate()

app_paths = []
for app in self.get_apps():
if hasattr(app, '__path__'): # models/__init__.py package
app_paths.extend([upath(path) for path in app.__path__])

This comment has been minimized.

Copy link
@timgraham

timgraham Jul 18, 2013

Member

will app.path ever be a list of length > 1? I changed this to app.path[0] in the pull request referenced in the above comment - again, let me know if I'm missing something.

This comment has been minimized.

Copy link
@aaugustin

aaugustin Jul 19, 2013

Author Member

I didn't write that code, merely moved it. If __path__ is a list I assume it could contain more than one element but I don't know under which circumstances that could happen.

else: # models.py module
app_paths.append(upath(app.__file__))
return app_paths

def get_app(self, app_label, emptyOK=False):
"""
Returns the module containing the models for the given app_label. If
Expand Down Expand Up @@ -260,6 +279,7 @@ def register_models(self, app_label, *models):
# These methods were always module level, so are kept that way for backwards
# compatibility.
get_apps = cache.get_apps
get_app_paths = cache.get_app_paths
get_app = cache.get_app
get_app_errors = cache.get_app_errors
get_models = cache.get_models
Expand Down

0 comments on commit b55624a

Please sign in to comment.