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