2021-01-13 05:19:53 +08:00
|
|
|
from urllib.parse import unquote, urlsplit, urlunsplit
|
2015-12-30 23:51:16 +08:00
|
|
|
|
2019-04-12 21:15:18 +08:00
|
|
|
from asgiref.local import Local
|
|
|
|
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.utils.functional import lazy
|
|
|
|
from django.utils.translation import override
|
|
|
|
|
|
|
|
from .exceptions import NoReverseMatch, Resolver404
|
2019-05-07 04:53:40 +08:00
|
|
|
from .resolvers import _get_cached_resolver, get_ns_resolver, get_resolver
|
2015-12-30 23:51:16 +08:00
|
|
|
from .utils import get_callable
|
|
|
|
|
|
|
|
# SCRIPT_NAME prefixes for each thread are stored here. If there's no entry for
|
|
|
|
# the current thread (which is the only one we ever access), it is assumed to
|
|
|
|
# be empty.
|
2019-04-12 21:15:18 +08:00
|
|
|
_prefixes = Local()
|
2015-12-30 23:51:16 +08:00
|
|
|
|
|
|
|
# Overridden URLconfs for each thread are stored here.
|
2019-04-12 21:15:18 +08:00
|
|
|
_urlconfs = Local()
|
2015-12-30 23:51:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
def resolve(path, urlconf=None):
|
|
|
|
if urlconf is None:
|
|
|
|
urlconf = get_urlconf()
|
|
|
|
return get_resolver(urlconf).resolve(path)
|
|
|
|
|
|
|
|
|
|
|
|
def reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None):
|
|
|
|
if urlconf is None:
|
|
|
|
urlconf = get_urlconf()
|
|
|
|
resolver = get_resolver(urlconf)
|
|
|
|
args = args or []
|
|
|
|
kwargs = kwargs or {}
|
|
|
|
|
|
|
|
prefix = get_script_prefix()
|
|
|
|
|
2016-12-29 23:27:49 +08:00
|
|
|
if not isinstance(viewname, str):
|
2015-12-30 23:51:16 +08:00
|
|
|
view = viewname
|
|
|
|
else:
|
2019-06-12 10:39:39 +08:00
|
|
|
*path, view = viewname.split(':')
|
2015-12-30 23:51:16 +08:00
|
|
|
|
|
|
|
if current_app:
|
|
|
|
current_path = current_app.split(':')
|
|
|
|
current_path.reverse()
|
|
|
|
else:
|
|
|
|
current_path = None
|
|
|
|
|
|
|
|
resolved_path = []
|
|
|
|
ns_pattern = ''
|
2018-05-27 04:56:17 +08:00
|
|
|
ns_converters = {}
|
2019-06-12 10:39:39 +08:00
|
|
|
for ns in path:
|
2015-12-30 23:51:16 +08:00
|
|
|
current_ns = current_path.pop() if current_path else None
|
|
|
|
# Lookup the name to see if it could be an app identifier.
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2015-12-30 23:51:16 +08:00
|
|
|
app_list = resolver.app_dict[ns]
|
|
|
|
# Yes! Path part matches an app in the current Resolver.
|
|
|
|
if current_ns and current_ns in app_list:
|
|
|
|
# If we are reversing for a particular app, use that
|
|
|
|
# namespace.
|
|
|
|
ns = current_ns
|
|
|
|
elif ns not in app_list:
|
|
|
|
# The name isn't shared by one of the instances (i.e.,
|
|
|
|
# the default) so pick the first instance as the default.
|
|
|
|
ns = app_list[0]
|
2017-09-07 20:16:21 +08:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2015-12-30 23:51:16 +08:00
|
|
|
|
|
|
|
if ns != current_ns:
|
|
|
|
current_path = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
extra, resolver = resolver.namespace_dict[ns]
|
|
|
|
resolved_path.append(ns)
|
|
|
|
ns_pattern = ns_pattern + extra
|
2018-05-27 04:56:17 +08:00
|
|
|
ns_converters.update(resolver.pattern.converters)
|
2015-12-30 23:51:16 +08:00
|
|
|
except KeyError as key:
|
|
|
|
if resolved_path:
|
|
|
|
raise NoReverseMatch(
|
|
|
|
"%s is not a registered namespace inside '%s'" %
|
|
|
|
(key, ':'.join(resolved_path))
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise NoReverseMatch("%s is not a registered namespace" % key)
|
|
|
|
if ns_pattern:
|
2018-05-27 04:56:17 +08:00
|
|
|
resolver = get_ns_resolver(ns_pattern, resolver, tuple(ns_converters.items()))
|
2015-12-30 23:51:16 +08:00
|
|
|
|
2020-10-04 20:58:54 +08:00
|
|
|
return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
|
2015-12-30 23:51:16 +08:00
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2016-12-29 23:27:49 +08:00
|
|
|
reverse_lazy = lazy(reverse, str)
|
2015-12-30 23:51:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
def clear_url_caches():
|
|
|
|
get_callable.cache_clear()
|
2019-05-07 04:53:40 +08:00
|
|
|
_get_cached_resolver.cache_clear()
|
2015-12-30 23:51:16 +08:00
|
|
|
get_ns_resolver.cache_clear()
|
|
|
|
|
|
|
|
|
|
|
|
def set_script_prefix(prefix):
|
|
|
|
"""
|
|
|
|
Set the script prefix for the current thread.
|
|
|
|
"""
|
|
|
|
if not prefix.endswith('/'):
|
|
|
|
prefix += '/'
|
|
|
|
_prefixes.value = prefix
|
|
|
|
|
|
|
|
|
|
|
|
def get_script_prefix():
|
|
|
|
"""
|
|
|
|
Return the currently active script prefix. Useful for client code that
|
|
|
|
wishes to construct their own URLs manually (although accessing the request
|
|
|
|
instance is normally going to be a lot cleaner).
|
|
|
|
"""
|
|
|
|
return getattr(_prefixes, "value", '/')
|
|
|
|
|
|
|
|
|
|
|
|
def clear_script_prefix():
|
|
|
|
"""
|
|
|
|
Unset the script prefix for the current thread.
|
|
|
|
"""
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2015-12-30 23:51:16 +08:00
|
|
|
del _prefixes.value
|
2017-09-07 20:16:21 +08:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
2015-12-30 23:51:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
def set_urlconf(urlconf_name):
|
|
|
|
"""
|
|
|
|
Set the URLconf for the current thread (overriding the default one in
|
|
|
|
settings). If urlconf_name is None, revert back to the default.
|
|
|
|
"""
|
|
|
|
if urlconf_name:
|
|
|
|
_urlconfs.value = urlconf_name
|
|
|
|
else:
|
|
|
|
if hasattr(_urlconfs, "value"):
|
|
|
|
del _urlconfs.value
|
|
|
|
|
|
|
|
|
|
|
|
def get_urlconf(default=None):
|
|
|
|
"""
|
|
|
|
Return the root URLconf to use for the current thread if it has been
|
|
|
|
changed from the default one.
|
|
|
|
"""
|
|
|
|
return getattr(_urlconfs, "value", default)
|
|
|
|
|
|
|
|
|
|
|
|
def is_valid_path(path, urlconf=None):
|
|
|
|
"""
|
2020-10-20 15:14:48 +08:00
|
|
|
Return the ResolverMatch if the given path resolves against the default URL
|
|
|
|
resolver, False otherwise. This is a convenience method to make working
|
|
|
|
with "is this a match?" cases easier, avoiding try...except blocks.
|
2015-12-30 23:51:16 +08:00
|
|
|
"""
|
|
|
|
try:
|
2020-10-20 15:14:48 +08:00
|
|
|
return resolve(path, urlconf)
|
2015-12-30 23:51:16 +08:00
|
|
|
except Resolver404:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def translate_url(url, lang_code):
|
|
|
|
"""
|
|
|
|
Given a URL (absolute or relative), try to get its translated version in
|
|
|
|
the `lang_code` language (either by i18n_patterns or by translated regex).
|
|
|
|
Return the original URL if no translated version is found.
|
|
|
|
"""
|
|
|
|
parsed = urlsplit(url)
|
|
|
|
try:
|
2021-01-13 05:19:53 +08:00
|
|
|
# URL may be encoded.
|
|
|
|
match = resolve(unquote(parsed.path))
|
2015-12-30 23:51:16 +08:00
|
|
|
except Resolver404:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
to_be_reversed = "%s:%s" % (match.namespace, match.url_name) if match.namespace else match.url_name
|
|
|
|
with override(lang_code):
|
|
|
|
try:
|
|
|
|
url = reverse(to_be_reversed, args=match.args, kwargs=match.kwargs)
|
|
|
|
except NoReverseMatch:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
url = urlunsplit((parsed.scheme, parsed.netloc, url, parsed.query, parsed.fragment))
|
|
|
|
return url
|