You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reverse.py 2.1KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """
  2. Provide urlresolver functions that return fully qualified URLs or view names
  3. """
  4. from django.urls import NoReverseMatch
  5. from django.urls import reverse as django_reverse
  6. from django.utils.functional import lazy
  7. from rest_framework.settings import api_settings
  8. from rest_framework.utils.urls import replace_query_param
  9. def preserve_builtin_query_params(url, request=None):
  10. """
  11. Given an incoming request, and an outgoing URL representation,
  12. append the value of any built-in query parameters.
  13. """
  14. if request is None:
  15. return url
  16. overrides = [
  17. api_settings.URL_FORMAT_OVERRIDE,
  18. ]
  19. for param in overrides:
  20. if param and (param in request.GET):
  21. value = request.GET[param]
  22. url = replace_query_param(url, param, value)
  23. return url
  24. def reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra):
  25. """
  26. If versioning is being used then we pass any `reverse` calls through
  27. to the versioning scheme instance, so that the resulting URL
  28. can be modified if needed.
  29. """
  30. scheme = getattr(request, 'versioning_scheme', None)
  31. if scheme is not None:
  32. try:
  33. url = scheme.reverse(viewname, args, kwargs, request, format, **extra)
  34. except NoReverseMatch:
  35. # In case the versioning scheme reversal fails, fallback to the
  36. # default implementation
  37. url = _reverse(viewname, args, kwargs, request, format, **extra)
  38. else:
  39. url = _reverse(viewname, args, kwargs, request, format, **extra)
  40. return preserve_builtin_query_params(url, request)
  41. def _reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra):
  42. """
  43. Same as `django.urls.reverse`, but optionally takes a request
  44. and returns a fully qualified URL, using the request to get the base URL.
  45. """
  46. if format is not None:
  47. kwargs = kwargs or {}
  48. kwargs['format'] = format
  49. url = django_reverse(viewname, args=args, kwargs=kwargs, **extra)
  50. if request:
  51. return request.build_absolute_uri(url)
  52. return url
  53. reverse_lazy = lazy(reverse, str)