-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented two-stage app-cache population.
First stage imports app modules. It doesn't catch import errors. This matches the previous behavior and keeps the code simple. Second stage import models modules. It catches import errors and retries them after walking through the entire list once. This matches the previous behavior and seems useful. populate_models() is intended to be equivalent to populate(). It isn't wired yet. That is coming in the next commit.
- Loading branch information
Showing
3 changed files
with
117 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,53 @@ | ||
from collections import OrderedDict | ||
from importlib import import_module | ||
|
||
from django.utils.module_loading import module_has_submodule | ||
from django.utils._os import upath | ||
|
||
|
||
MODELS_MODULE_NAME = 'models' | ||
|
||
|
||
class AppConfig(object): | ||
""" | ||
Class representing a Django application and its configuration. | ||
""" | ||
|
||
def __init__(self, name, app_module, models_module): | ||
def __init__(self, app_name): | ||
# Full Python path to the application eg. 'django.contrib.admin'. | ||
# This is the value that appears in INSTALLED_APPS. | ||
self.name = name | ||
self.name = app_name | ||
|
||
# Last component of the Python path to the application eg. 'admin'. | ||
# This value must be unique across a Django project. | ||
self.label = name.rpartition(".")[2] | ||
self.label = app_name.rpartition(".")[2] | ||
|
||
# Root module eg. <module 'django.contrib.admin' from | ||
# 'django/contrib/admin/__init__.pyc'>. | ||
self.app_module = app_module | ||
self.app_module = import_module(app_name) | ||
|
||
# Module containing models eg. <module 'django.contrib.admin.models' | ||
# from 'django/contrib/admin/models.pyc'>. None if the application | ||
# doesn't have a models module. | ||
self.models_module = models_module | ||
# from 'django/contrib/admin/models.pyc'>. Set by import_models(). | ||
# None if the application doesn't have a models module. | ||
self.models_module = None | ||
|
||
# Mapping of lower case model names to model classes. | ||
# Populated by calls to AppCache.register_model(). | ||
self.models = OrderedDict() | ||
# Mapping of lower case model names to model classes. Initally set to | ||
# None to prevent accidental access before import_models() runs. | ||
self.models = None | ||
|
||
# Filesystem path to the application directory eg. | ||
# u'/usr/lib/python2.7/dist-packages/django/contrib/admin'. | ||
# This is a unicode object on Python 2 and a str on Python 3. | ||
self.path = upath(app_module.__path__[0]) | ||
self.path = upath(self.app_module.__path__[0]) | ||
|
||
def __repr__(self): | ||
return '<AppConfig: %s>' % self.label | ||
|
||
def import_models(self, all_models): | ||
# Dictionary of models for this app, stored in the 'all_models' | ||
# attribute of the AppCache this AppConfig is attached to. Injected as | ||
# a parameter because it may get populated before this method has run. | ||
self.models = all_models | ||
|
||
if module_has_submodule(self.app_module, MODELS_MODULE_NAME): | ||
models_module_name = '%s.%s' % (self.name, MODELS_MODULE_NAME) | ||
self.models_module = import_module(models_module_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters