Simplified handling of RegexURLResolver.urlconf_module.

Also made RegexURLResolver.url_patterns a cached property for
consistency and efficiency.
This commit is contained in:
Aymeric Augustin 2015-02-05 12:14:36 +01:00
parent 737b184d91
commit de62b8ef45
1 changed files with 10 additions and 11 deletions

View File

@ -19,7 +19,7 @@ from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
from django.utils.datastructures import MultiValueDict from django.utils.datastructures import MultiValueDict
from django.utils.deprecation import RemovedInDjango20Warning from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.encoding import force_str, force_text, iri_to_uri 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.http import RFC3986_SUBDELIMS, urlquote
from django.utils.module_loading import module_has_submodule from django.utils.module_loading import module_has_submodule
from django.utils.regex_helper import normalize from django.utils.regex_helper import normalize
@ -252,10 +252,10 @@ class RegexURLPattern(LocaleRegexProvider):
class RegexURLResolver(LocaleRegexProvider): class RegexURLResolver(LocaleRegexProvider):
def __init__(self, regex, urlconf_name, default_kwargs=None, app_name=None, namespace=None): def __init__(self, regex, urlconf_name, default_kwargs=None, app_name=None, namespace=None):
LocaleRegexProvider.__init__(self, regex) 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 self.urlconf_name = urlconf_name
if not isinstance(urlconf_name, six.string_types):
self._urlconf_module = self.urlconf_name
self.callback = None self.callback = None
self.default_kwargs = default_kwargs or {} self.default_kwargs = default_kwargs or {}
self.namespace = namespace self.namespace = namespace
@ -389,15 +389,14 @@ class RegexURLResolver(LocaleRegexProvider):
raise Resolver404({'tried': tried, 'path': new_path}) raise Resolver404({'tried': tried, 'path': new_path})
raise Resolver404({'path': path}) raise Resolver404({'path': path})
@property @cached_property
def urlconf_module(self): def urlconf_module(self):
try: if isinstance(self.urlconf_name, six.string_types):
return self._urlconf_module return import_module(self.urlconf_name)
except AttributeError: else:
self._urlconf_module = import_module(self.urlconf_name) return self.urlconf_name
return self._urlconf_module
@property @cached_property
def url_patterns(self): def url_patterns(self):
# urlconf_module might be a valid set of patterns, so we default to it # urlconf_module might be a valid set of patterns, so we default to it
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)