django-rest-framework/rest_framework/reverse.py

44 lines
1.5 KiB
Python
Raw Normal View History

"""
Provide urlresolver functions that return fully qualified URLs or view names
"""
from __future__ import unicode_literals
from django.core.urlresolvers import reverse as django_reverse
2015-03-20 02:12:28 +03:00
from django.core.urlresolvers import NoReverseMatch
from django.utils import six
from django.utils.functional import lazy
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:
return scheme.reverse(viewname, args, kwargs, request, format, **extra)
except NoReverseMatch:
# In case the versioning scheme reversal fails, fallback to the
# default implementation
pass
2014-12-16 18:34:19 +03:00
return _reverse(viewname, args, kwargs, request, format, **extra)
def _reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra):
"""
Same as `django.core.urlresolvers.reverse`, but optionally takes a request
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)
if request:
return request.build_absolute_uri(url)
return url
reverse_lazy = lazy(reverse, six.text_type)