2013-02-05 00:55:35 +04:00
|
|
|
from __future__ import unicode_literals
|
2015-06-18 16:38:29 +03:00
|
|
|
|
2015-06-25 23:55:51 +03:00
|
|
|
from django.conf.urls import include, url
|
2013-02-05 00:55:35 +04:00
|
|
|
from django.core.urlresolvers import RegexURLResolver
|
2015-06-18 16:38:29 +03:00
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
from rest_framework.settings import api_settings
|
2013-01-19 20:56:48 +04:00
|
|
|
|
|
|
|
|
|
|
|
def apply_suffix_patterns(urlpatterns, suffix_pattern, suffix_required):
|
|
|
|
ret = []
|
|
|
|
for urlpattern in urlpatterns:
|
2013-01-21 21:42:33 +04:00
|
|
|
if isinstance(urlpattern, RegexURLResolver):
|
2013-01-19 20:56:48 +04:00
|
|
|
# Set of included URL patterns
|
|
|
|
regex = urlpattern.regex.pattern
|
|
|
|
namespace = urlpattern.namespace
|
|
|
|
app_name = urlpattern.app_name
|
2013-01-19 22:39:39 +04:00
|
|
|
kwargs = urlpattern.default_kwargs
|
2013-01-21 21:42:33 +04:00
|
|
|
# Add in the included patterns, after applying the suffixes
|
2013-01-19 20:56:48 +04:00
|
|
|
patterns = apply_suffix_patterns(urlpattern.url_patterns,
|
|
|
|
suffix_pattern,
|
|
|
|
suffix_required)
|
2013-01-19 22:39:39 +04:00
|
|
|
ret.append(url(regex, include(patterns, namespace, app_name), kwargs))
|
2013-01-21 21:42:33 +04:00
|
|
|
|
|
|
|
else:
|
|
|
|
# Regular URL pattern
|
2015-06-11 01:32:05 +03:00
|
|
|
regex = urlpattern.regex.pattern.rstrip('$').rstrip('/') + suffix_pattern
|
2016-06-01 17:31:00 +03:00
|
|
|
view = urlpattern.callback
|
2013-01-21 21:42:33 +04:00
|
|
|
kwargs = urlpattern.default_args
|
|
|
|
name = urlpattern.name
|
|
|
|
# Add in both the existing and the new urlpattern
|
|
|
|
if not suffix_required:
|
|
|
|
ret.append(urlpattern)
|
|
|
|
ret.append(url(regex, view, kwargs, name))
|
|
|
|
|
2013-01-19 20:56:48 +04:00
|
|
|
return ret
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
2012-10-19 22:59:46 +04:00
|
|
|
def format_suffix_patterns(urlpatterns, suffix_required=False, allowed=None):
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
2012-11-14 22:36:29 +04:00
|
|
|
Supplement existing urlpatterns with corresponding patterns that also
|
2012-09-20 16:06:27 +04:00
|
|
|
include a '.format' suffix. Retains urlpattern ordering.
|
2012-10-09 15:01:17 +04:00
|
|
|
|
2012-10-19 22:59:46 +04:00
|
|
|
urlpatterns:
|
|
|
|
A list of URL patterns.
|
|
|
|
|
2012-10-09 15:01:17 +04:00
|
|
|
suffix_required:
|
|
|
|
If `True`, only suffixed URLs will be generated, and non-suffixed
|
|
|
|
URLs will not be used. Defaults to `False`.
|
|
|
|
|
|
|
|
allowed:
|
|
|
|
An optional tuple/list of allowed suffixes. eg ['json', 'api']
|
|
|
|
Defaults to `None`, which allows any suffix.
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
2012-10-19 22:59:46 +04:00
|
|
|
suffix_kwarg = api_settings.FORMAT_SUFFIX_KWARG
|
2012-10-09 15:01:17 +04:00
|
|
|
if allowed:
|
|
|
|
if len(allowed) == 1:
|
|
|
|
allowed_pattern = allowed[0]
|
|
|
|
else:
|
|
|
|
allowed_pattern = '(%s)' % '|'.join(allowed)
|
2015-06-11 01:32:05 +03:00
|
|
|
suffix_pattern = r'\.(?P<%s>%s)/?$' % (suffix_kwarg, allowed_pattern)
|
2012-10-09 15:01:17 +04:00
|
|
|
else:
|
2015-06-11 01:32:05 +03:00
|
|
|
suffix_pattern = r'\.(?P<%s>[a-z0-9]+)/?$' % suffix_kwarg
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2013-01-19 20:56:48 +04:00
|
|
|
return apply_suffix_patterns(urlpatterns, suffix_pattern, suffix_required)
|