summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2015-02-05 12:14:36 +0100
committerAymeric Augustin <aymeric.augustin@m4x.org>2015-02-05 12:43:55 +0100
commitde62b8ef4587e8f01462629e2812f06f9ddf71e8 (patch)
treedca84fa3fc0587b517a6eb6d555d0f60c202eccd
parent737b184d914d5cc4a6ed8fe2a1d66ec1b7369f46 (diff)
Simplified handling of RegexURLResolver.urlconf_module.
Also made RegexURLResolver.url_patterns a cached property for consistency and efficiency.
-rw-r--r--django/core/urlresolvers.py21
1 files changed, 10 insertions, 11 deletions
diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py
index d7383a0422..702290b61d 100644
--- a/django/core/urlresolvers.py
+++ b/django/core/urlresolvers.py
@@ -19,7 +19,7 @@ from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
from django.utils.datastructures import MultiValueDict
from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.encoding import force_str, force_text, iri_to_uri
-from django.utils.functional import lazy
+from django.utils.functional import cached_property, lazy
from django.utils.http import RFC3986_SUBDELIMS, urlquote
from django.utils.module_loading import module_has_submodule
from django.utils.regex_helper import normalize
@@ -252,10 +252,10 @@ class RegexURLPattern(LocaleRegexProvider):
class RegexURLResolver(LocaleRegexProvider):
def __init__(self, regex, urlconf_name, default_kwargs=None, app_name=None, namespace=None):
LocaleRegexProvider.__init__(self, regex)
- # urlconf_name is a string representing the module containing URLconfs.
+ # urlconf_name is the dotted Python path to the module defining
+ # urlpatterns. It may also be an object with an urlpatterns attribute
+ # or urlpatterns itself.
self.urlconf_name = urlconf_name
- if not isinstance(urlconf_name, six.string_types):
- self._urlconf_module = self.urlconf_name
self.callback = None
self.default_kwargs = default_kwargs or {}
self.namespace = namespace
@@ -389,15 +389,14 @@ class RegexURLResolver(LocaleRegexProvider):
raise Resolver404({'tried': tried, 'path': new_path})
raise Resolver404({'path': path})
- @property
+ @cached_property
def urlconf_module(self):
- try:
- return self._urlconf_module
- except AttributeError:
- self._urlconf_module = import_module(self.urlconf_name)
- return self._urlconf_module
+ if isinstance(self.urlconf_name, six.string_types):
+ return import_module(self.urlconf_name)
+ else:
+ return self.urlconf_name
- @property
+ @cached_property
def url_patterns(self):
# urlconf_module might be a valid set of patterns, so we default to it
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)