2012-09-20 16:06:27 +04:00
|
|
|
"""
|
2015-01-29 04:08:34 +03:00
|
|
|
Provide urlresolver functions that return fully qualified URLs or view names
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
2017-11-10 11:41:03 +03:00
|
|
|
from django.urls import NoReverseMatch
|
2018-02-14 23:12:14 +03:00
|
|
|
from django.urls import reverse as django_reverse
|
2012-09-20 16:06:27 +04:00
|
|
|
from django.utils.functional import lazy
|
|
|
|
|
2015-05-15 11:18:45 +03:00
|
|
|
from rest_framework.settings import api_settings
|
|
|
|
from rest_framework.utils.urls import replace_query_param
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
2015-05-19 17:49:37 +03:00
|
|
|
def preserve_builtin_query_params(url, request=None):
|
|
|
|
"""
|
|
|
|
Given an incoming request, and an outgoing URL representation,
|
|
|
|
append the value of any built-in query parameters.
|
|
|
|
"""
|
|
|
|
if request is None:
|
|
|
|
return url
|
|
|
|
|
|
|
|
overrides = [
|
|
|
|
api_settings.URL_FORMAT_OVERRIDE,
|
|
|
|
]
|
|
|
|
|
|
|
|
for param in overrides:
|
|
|
|
if param and (param in request.GET):
|
|
|
|
value = request.GET[param]
|
|
|
|
url = replace_query_param(url, param, value)
|
|
|
|
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
2012-10-29 21:08:38 +04:00
|
|
|
def reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra):
|
2014-12-16 18:34:19 +03:00
|
|
|
"""
|
|
|
|
If versioning is being used then we pass any `reverse` calls through
|
|
|
|
to the versioning scheme instance, so that the resulting URL
|
|
|
|
can be modified if needed.
|
|
|
|
"""
|
|
|
|
scheme = getattr(request, 'versioning_scheme', None)
|
|
|
|
if scheme is not None:
|
2015-03-20 02:12:28 +03:00
|
|
|
try:
|
2015-05-15 11:18:45 +03:00
|
|
|
url = scheme.reverse(viewname, args, kwargs, request, format, **extra)
|
2015-03-20 02:12:28 +03:00
|
|
|
except NoReverseMatch:
|
|
|
|
# In case the versioning scheme reversal fails, fallback to the
|
|
|
|
# default implementation
|
2015-05-15 11:18:45 +03:00
|
|
|
url = _reverse(viewname, args, kwargs, request, format, **extra)
|
|
|
|
else:
|
|
|
|
url = _reverse(viewname, args, kwargs, request, format, **extra)
|
2014-12-16 18:34:19 +03:00
|
|
|
|
2015-05-19 17:49:37 +03:00
|
|
|
return preserve_builtin_query_params(url, request)
|
|
|
|
|
2014-12-16 18:34:19 +03:00
|
|
|
|
|
|
|
def _reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra):
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
2016-10-10 15:03:46 +03:00
|
|
|
Same as `django.urls.reverse`, but optionally takes a request
|
2012-09-20 16:06:27 +04:00
|
|
|
and returns a fully qualified URL, using the request to get the base URL.
|
|
|
|
"""
|
2012-10-29 21:08:38 +04:00
|
|
|
if format is not None:
|
|
|
|
kwargs = kwargs or {}
|
|
|
|
kwargs['format'] = format
|
|
|
|
url = django_reverse(viewname, args=args, kwargs=kwargs, **extra)
|
2012-09-20 16:06:27 +04:00
|
|
|
if request:
|
|
|
|
return request.build_absolute_uri(url)
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
2019-04-30 18:53:44 +03:00
|
|
|
reverse_lazy = lazy(reverse, str)
|