mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-25 19:14:01 +03:00
0dec36eb41
* Start test case * Added 'requests' test client * Address typos * Graceful fallback if requests is not installed. * Add cookie support * Tests for auth and CSRF * Py3 compat * py3 compat * py3 compat * Add get_requests_client * Added SchemaGenerator.should_include_link * add settings for html cutoff on related fields * Router doesn't work if prefix is blank, though project urls.py handles prefix * Fix Django 1.10 to-many deprecation * Add django.core.urlresolvers compatibility * Update django-filter & django-guardian * Check for empty router prefix; adjust URL accordingly It's easiest to fix this issue after we have made the regex. To try to fix it before would require doing something different for List vs Detail, which means we'd have to know which type of url we're constructing before acting accordingly. * Fix misc django deprecations * Use TOC extension instead of header * Fix deprecations for py3k * Add py3k compatibility to is_simple_callable * Add is_simple_callable tests * Drop python 3.2 support (EOL, Dropped by Django) * schema_renderers= should *set* the renderers, not append to them. * API client (#4424) * Fix release notes * Add note about 'User account is disabled.' vs 'Unable to log in' * Clean up schema generation (#4527) * Handle multiple methods on custom action (#4529) * RequestsClient, CoreAPIClient * exclude_from_schema * Added 'get_schema_view()' shortcut * Added schema descriptions * Better descriptions for schemas * Add type annotation to schema generation * Coerce schema 'pk' in path to actual field name * Deprecations move into assertion errors * Use get_schema_view in tests * Updte CoreJSON media type * Handle schema structure correctly when path prefixs exist. Closes #4401 * Add PendingDeprecation to Router schema generation. * Added SCHEMA_COERCE_PATH_PK and SCHEMA_COERCE_METHOD_NAMES * Renamed and documented 'get_schema_fields' interface.
65 lines
2.4 KiB
Python
65 lines
2.4 KiB
Python
from __future__ import unicode_literals
|
|
|
|
from django.conf.urls import include, url
|
|
|
|
from rest_framework.compat import RegexURLResolver
|
|
from rest_framework.settings import api_settings
|
|
|
|
|
|
def apply_suffix_patterns(urlpatterns, suffix_pattern, suffix_required):
|
|
ret = []
|
|
for urlpattern in urlpatterns:
|
|
if isinstance(urlpattern, RegexURLResolver):
|
|
# Set of included URL patterns
|
|
regex = urlpattern.regex.pattern
|
|
namespace = urlpattern.namespace
|
|
app_name = urlpattern.app_name
|
|
kwargs = urlpattern.default_kwargs
|
|
# Add in the included patterns, after applying the suffixes
|
|
patterns = apply_suffix_patterns(urlpattern.url_patterns,
|
|
suffix_pattern,
|
|
suffix_required)
|
|
ret.append(url(regex, include(patterns, namespace, app_name), kwargs))
|
|
|
|
else:
|
|
# Regular URL pattern
|
|
regex = urlpattern.regex.pattern.rstrip('$').rstrip('/') + suffix_pattern
|
|
view = urlpattern.callback
|
|
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
|
|
|
|
|
|
def format_suffix_patterns(urlpatterns, suffix_required=False, allowed=None):
|
|
"""
|
|
Supplement existing urlpatterns with corresponding patterns that also
|
|
include a '.format' suffix. Retains urlpattern ordering.
|
|
|
|
urlpatterns:
|
|
A list of URL patterns.
|
|
|
|
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.
|
|
"""
|
|
suffix_kwarg = api_settings.FORMAT_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-z0-9]+)/?$' % suffix_kwarg
|
|
|
|
return apply_suffix_patterns(urlpatterns, suffix_pattern, suffix_required)
|