2015-12-30 23:51:16 +08:00
|
|
|
"""
|
|
|
|
This module converts requested URLs to callback view functions.
|
|
|
|
|
|
|
|
RegexURLResolver is the main class here. Its resolve() method takes a URL (as
|
|
|
|
a string) and returns a ResolverMatch object which provides access to all
|
|
|
|
attributes of the resolved URL match.
|
|
|
|
"""
|
|
|
|
import functools
|
|
|
|
import re
|
2016-07-15 02:41:52 +08:00
|
|
|
import threading
|
2015-12-30 23:51:16 +08:00
|
|
|
from importlib import import_module
|
2017-01-26 21:25:15 +08:00
|
|
|
from urllib.parse import quote
|
2015-12-30 23:51:16 +08:00
|
|
|
|
2016-03-06 03:20:40 +08:00
|
|
|
from django.conf import settings
|
2016-10-06 03:34:26 +08:00
|
|
|
from django.core.checks import Warning
|
|
|
|
from django.core.checks.urls import check_resolver
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
from django.utils.datastructures import MultiValueDict
|
|
|
|
from django.utils.functional import cached_property
|
2017-01-26 21:25:15 +08:00
|
|
|
from django.utils.http import RFC3986_SUBDELIMS
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.utils.regex_helper import normalize
|
|
|
|
from django.utils.translation import get_language
|
|
|
|
|
|
|
|
from .exceptions import NoReverseMatch, Resolver404
|
|
|
|
from .utils import get_callable
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class ResolverMatch:
|
2015-12-30 23:51:16 +08:00
|
|
|
def __init__(self, func, args, kwargs, url_name=None, app_names=None, namespaces=None):
|
|
|
|
self.func = func
|
|
|
|
self.args = args
|
|
|
|
self.kwargs = kwargs
|
|
|
|
self.url_name = url_name
|
|
|
|
|
|
|
|
# If a URLRegexResolver doesn't have a namespace or app_name, it passes
|
|
|
|
# in an empty value.
|
|
|
|
self.app_names = [x for x in app_names if x] if app_names else []
|
|
|
|
self.app_name = ':'.join(self.app_names)
|
|
|
|
self.namespaces = [x for x in namespaces if x] if namespaces else []
|
|
|
|
self.namespace = ':'.join(self.namespaces)
|
|
|
|
|
|
|
|
if not hasattr(func, '__name__'):
|
|
|
|
# A class-based view
|
2017-05-21 05:16:36 +08:00
|
|
|
self._func_path = func.__class__.__module__ + '.' + func.__class__.__name__
|
2015-12-30 23:51:16 +08:00
|
|
|
else:
|
|
|
|
# A function-based view
|
2017-05-21 05:16:36 +08:00
|
|
|
self._func_path = func.__module__ + '.' + func.__name__
|
2015-12-30 23:51:16 +08:00
|
|
|
|
|
|
|
view_path = url_name or self._func_path
|
|
|
|
self.view_name = ':'.join(self.namespaces + [view_path])
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
return (self.func, self.args, self.kwargs)[index]
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "ResolverMatch(func=%s, args=%s, kwargs=%s, url_name=%s, app_names=%s, namespaces=%s)" % (
|
|
|
|
self._func_path, self.args, self.kwargs, self.url_name,
|
|
|
|
self.app_names, self.namespaces,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-01-19 04:30:21 +08:00
|
|
|
@functools.lru_cache(maxsize=None)
|
2015-12-30 23:51:16 +08:00
|
|
|
def get_resolver(urlconf=None):
|
|
|
|
if urlconf is None:
|
|
|
|
from django.conf import settings
|
|
|
|
urlconf = settings.ROOT_URLCONF
|
|
|
|
return RegexURLResolver(r'^/', urlconf)
|
|
|
|
|
|
|
|
|
2017-01-19 04:30:21 +08:00
|
|
|
@functools.lru_cache(maxsize=None)
|
2015-12-30 23:51:16 +08:00
|
|
|
def get_ns_resolver(ns_pattern, resolver):
|
|
|
|
# Build a namespaced resolver for the given parent URLconf pattern.
|
|
|
|
# This makes it possible to have captured parameters in the parent
|
|
|
|
# URLconf pattern.
|
|
|
|
ns_resolver = RegexURLResolver(ns_pattern, resolver.url_patterns)
|
|
|
|
return RegexURLResolver(r'^/', [ns_resolver])
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class LocaleRegexDescriptor:
|
2016-11-06 20:52:07 +08:00
|
|
|
def __get__(self, instance, cls=None):
|
|
|
|
"""
|
|
|
|
Return a compiled regular expression based on the active language.
|
|
|
|
"""
|
|
|
|
if instance is None:
|
|
|
|
return self
|
|
|
|
# As a performance optimization, if the given regex string is a regular
|
|
|
|
# string (not a lazily-translated string proxy), compile it once and
|
|
|
|
# avoid per-language compilation.
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(instance._regex, str):
|
2016-11-06 20:52:07 +08:00
|
|
|
instance.__dict__['regex'] = self._compile(instance._regex)
|
|
|
|
return instance.__dict__['regex']
|
|
|
|
language_code = get_language()
|
|
|
|
if language_code not in instance._regex_dict:
|
2017-04-22 01:52:26 +08:00
|
|
|
instance._regex_dict[language_code] = self._compile(str(instance._regex))
|
2016-11-06 20:52:07 +08:00
|
|
|
return instance._regex_dict[language_code]
|
|
|
|
|
|
|
|
def _compile(self, regex):
|
|
|
|
"""
|
|
|
|
Compile and return the given regular expression.
|
|
|
|
"""
|
|
|
|
try:
|
2017-01-19 05:52:25 +08:00
|
|
|
return re.compile(regex)
|
2016-11-06 20:52:07 +08:00
|
|
|
except re.error as e:
|
|
|
|
raise ImproperlyConfigured(
|
2016-12-29 23:27:49 +08:00
|
|
|
'"%s" is not a valid regular expression: %s' % (regex, e)
|
2016-11-06 20:52:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class LocaleRegexProvider:
|
2015-12-30 23:51:16 +08:00
|
|
|
"""
|
|
|
|
A mixin to provide a default regex property which can vary by active
|
|
|
|
language.
|
|
|
|
"""
|
|
|
|
def __init__(self, regex):
|
|
|
|
# regex is either a string representing a regular expression, or a
|
2017-01-27 03:58:33 +08:00
|
|
|
# translatable string (using gettext_lazy) representing a regular
|
2015-12-30 23:51:16 +08:00
|
|
|
# expression.
|
|
|
|
self._regex = regex
|
|
|
|
self._regex_dict = {}
|
|
|
|
|
2016-11-06 20:52:07 +08:00
|
|
|
regex = LocaleRegexDescriptor()
|
2015-12-30 23:51:16 +08:00
|
|
|
|
2016-10-06 03:34:26 +08:00
|
|
|
def describe(self):
|
|
|
|
"""
|
|
|
|
Format the URL pattern for display in warning messages.
|
|
|
|
"""
|
|
|
|
description = "'{}'".format(self.regex.pattern)
|
|
|
|
if getattr(self, 'name', False):
|
|
|
|
description += " [name='{}']".format(self.name)
|
|
|
|
return description
|
|
|
|
|
|
|
|
def _check_pattern_startswith_slash(self):
|
|
|
|
"""
|
|
|
|
Check that the pattern does not begin with a forward slash.
|
|
|
|
"""
|
|
|
|
regex_pattern = self.regex.pattern
|
|
|
|
if not settings.APPEND_SLASH:
|
|
|
|
# Skip check as it can be useful to start a URL pattern with a slash
|
|
|
|
# when APPEND_SLASH=False.
|
|
|
|
return []
|
|
|
|
if (regex_pattern.startswith('/') or regex_pattern.startswith('^/')) and not regex_pattern.endswith('/'):
|
|
|
|
warning = Warning(
|
|
|
|
"Your URL pattern {} has a regex beginning with a '/'. Remove this "
|
|
|
|
"slash as it is unnecessary. If this pattern is targeted in an "
|
|
|
|
"include(), ensure the include() pattern has a trailing '/'.".format(
|
|
|
|
self.describe()
|
|
|
|
),
|
|
|
|
id="urls.W002",
|
|
|
|
)
|
|
|
|
return [warning]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-12-30 23:51:16 +08:00
|
|
|
|
|
|
|
class RegexURLPattern(LocaleRegexProvider):
|
|
|
|
def __init__(self, regex, callback, default_args=None, name=None):
|
|
|
|
LocaleRegexProvider.__init__(self, regex)
|
|
|
|
self.callback = callback # the view
|
|
|
|
self.default_args = default_args or {}
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def __repr__(self):
|
2017-01-12 06:17:25 +08:00
|
|
|
return '<%s %s %s>' % (self.__class__.__name__, self.name, self.regex.pattern)
|
2015-12-30 23:51:16 +08:00
|
|
|
|
2016-10-06 03:34:26 +08:00
|
|
|
def check(self):
|
|
|
|
warnings = self._check_pattern_name()
|
|
|
|
if not warnings:
|
|
|
|
warnings = self._check_pattern_startswith_slash()
|
|
|
|
return warnings
|
|
|
|
|
|
|
|
def _check_pattern_name(self):
|
|
|
|
"""
|
|
|
|
Check that the pattern name does not contain a colon.
|
|
|
|
"""
|
|
|
|
if self.name is not None and ":" in self.name:
|
|
|
|
warning = Warning(
|
|
|
|
"Your URL pattern {} has a name including a ':'. Remove the colon, to "
|
|
|
|
"avoid ambiguous namespace references.".format(self.describe()),
|
|
|
|
id="urls.W003",
|
|
|
|
)
|
|
|
|
return [warning]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-12-30 23:51:16 +08:00
|
|
|
def resolve(self, path):
|
|
|
|
match = self.regex.search(path)
|
|
|
|
if match:
|
|
|
|
# If there are any named groups, use those as kwargs, ignoring
|
|
|
|
# non-named groups. Otherwise, pass all non-named arguments as
|
|
|
|
# positional arguments.
|
|
|
|
kwargs = match.groupdict()
|
|
|
|
args = () if kwargs else match.groups()
|
|
|
|
# In both cases, pass any extra_kwargs as **kwargs.
|
|
|
|
kwargs.update(self.default_args)
|
|
|
|
return ResolverMatch(self.callback, args, kwargs, self.name)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def lookup_str(self):
|
|
|
|
"""
|
|
|
|
A string that identifies the view (e.g. 'path.to.view_function' or
|
|
|
|
'path.to.ClassBasedView').
|
|
|
|
"""
|
|
|
|
callback = self.callback
|
2016-01-06 05:47:48 +08:00
|
|
|
# Python 3.5 collapses nested partials, so can change "while" to "if"
|
|
|
|
# when it's the minimum supported version.
|
|
|
|
while isinstance(callback, functools.partial):
|
2015-12-30 23:51:16 +08:00
|
|
|
callback = callback.func
|
|
|
|
if not hasattr(callback, '__name__'):
|
|
|
|
return callback.__module__ + "." + callback.__class__.__name__
|
2016-12-01 18:38:01 +08:00
|
|
|
return callback.__module__ + "." + callback.__qualname__
|
2015-12-30 23:51:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
class RegexURLResolver(LocaleRegexProvider):
|
|
|
|
def __init__(self, regex, urlconf_name, default_kwargs=None, app_name=None, namespace=None):
|
|
|
|
LocaleRegexProvider.__init__(self, regex)
|
|
|
|
# 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.callback = None
|
|
|
|
self.default_kwargs = default_kwargs or {}
|
|
|
|
self.namespace = namespace
|
|
|
|
self.app_name = app_name
|
|
|
|
self._reverse_dict = {}
|
|
|
|
self._namespace_dict = {}
|
|
|
|
self._app_dict = {}
|
|
|
|
# set of dotted paths to all functions and classes that are used in
|
|
|
|
# urlpatterns
|
|
|
|
self._callback_strs = set()
|
|
|
|
self._populated = False
|
2016-07-15 02:41:52 +08:00
|
|
|
self._local = threading.local()
|
2015-12-30 23:51:16 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
if isinstance(self.urlconf_name, list) and len(self.urlconf_name):
|
|
|
|
# Don't bother to output the whole list, it can be huge
|
|
|
|
urlconf_repr = '<%s list>' % self.urlconf_name[0].__class__.__name__
|
|
|
|
else:
|
|
|
|
urlconf_repr = repr(self.urlconf_name)
|
2017-01-20 17:20:53 +08:00
|
|
|
return '<%s %s (%s:%s) %s>' % (
|
2015-12-30 23:51:16 +08:00
|
|
|
self.__class__.__name__, urlconf_repr, self.app_name,
|
|
|
|
self.namespace, self.regex.pattern,
|
|
|
|
)
|
|
|
|
|
2016-10-06 03:34:26 +08:00
|
|
|
def check(self):
|
|
|
|
warnings = self._check_include_trailing_dollar()
|
|
|
|
for pattern in self.url_patterns:
|
|
|
|
warnings.extend(check_resolver(pattern))
|
|
|
|
if not warnings:
|
|
|
|
warnings = self._check_pattern_startswith_slash()
|
|
|
|
return warnings
|
|
|
|
|
|
|
|
def _check_include_trailing_dollar(self):
|
|
|
|
"""
|
|
|
|
Check that include is not used with a regex ending with a dollar.
|
|
|
|
"""
|
|
|
|
regex_pattern = self.regex.pattern
|
|
|
|
if regex_pattern.endswith('$') and not regex_pattern.endswith(r'\$'):
|
|
|
|
warning = Warning(
|
|
|
|
"Your URL pattern {} uses include with a regex ending with a '$'. "
|
|
|
|
"Remove the dollar from the regex to avoid problems including "
|
|
|
|
"URLs.".format(self.describe()),
|
|
|
|
id="urls.W001",
|
|
|
|
)
|
|
|
|
return [warning]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2015-12-30 23:51:16 +08:00
|
|
|
def _populate(self):
|
2016-07-15 02:41:52 +08:00
|
|
|
# Short-circuit if called recursively in this thread to prevent
|
|
|
|
# infinite recursion. Concurrent threads may call this at the same
|
|
|
|
# time and will need to continue, so set 'populating' on a
|
|
|
|
# thread-local variable.
|
|
|
|
if getattr(self._local, 'populating', False):
|
2016-06-18 22:39:32 +08:00
|
|
|
return
|
2016-07-15 02:41:52 +08:00
|
|
|
self._local.populating = True
|
2015-12-30 23:51:16 +08:00
|
|
|
lookups = MultiValueDict()
|
|
|
|
namespaces = {}
|
|
|
|
apps = {}
|
|
|
|
language_code = get_language()
|
|
|
|
for pattern in reversed(self.url_patterns):
|
|
|
|
if isinstance(pattern, RegexURLPattern):
|
|
|
|
self._callback_strs.add(pattern.lookup_str)
|
|
|
|
p_pattern = pattern.regex.pattern
|
|
|
|
if p_pattern.startswith('^'):
|
|
|
|
p_pattern = p_pattern[1:]
|
|
|
|
if isinstance(pattern, RegexURLResolver):
|
|
|
|
if pattern.namespace:
|
|
|
|
namespaces[pattern.namespace] = (p_pattern, pattern)
|
|
|
|
if pattern.app_name:
|
|
|
|
apps.setdefault(pattern.app_name, []).append(pattern.namespace)
|
|
|
|
else:
|
|
|
|
parent_pat = pattern.regex.pattern
|
|
|
|
for name in pattern.reverse_dict:
|
|
|
|
for matches, pat, defaults in pattern.reverse_dict.getlist(name):
|
|
|
|
new_matches = normalize(parent_pat + pat)
|
|
|
|
lookups.appendlist(
|
|
|
|
name,
|
|
|
|
(
|
|
|
|
new_matches,
|
|
|
|
p_pattern + pat,
|
|
|
|
dict(defaults, **pattern.default_kwargs),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
for namespace, (prefix, sub_pattern) in pattern.namespace_dict.items():
|
|
|
|
namespaces[namespace] = (p_pattern + prefix, sub_pattern)
|
|
|
|
for app_name, namespace_list in pattern.app_dict.items():
|
|
|
|
apps.setdefault(app_name, []).extend(namespace_list)
|
2016-07-15 02:41:52 +08:00
|
|
|
if not getattr(pattern._local, 'populating', False):
|
2016-06-18 22:39:32 +08:00
|
|
|
pattern._populate()
|
|
|
|
self._callback_strs.update(pattern._callback_strs)
|
2015-12-30 23:51:16 +08:00
|
|
|
else:
|
|
|
|
bits = normalize(p_pattern)
|
|
|
|
lookups.appendlist(pattern.callback, (bits, p_pattern, pattern.default_args))
|
|
|
|
if pattern.name is not None:
|
|
|
|
lookups.appendlist(pattern.name, (bits, p_pattern, pattern.default_args))
|
|
|
|
self._reverse_dict[language_code] = lookups
|
|
|
|
self._namespace_dict[language_code] = namespaces
|
|
|
|
self._app_dict[language_code] = apps
|
|
|
|
self._populated = True
|
2016-07-15 02:41:52 +08:00
|
|
|
self._local.populating = False
|
2015-12-30 23:51:16 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def reverse_dict(self):
|
|
|
|
language_code = get_language()
|
|
|
|
if language_code not in self._reverse_dict:
|
|
|
|
self._populate()
|
|
|
|
return self._reverse_dict[language_code]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def namespace_dict(self):
|
|
|
|
language_code = get_language()
|
|
|
|
if language_code not in self._namespace_dict:
|
|
|
|
self._populate()
|
|
|
|
return self._namespace_dict[language_code]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def app_dict(self):
|
|
|
|
language_code = get_language()
|
|
|
|
if language_code not in self._app_dict:
|
|
|
|
self._populate()
|
|
|
|
return self._app_dict[language_code]
|
|
|
|
|
|
|
|
def _is_callback(self, name):
|
|
|
|
if not self._populated:
|
|
|
|
self._populate()
|
|
|
|
return name in self._callback_strs
|
|
|
|
|
|
|
|
def resolve(self, path):
|
2017-04-22 01:52:26 +08:00
|
|
|
path = str(path) # path may be a reverse_lazy object
|
2015-12-30 23:51:16 +08:00
|
|
|
tried = []
|
|
|
|
match = self.regex.search(path)
|
|
|
|
if match:
|
|
|
|
new_path = path[match.end():]
|
|
|
|
for pattern in self.url_patterns:
|
|
|
|
try:
|
|
|
|
sub_match = pattern.resolve(new_path)
|
|
|
|
except Resolver404 as e:
|
|
|
|
sub_tried = e.args[0].get('tried')
|
|
|
|
if sub_tried is not None:
|
|
|
|
tried.extend([pattern] + t for t in sub_tried)
|
|
|
|
else:
|
|
|
|
tried.append([pattern])
|
|
|
|
else:
|
|
|
|
if sub_match:
|
|
|
|
# Merge captured arguments in match with submatch
|
|
|
|
sub_match_dict = dict(match.groupdict(), **self.default_kwargs)
|
|
|
|
sub_match_dict.update(sub_match.kwargs)
|
|
|
|
|
|
|
|
# If there are *any* named groups, ignore all non-named groups.
|
|
|
|
# Otherwise, pass all non-named arguments as positional arguments.
|
|
|
|
sub_match_args = sub_match.args
|
|
|
|
if not sub_match_dict:
|
|
|
|
sub_match_args = match.groups() + sub_match.args
|
|
|
|
|
|
|
|
return ResolverMatch(
|
|
|
|
sub_match.func,
|
|
|
|
sub_match_args,
|
|
|
|
sub_match_dict,
|
|
|
|
sub_match.url_name,
|
|
|
|
[self.app_name] + sub_match.app_names,
|
|
|
|
[self.namespace] + sub_match.namespaces,
|
|
|
|
)
|
|
|
|
tried.append([pattern])
|
|
|
|
raise Resolver404({'tried': tried, 'path': new_path})
|
|
|
|
raise Resolver404({'path': path})
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def urlconf_module(self):
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(self.urlconf_name, str):
|
2015-12-30 23:51:16 +08:00
|
|
|
return import_module(self.urlconf_name)
|
|
|
|
else:
|
|
|
|
return self.urlconf_name
|
|
|
|
|
|
|
|
@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)
|
|
|
|
try:
|
|
|
|
iter(patterns)
|
|
|
|
except TypeError:
|
|
|
|
msg = (
|
|
|
|
"The included URLconf '{name}' does not appear to have any "
|
|
|
|
"patterns in it. If you see valid patterns in the file then "
|
|
|
|
"the issue is probably caused by a circular import."
|
|
|
|
)
|
|
|
|
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
|
|
|
|
return patterns
|
|
|
|
|
|
|
|
def resolve_error_handler(self, view_type):
|
|
|
|
callback = getattr(self.urlconf_module, 'handler%s' % view_type, None)
|
|
|
|
if not callback:
|
|
|
|
# No handler specified in file; use lazy import, since
|
|
|
|
# django.conf.urls imports this file.
|
|
|
|
from django.conf import urls
|
|
|
|
callback = getattr(urls, 'handler%s' % view_type)
|
|
|
|
return get_callable(callback), {}
|
|
|
|
|
2016-06-21 01:24:51 +08:00
|
|
|
def reverse(self, lookup_view, *args, **kwargs):
|
|
|
|
return self._reverse_with_prefix(lookup_view, '', *args, **kwargs)
|
|
|
|
|
2015-12-30 23:51:16 +08:00
|
|
|
def _reverse_with_prefix(self, lookup_view, _prefix, *args, **kwargs):
|
|
|
|
if args and kwargs:
|
|
|
|
raise ValueError("Don't mix *args and **kwargs in call to reverse()!")
|
2017-04-22 01:52:26 +08:00
|
|
|
text_args = [str(v) for v in args]
|
|
|
|
text_kwargs = {k: str(v) for (k, v) in kwargs.items()}
|
2015-12-30 23:51:16 +08:00
|
|
|
|
|
|
|
if not self._populated:
|
|
|
|
self._populate()
|
|
|
|
|
|
|
|
possibilities = self.reverse_dict.getlist(lookup_view)
|
|
|
|
|
|
|
|
for possibility, pattern, defaults in possibilities:
|
|
|
|
for result, params in possibility:
|
|
|
|
if args:
|
|
|
|
if len(args) != len(params):
|
|
|
|
continue
|
|
|
|
candidate_subs = dict(zip(params, text_args))
|
|
|
|
else:
|
2017-05-28 07:08:46 +08:00
|
|
|
if set(kwargs) | set(defaults) != set(params) | set(defaults):
|
2015-12-30 23:51:16 +08:00
|
|
|
continue
|
|
|
|
matches = True
|
|
|
|
for k, v in defaults.items():
|
|
|
|
if kwargs.get(k, v) != v:
|
|
|
|
matches = False
|
|
|
|
break
|
|
|
|
if not matches:
|
|
|
|
continue
|
|
|
|
candidate_subs = text_kwargs
|
|
|
|
# WSGI provides decoded URLs, without %xx escapes, and the URL
|
|
|
|
# resolver operates on such URLs. First substitute arguments
|
|
|
|
# without quoting to build a decoded URL and look for a match.
|
|
|
|
# Then, if we have a match, redo the substitution with quoted
|
|
|
|
# arguments in order to return a properly encoded URL.
|
|
|
|
candidate_pat = _prefix.replace('%', '%%') + result
|
2017-01-19 05:52:25 +08:00
|
|
|
if re.search('^%s%s' % (re.escape(_prefix), pattern), candidate_pat % candidate_subs):
|
2015-12-30 23:51:16 +08:00
|
|
|
# safe characters from `pchar` definition of RFC 3986
|
2017-01-26 21:25:15 +08:00
|
|
|
url = quote(candidate_pat % candidate_subs, safe=RFC3986_SUBDELIMS + '/~:@')
|
2015-12-30 23:51:16 +08:00
|
|
|
# Don't allow construction of scheme relative urls.
|
|
|
|
if url.startswith('//'):
|
|
|
|
url = '/%%2F%s' % url[2:]
|
|
|
|
return url
|
|
|
|
# lookup_view can be URL name or callable, but callables are not
|
|
|
|
# friendly in error messages.
|
|
|
|
m = getattr(lookup_view, '__module__', None)
|
|
|
|
n = getattr(lookup_view, '__name__', None)
|
|
|
|
if m is not None and n is not None:
|
|
|
|
lookup_view_s = "%s.%s" % (m, n)
|
|
|
|
else:
|
|
|
|
lookup_view_s = lookup_view
|
|
|
|
|
|
|
|
patterns = [pattern for (possibility, pattern, defaults) in possibilities]
|
2017-01-12 10:40:18 +08:00
|
|
|
if patterns:
|
|
|
|
if args:
|
|
|
|
arg_msg = "arguments '%s'" % (args,)
|
|
|
|
elif kwargs:
|
|
|
|
arg_msg = "keyword arguments '%s'" % (kwargs,)
|
|
|
|
else:
|
|
|
|
arg_msg = "no arguments"
|
|
|
|
msg = (
|
|
|
|
"Reverse for '%s' with %s not found. %d pattern(s) tried: %s" %
|
|
|
|
(lookup_view_s, arg_msg, len(patterns), patterns)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
msg = (
|
|
|
|
"Reverse for '%(view)s' not found. '%(view)s' is not "
|
|
|
|
"a valid view function or pattern name." % {'view': lookup_view_s}
|
|
|
|
)
|
|
|
|
raise NoReverseMatch(msg)
|
2015-12-30 23:51:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
class LocaleRegexURLResolver(RegexURLResolver):
|
|
|
|
"""
|
|
|
|
A URL resolver that always matches the active language code as URL prefix.
|
|
|
|
|
|
|
|
Rather than taking a regex argument, we just override the ``regex``
|
|
|
|
function to always return the active language-code as regex.
|
|
|
|
"""
|
2016-02-29 16:24:19 +08:00
|
|
|
def __init__(
|
|
|
|
self, urlconf_name, default_kwargs=None, app_name=None, namespace=None,
|
|
|
|
prefix_default_language=True,
|
|
|
|
):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(None, urlconf_name, default_kwargs, app_name, namespace)
|
2016-02-29 16:24:19 +08:00
|
|
|
self.prefix_default_language = prefix_default_language
|
2015-12-30 23:51:16 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def regex(self):
|
2016-03-06 03:20:40 +08:00
|
|
|
language_code = get_language() or settings.LANGUAGE_CODE
|
2015-12-30 23:51:16 +08:00
|
|
|
if language_code not in self._regex_dict:
|
2016-02-29 16:24:19 +08:00
|
|
|
if language_code == settings.LANGUAGE_CODE and not self.prefix_default_language:
|
|
|
|
regex_string = ''
|
|
|
|
else:
|
|
|
|
regex_string = '^%s/' % language_code
|
2017-01-19 05:52:25 +08:00
|
|
|
self._regex_dict[language_code] = re.compile(regex_string)
|
2015-12-30 23:51:16 +08:00
|
|
|
return self._regex_dict[language_code]
|