aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/urlpatterns.py
diff options
context:
space:
mode:
Diffstat (limited to 'rest_framework/urlpatterns.py')
-rw-r--r--rest_framework/urlpatterns.py25
1 files changed, 23 insertions, 2 deletions
diff --git a/rest_framework/urlpatterns.py b/rest_framework/urlpatterns.py
index a040b621..386c78a2 100644
--- a/rest_framework/urlpatterns.py
+++ b/rest_framework/urlpatterns.py
@@ -2,13 +2,34 @@ from django.conf.urls.defaults import url
from rest_framework.settings import api_settings
-def format_suffix_patterns(urlpatterns, suffix_required=False, suffix_kwarg=None):
+def format_suffix_patterns(urlpatterns, suffix_required=False,
+ suffix_kwarg=None, allowed=None):
"""
Supplement existing urlpatterns with corrosponding patterns that also
include a '.format' suffix. Retains urlpattern ordering.
+
+ suffix_required:
+ If `True`, only suffixed URLs will be generated, and non-suffixed
+ URLs will not be used. Defaults to `False`.
+
+ suffix_kwarg:
+ The name of the kwarg that will be passed to the view.
+ Defaults to 'format'.
+
+ allowed:
+ An optional tuple/list of allowed suffixes. eg ['json', 'api']
+ Defaults to `None`, which allows any suffix.
+
"""
suffix_kwarg = suffix_kwarg or api_settings.FORMAT_SUFFIX_KWARG
- suffix_pattern = r'\.(?P<%s>[a-z]+)$' % suffix_kwarg
+ if allowed:
+ if len(allowed) == 1:
+ allowed_pattern = allowed[0]
+ else:
+ allowed_pattern = '(%s)' % '|'.join(allowed)
+ suffix_pattern = r'\.(?P<%s>%s)$' % (suffix_kwarg, allowed_pattern)
+ else:
+ suffix_pattern = r'\.(?P<%s>[a-z]+)$' % suffix_kwarg
ret = []
for urlpattern in urlpatterns: