urlpatterns.py 4.2 KB

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