diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2013-06-02 10:13:54 +0200 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2013-06-02 20:39:45 +0200 |
| commit | b55624a0263e31be80fbe7bb51e47ccd253e7a5d (patch) | |
| tree | d1cc0fefa4dd8033f666786f676170008cc644bf | |
| parent | 09d0568697cf5f82737816cf8041a6c270861a7d (diff) | |
Added get_app_paths() to the AppCache.
This method is useful to discover files inside apps.
| -rw-r--r-- | django/db/models/__init__.py | 2 | ||||
| -rw-r--r-- | django/db/models/loading.py | 22 |
2 files changed, 22 insertions, 2 deletions
diff --git a/django/db/models/__init__.py b/django/db/models/__init__.py index 5f17229753..3eac2167d4 100644 --- a/django/db/models/__init__.py +++ b/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 diff --git a/django/db/models/loading.py b/django/db/models/loading.py index c027105c5b..4372873e50 100644 --- a/django/db/models/loading.py +++ b/django/db/models/loading.py @@ -130,7 +130,9 @@ class AppCache(object): 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 @@ -140,6 +142,23 @@ class AppCache(object): apps.sort() return [elt[1] for elt in apps] + def get_app_paths(self): + """ + 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__]) + 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 @@ -260,6 +279,7 @@ cache = AppCache() # 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 |
