urlpatterns.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from django.urls import URLResolver, include, path, re_path, register_converter
  2. from django.urls.resolvers import RoutePattern
  3. from rest_framework.settings import api_settings
  4. def _get_format_path_converter(suffix_kwarg, allowed):
  5. if allowed:
  6. if len(allowed) == 1:
  7. allowed_pattern = allowed[0]
  8. else:
  9. allowed_pattern = '(?:%s)' % '|'.join(allowed)
  10. suffix_pattern = r"\.%s/?" % allowed_pattern
  11. else:
  12. suffix_pattern = r"\.[a-z0-9]+/?"
  13. class FormatSuffixConverter:
  14. regex = suffix_pattern
  15. def to_python(self, value):
  16. return value.strip('./')
  17. def to_url(self, value):
  18. return '.' + value + '/'
  19. converter_name = 'drf_format_suffix'
  20. if allowed:
  21. converter_name += '_' + '_'.join(allowed)
  22. return converter_name, FormatSuffixConverter
  23. def apply_suffix_patterns(urlpatterns, suffix_pattern, suffix_required, suffix_route=None):
  24. ret = []
  25. for urlpattern in urlpatterns:
  26. if isinstance(urlpattern, URLResolver):
  27. # Set of included URL patterns
  28. regex = urlpattern.pattern.regex.pattern
  29. namespace = urlpattern.namespace
  30. app_name = urlpattern.app_name
  31. kwargs = urlpattern.default_kwargs
  32. # Add in the included patterns, after applying the suffixes
  33. patterns = apply_suffix_patterns(urlpattern.url_patterns,
  34. suffix_pattern,
  35. suffix_required,
  36. suffix_route)
  37. # if the original pattern was a RoutePattern we need to preserve it
  38. if isinstance(urlpattern.pattern, RoutePattern):
  39. assert path is not None
  40. route = str(urlpattern.pattern)
  41. new_pattern = path(route, include((patterns, app_name), namespace), kwargs)
  42. else:
  43. new_pattern = re_path(regex, include((patterns, app_name), namespace), kwargs)
  44. ret.append(new_pattern)
  45. else:
  46. # Regular URL pattern
  47. regex = urlpattern.pattern.regex.pattern.rstrip('$').rstrip('/') + suffix_pattern
  48. view = urlpattern.callback
  49. kwargs = urlpattern.default_args
  50. name = urlpattern.name
  51. # Add in both the existing and the new urlpattern
  52. if not suffix_required:
  53. ret.append(urlpattern)
  54. # if the original pattern was a RoutePattern we need to preserve it
  55. if isinstance(urlpattern.pattern, RoutePattern):
  56. assert path is not None
  57. assert suffix_route is not None
  58. route = str(urlpattern.pattern).rstrip('$').rstrip('/') + suffix_route
  59. new_pattern = path(route, view, kwargs, name)
  60. else:
  61. new_pattern = re_path(regex, view, kwargs, name)
  62. ret.append(new_pattern)
  63. return ret
  64. def format_suffix_patterns(urlpatterns, suffix_required=False, allowed=None):
  65. """
  66. Supplement existing urlpatterns with corresponding patterns that also
  67. include a '.format' suffix. Retains urlpattern ordering.
  68. urlpatterns:
  69. A list of URL patterns.
  70. suffix_required:
  71. If `True`, only suffixed URLs will be generated, and non-suffixed
  72. URLs will not be used. Defaults to `False`.
  73. allowed:
  74. An optional tuple/list of allowed suffixes. eg ['json', 'api']
  75. Defaults to `None`, which allows any suffix.
  76. """
  77. suffix_kwarg = api_settings.FORMAT_SUFFIX_KWARG
  78. if allowed:
  79. if len(allowed) == 1:
  80. allowed_pattern = allowed[0]
  81. else:
  82. allowed_pattern = '(%s)' % '|'.join(allowed)
  83. suffix_pattern = r'\.(?P<%s>%s)/?$' % (suffix_kwarg, allowed_pattern)
  84. else:
  85. suffix_pattern = r'\.(?P<%s>[a-z0-9]+)/?$' % suffix_kwarg
  86. converter_name, suffix_converter = _get_format_path_converter(suffix_kwarg, allowed)
  87. register_converter(suffix_converter, converter_name)
  88. suffix_route = '<%s:%s>' % (converter_name, suffix_kwarg)
  89. return apply_suffix_patterns(urlpatterns, suffix_pattern, suffix_required, suffix_route)