Skip to content

Commit

Permalink
Support URLPattern and URLResolver from Django 2.0 (#5500)
Browse files Browse the repository at this point in the history
* Support URLPattern and URLResolver from Django 2.0

* fix import order
  • Loading branch information
mkuznets authored and Carlton Gibson committed Oct 16, 2017
1 parent e39d933 commit c91b081
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
23 changes: 22 additions & 1 deletion rest_framework/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,35 @@

try:
from django.urls import (
NoReverseMatch, RegexURLPattern, RegexURLResolver, ResolverMatch, Resolver404, get_script_prefix, reverse, reverse_lazy, resolve
NoReverseMatch, URLPattern as RegexURLPattern, URLResolver as RegexURLResolver, ResolverMatch, Resolver404, get_script_prefix, reverse, reverse_lazy, resolve
)

except ImportError:
from django.core.urlresolvers import ( # Will be removed in Django 2.0
NoReverseMatch, RegexURLPattern, RegexURLResolver, ResolverMatch, Resolver404, get_script_prefix, reverse, reverse_lazy, resolve
)


def get_regex_pattern(urlpattern):
if hasattr(urlpattern, 'pattern'):
# Django 2.0
return urlpattern.pattern.regex.pattern
else:
# Django < 2.0
return urlpattern.regex.pattern


def make_url_resolver(regex, urlpatterns):
try:
# Django 2.0
from django.urls.resolvers import RegexPattern
return RegexURLResolver(RegexPattern(regex), urlpatterns)

except ImportError:
# Django < 2.0
return RegexURLResolver(regex, urlpatterns)


try:
import urlparse # Python 2.x
except ImportError:
Expand Down
4 changes: 2 additions & 2 deletions rest_framework/schemas/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from rest_framework import exceptions
from rest_framework.compat import (
RegexURLPattern, RegexURLResolver, coreapi, coreschema
RegexURLPattern, RegexURLResolver, coreapi, coreschema, get_regex_pattern
)
from rest_framework.request import clone_request
from rest_framework.settings import api_settings
Expand Down Expand Up @@ -135,7 +135,7 @@ def get_api_endpoints(self, patterns=None, prefix=''):
api_endpoints = []

for pattern in patterns:
path_regex = prefix + pattern.regex.pattern
path_regex = prefix + get_regex_pattern(pattern)
if isinstance(pattern, RegexURLPattern):
path = self.get_path_from_regex(path_regex)
callback = pattern.callback
Expand Down
6 changes: 3 additions & 3 deletions rest_framework/urlpatterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.conf.urls import include, url

from rest_framework.compat import RegexURLResolver
from rest_framework.compat import RegexURLResolver, get_regex_pattern
from rest_framework.settings import api_settings


Expand All @@ -11,7 +11,7 @@ def apply_suffix_patterns(urlpatterns, suffix_pattern, suffix_required):
for urlpattern in urlpatterns:
if isinstance(urlpattern, RegexURLResolver):
# Set of included URL patterns
regex = urlpattern.regex.pattern
regex = get_regex_pattern(urlpattern)
namespace = urlpattern.namespace
app_name = urlpattern.app_name
kwargs = urlpattern.default_kwargs
Expand All @@ -22,7 +22,7 @@ def apply_suffix_patterns(urlpatterns, suffix_pattern, suffix_required):
ret.append(url(regex, include((patterns, app_name), namespace), kwargs))
else:
# Regular URL pattern
regex = urlpattern.regex.pattern.rstrip('$').rstrip('/') + suffix_pattern
regex = get_regex_pattern(urlpattern).rstrip('$').rstrip('/') + suffix_pattern
view = urlpattern.callback
kwargs = urlpattern.default_args
name = urlpattern.name
Expand Down
6 changes: 3 additions & 3 deletions tests/test_urlpatterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.conf.urls import include, url
from django.test import TestCase

from rest_framework.compat import RegexURLResolver, Resolver404
from rest_framework.compat import Resolver404, make_url_resolver
from rest_framework.test import APIRequestFactory
from rest_framework.urlpatterns import format_suffix_patterns

Expand All @@ -28,7 +28,7 @@ def _resolve_urlpatterns(self, urlpatterns, test_paths):
urlpatterns = format_suffix_patterns(urlpatterns)
except Exception:
self.fail("Failed to apply `format_suffix_patterns` on the supplied urlpatterns")
resolver = RegexURLResolver(r'^/', urlpatterns)
resolver = make_url_resolver(r'^/', urlpatterns)
for test_path in test_paths:
request = factory.get(test_path.path)
try:
Expand All @@ -43,7 +43,7 @@ def test_trailing_slash(self):
urlpatterns = format_suffix_patterns([
url(r'^test/$', dummy_view),
])
resolver = RegexURLResolver(r'^/', urlpatterns)
resolver = make_url_resolver(r'^/', urlpatterns)

test_paths = [
(URLTestPath('/test.api', (), {'format': 'api'}), True),
Expand Down

0 comments on commit c91b081

Please sign in to comment.