django-rest-framework/djangorestframework/urlpatterns.py

25 lines
956 B
Python
Raw Normal View History

from django.conf.urls.defaults import url
2012-09-07 12:36:52 +04:00
from djangorestframework.settings import api_settings
2012-09-07 12:36:52 +04:00
def format_suffix_patterns(urlpatterns, suffix_required=False, suffix_kwarg=None):
"""
Supplement existing urlpatterns with corrosponding patterns that also
include a '.format' suffix. Retains urlpattern ordering.
"""
2012-09-07 12:36:52 +04:00
suffix_kwarg = suffix_kwarg or api_settings.FORMAT_SUFFIX_KWARG
suffix_pattern = '.(?P<%s>[a-z]+)$' % suffix_kwarg
ret = []
for urlpattern in urlpatterns:
# Form our complementing '.format' urlpattern
2012-09-07 12:36:52 +04:00
regex = urlpattern.regex.pattern.rstrip('$') + suffix_pattern
view = urlpattern._callback or urlpattern._callback_str
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))
return ret