Created
February 7, 2019 14:32
-
-
Save ryu22e/0fc8ca0fcf9ee83d5c4aa51efef38de9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
if not isinstance(viewname, str): | |
view = viewname | |
else: | |
parts = viewname.split(':') | |
parts.reverse() | |
view = parts[0] | |
path = parts[1:] | |
if current_app: | |
current_path = current_app.split(':') | |
current_path.reverse() | |
else: | |
current_path = None | |
resolved_path = [] | |
ns_pattern = '' | |
ns_converters = {} | |
while path: | |
ns = path.pop() | |
current_ns = current_path.pop() if current_path else None | |
# Lookup the name to see if it could be an app identifier. | |
try: | |
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] | |
except KeyError: | |
pass | |
if ns != current_ns: | |
current_path = None | |
try: | |
extra, resolver = resolver.namespace_dict[ns] | |
resolved_path.append(ns) | |
ns_pattern = ns_pattern + extra | |
ns_converters.update(resolver.pattern.converters) | |
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: | |
resolver = get_ns_resolver(ns_pattern, resolver, tuple(ns_converters.items())) | |
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment