Skip to content

Commit 4649d85

Browse files
committed
Introducing django-1.7.1
- Legacy-Id: 8817
1 parent 435d944 commit 4649d85

3,699 files changed

Lines changed: 510162 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

django/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
VERSION = (1, 7, 1, 'final', 0)
2+
3+
4+
def get_version(*args, **kwargs):
5+
# Don't litter django/__init__.py with all the get_version stuff.
6+
# Only import if it's actually called.
7+
from django.utils.version import get_version
8+
return get_version(*args, **kwargs)
9+
10+
11+
def setup():
12+
"""
13+
Configure the settings (this happens as a side effect of accessing the
14+
first setting), configure logging and populate the app registry.
15+
"""
16+
from django.apps import apps
17+
from django.conf import settings
18+
from django.utils.log import configure_logging
19+
20+
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
21+
apps.populate(settings.INSTALLED_APPS)

django/apps/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .config import AppConfig # NOQA
2+
from .registry import apps # NOQA

django/apps/config.py

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
from importlib import import_module
2+
import os
3+
4+
from django.core.exceptions import AppRegistryNotReady, ImproperlyConfigured
5+
from django.utils.module_loading import module_has_submodule
6+
from django.utils._os import upath
7+
8+
9+
MODELS_MODULE_NAME = 'models'
10+
11+
12+
class AppConfig(object):
13+
"""
14+
Class representing a Django application and its configuration.
15+
"""
16+
17+
def __init__(self, app_name, app_module):
18+
# Full Python path to the application eg. 'django.contrib.admin'.
19+
self.name = app_name
20+
21+
# Root module for the application eg. <module 'django.contrib.admin'
22+
# from 'django/contrib/admin/__init__.pyc'>.
23+
self.module = app_module
24+
25+
# The following attributes could be defined at the class level in a
26+
# subclass, hence the test-and-set pattern.
27+
28+
# Last component of the Python path to the application eg. 'admin'.
29+
# This value must be unique across a Django project.
30+
if not hasattr(self, 'label'):
31+
self.label = app_name.rpartition(".")[2]
32+
33+
# Human-readable name for the application eg. "Admin".
34+
if not hasattr(self, 'verbose_name'):
35+
self.verbose_name = self.label.title()
36+
37+
# Filesystem path to the application directory eg.
38+
# u'/usr/lib/python2.7/dist-packages/django/contrib/admin'. Unicode on
39+
# Python 2 and a str on Python 3.
40+
if not hasattr(self, 'path'):
41+
self.path = self._path_from_module(app_module)
42+
43+
# Module containing models eg. <module 'django.contrib.admin.models'
44+
# from 'django/contrib/admin/models.pyc'>. Set by import_models().
45+
# None if the application doesn't have a models module.
46+
self.models_module = None
47+
48+
# Mapping of lower case model names to model classes. Initially set to
49+
# None to prevent accidental access before import_models() runs.
50+
self.models = None
51+
52+
def __repr__(self):
53+
return '<%s: %s>' % (self.__class__.__name__, self.label)
54+
55+
def _path_from_module(self, module):
56+
"""Attempt to determine app's filesystem path from its module."""
57+
# See #21874 for extended discussion of the behavior of this method in
58+
# various cases.
59+
# Convert paths to list because Python 3.3 _NamespacePath does not
60+
# support indexing.
61+
paths = list(getattr(module, '__path__', []))
62+
if len(paths) != 1:
63+
filename = getattr(module, '__file__', None)
64+
if filename is not None:
65+
paths = [os.path.dirname(filename)]
66+
if len(paths) > 1:
67+
raise ImproperlyConfigured(
68+
"The app module %r has multiple filesystem locations (%r); "
69+
"you must configure this app with an AppConfig subclass "
70+
"with a 'path' class attribute." % (module, paths))
71+
elif not paths:
72+
raise ImproperlyConfigured(
73+
"The app module %r has no filesystem location, "
74+
"you must configure this app with an AppConfig subclass "
75+
"with a 'path' class attribute." % (module,))
76+
return upath(paths[0])
77+
78+
@classmethod
79+
def create(cls, entry):
80+
"""
81+
Factory that creates an app config from an entry in INSTALLED_APPS.
82+
"""
83+
try:
84+
# If import_module succeeds, entry is a path to an app module,
85+
# which may specify an app config class with default_app_config.
86+
# Otherwise, entry is a path to an app config class or an error.
87+
module = import_module(entry)
88+
89+
except ImportError:
90+
# Track that importing as an app module failed. If importing as an
91+
# app config class fails too, we'll trigger the ImportError again.
92+
module = None
93+
94+
mod_path, _, cls_name = entry.rpartition('.')
95+
96+
# Raise the original exception when entry cannot be a path to an
97+
# app config class.
98+
if not mod_path:
99+
raise
100+
101+
else:
102+
try:
103+
# If this works, the app module specifies an app config class.
104+
entry = module.default_app_config
105+
except AttributeError:
106+
# Otherwise, it simply uses the default app config class.
107+
return cls(entry, module)
108+
else:
109+
mod_path, _, cls_name = entry.rpartition('.')
110+
111+
# If we're reaching this point, we must attempt to load the app config
112+
# class located at <mod_path>.<cls_name>
113+
114+
# Avoid django.utils.module_loading.import_by_path because it
115+
# masks errors -- it reraises ImportError as ImproperlyConfigured.
116+
mod = import_module(mod_path)
117+
try:
118+
cls = getattr(mod, cls_name)
119+
except AttributeError:
120+
if module is None:
121+
# If importing as an app module failed, that error probably
122+
# contains the most informative traceback. Trigger it again.
123+
import_module(entry)
124+
else:
125+
raise
126+
127+
# Check for obvious errors. (This check prevents duck typing, but
128+
# it could be removed if it became a problem in practice.)
129+
if not issubclass(cls, AppConfig):
130+
raise ImproperlyConfigured(
131+
"'%s' isn't a subclass of AppConfig." % entry)
132+
133+
# Obtain app name here rather than in AppClass.__init__ to keep
134+
# all error checking for entries in INSTALLED_APPS in one place.
135+
try:
136+
app_name = cls.name
137+
except AttributeError:
138+
raise ImproperlyConfigured(
139+
"'%s' must supply a name attribute." % entry)
140+
141+
# Ensure app_name points to a valid module.
142+
app_module = import_module(app_name)
143+
144+
# Entry is a path to an app config class.
145+
return cls(app_name, app_module)
146+
147+
def check_models_ready(self):
148+
"""
149+
Raises an exception if models haven't been imported yet.
150+
"""
151+
if self.models is None:
152+
raise AppRegistryNotReady(
153+
"Models for app '%s' haven't been imported yet." % self.label)
154+
155+
def get_model(self, model_name):
156+
"""
157+
Returns the model with the given case-insensitive model_name.
158+
159+
Raises LookupError if no model exists with this name.
160+
"""
161+
self.check_models_ready()
162+
try:
163+
return self.models[model_name.lower()]
164+
except KeyError:
165+
raise LookupError(
166+
"App '%s' doesn't have a '%s' model." % (self.label, model_name))
167+
168+
def get_models(self, include_auto_created=False,
169+
include_deferred=False, include_swapped=False):
170+
"""
171+
Returns an iterable of models.
172+
173+
By default, the following models aren't included:
174+
175+
- auto-created models for many-to-many relations without
176+
an explicit intermediate table,
177+
- models created to satisfy deferred attribute queries,
178+
- models that have been swapped out.
179+
180+
Set the corresponding keyword argument to True to include such models.
181+
Keyword arguments aren't documented; they're a private API.
182+
"""
183+
self.check_models_ready()
184+
for model in self.models.values():
185+
if model._deferred and not include_deferred:
186+
continue
187+
if model._meta.auto_created and not include_auto_created:
188+
continue
189+
if model._meta.swapped and not include_swapped:
190+
continue
191+
yield model
192+
193+
def import_models(self, all_models):
194+
# Dictionary of models for this app, primarily maintained in the
195+
# 'all_models' attribute of the Apps this AppConfig is attached to.
196+
# Injected as a parameter because it gets populated when models are
197+
# imported, which might happen before populate() imports models.
198+
self.models = all_models
199+
200+
if module_has_submodule(self.module, MODELS_MODULE_NAME):
201+
models_module_name = '%s.%s' % (self.name, MODELS_MODULE_NAME)
202+
self.models_module = import_module(models_module_name)
203+
204+
def ready(self):
205+
"""
206+
Override this method in subclasses to run code when Django starts.
207+
"""

0 commit comments

Comments
 (0)