mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
Provide rest_framework.resolve. Fixes #2489
This commit is contained in:
parent
e63f49bd1d
commit
77d061d234
|
@ -1,7 +1,7 @@
|
|||
# coding: utf-8
|
||||
from __future__ import unicode_literals
|
||||
from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured
|
||||
from django.core.urlresolvers import resolve, get_script_prefix, NoReverseMatch, Resolver404
|
||||
from django.core.urlresolvers import get_script_prefix, NoReverseMatch, Resolver404
|
||||
from django.db.models.query import QuerySet
|
||||
from django.utils import six
|
||||
from django.utils.encoding import smart_text
|
||||
|
@ -9,7 +9,7 @@ from django.utils.six.moves.urllib import parse as urlparse
|
|||
from django.utils.translation import ugettext_lazy as _
|
||||
from rest_framework.compat import OrderedDict
|
||||
from rest_framework.fields import get_attribute, empty, Field
|
||||
from rest_framework.reverse import reverse
|
||||
from rest_framework.reverse import reverse, resolve
|
||||
from rest_framework.utils import html
|
||||
|
||||
|
||||
|
@ -205,6 +205,7 @@ class HyperlinkedRelatedField(RelatedField):
|
|||
return self.reverse(view_name, kwargs=kwargs, request=request, format=format)
|
||||
|
||||
def to_internal_value(self, data):
|
||||
request = self.context.get('request', None)
|
||||
try:
|
||||
http_prefix = data.startswith(('http:', 'https:'))
|
||||
except AttributeError:
|
||||
|
@ -218,7 +219,7 @@ class HyperlinkedRelatedField(RelatedField):
|
|||
data = '/' + data[len(prefix):]
|
||||
|
||||
try:
|
||||
match = self.resolve(data)
|
||||
match = self.resolve(data, request=request)
|
||||
except Resolver404:
|
||||
self.fail('no_match')
|
||||
|
||||
|
|
|
@ -1,12 +1,25 @@
|
|||
"""
|
||||
Provide reverse functions that return fully qualified URLs
|
||||
Provide urlresolver functions that return fully qualified URLs or view names
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
from django.core.urlresolvers import reverse as django_reverse
|
||||
from django.core.urlresolvers import resolve as django_resolve
|
||||
from django.utils import six
|
||||
from django.utils.functional import lazy
|
||||
|
||||
|
||||
def resolve(path, urlconf=None, request=None):
|
||||
"""
|
||||
If versioning is being used then we pass any `resolve` calls through
|
||||
to the versioning scheme instance, so that the resulting view name
|
||||
can be modified if needed.
|
||||
"""
|
||||
scheme = getattr(request, 'versioning_scheme', None)
|
||||
if scheme is not None:
|
||||
return scheme.resolve(path, urlconf, request)
|
||||
return django_resolve(path, urlconf)
|
||||
|
||||
|
||||
def reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra):
|
||||
"""
|
||||
If versioning is being used then we pass any `reverse` calls through
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
# coding: utf-8
|
||||
from __future__ import unicode_literals
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.core.urlresolvers import resolve as django_resolve
|
||||
from django.core.urlresolvers import ResolverMatch
|
||||
from rest_framework import exceptions
|
||||
from rest_framework.compat import unicode_http_header
|
||||
from rest_framework.reverse import _reverse
|
||||
|
@ -24,6 +26,9 @@ class BaseVersioning(object):
|
|||
def reverse(self, viewname, args=None, kwargs=None, request=None, format=None, **extra):
|
||||
return _reverse(viewname, args, kwargs, request, format, **extra)
|
||||
|
||||
def resolve(self, path, urlconf=None):
|
||||
return django_resolve(path, urlconf)
|
||||
|
||||
def is_allowed_version(self, version):
|
||||
if not self.allowed_versions:
|
||||
return True
|
||||
|
@ -127,6 +132,17 @@ class NamespaceVersioning(BaseVersioning):
|
|||
viewname, args, kwargs, request, format, **extra
|
||||
)
|
||||
|
||||
def resolve(self, path, urlconf=None, request=None):
|
||||
match = django_resolve(path, urlconf)
|
||||
if match.namespace:
|
||||
_, view_name = match.view_name.split(':')
|
||||
return ResolverMatch(func=match.func,
|
||||
args=match.args,
|
||||
kwargs=match.kwargs,
|
||||
url_name=view_name,
|
||||
app_name=match.app_name)
|
||||
return match
|
||||
|
||||
|
||||
class HostNameVersioning(BaseVersioning):
|
||||
"""
|
||||
|
|
|
@ -1,11 +1,28 @@
|
|||
from .utils import mock_reverse, fail_reverse, BadType, MockObject, MockQueryset
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.conf.urls import patterns, url, include
|
||||
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
|
||||
from django.utils.datastructures import MultiValueDict
|
||||
from rest_framework import serializers
|
||||
from rest_framework.fields import empty
|
||||
from rest_framework.test import APISimpleTestCase
|
||||
from rest_framework.test import APISimpleTestCase, APIRequestFactory
|
||||
from rest_framework.versioning import NamespaceVersioning
|
||||
import pytest
|
||||
|
||||
factory = APIRequestFactory()
|
||||
request = factory.get('/') # Just to ensure we have a request in the serializer context
|
||||
|
||||
dummy_view = lambda request, pk: None
|
||||
|
||||
included_patterns = [
|
||||
url(r'^example/(?P<pk>\d+)/$', dummy_view, name='example-detail')
|
||||
]
|
||||
|
||||
urlpatterns = patterns(
|
||||
'',
|
||||
url(r'^v1/', include(included_patterns, namespace='v1')),
|
||||
url(r'^example/(?P<pk>\d+)/$', dummy_view, name='example-detail')
|
||||
)
|
||||
|
||||
|
||||
class TestStringRelatedField(APISimpleTestCase):
|
||||
def setUp(self):
|
||||
|
@ -48,6 +65,35 @@ class TestPrimaryKeyRelatedField(APISimpleTestCase):
|
|||
assert representation == self.instance.pk
|
||||
|
||||
|
||||
class TestHyperlinkedRelatedField(APISimpleTestCase):
|
||||
urls = 'tests.test_relations'
|
||||
|
||||
def setUp(self):
|
||||
class HyperlinkedMockQueryset(MockQueryset):
|
||||
def get(self, **lookup):
|
||||
for item in self.items:
|
||||
if item.pk == int(lookup.get('pk', -1)):
|
||||
return item
|
||||
raise ObjectDoesNotExist()
|
||||
|
||||
self.queryset = HyperlinkedMockQueryset([
|
||||
MockObject(pk=1, name='foo'),
|
||||
MockObject(pk=2, name='bar'),
|
||||
MockObject(pk=3, name='baz')
|
||||
])
|
||||
self.field = serializers.HyperlinkedRelatedField(
|
||||
view_name='example-detail',
|
||||
queryset=self.queryset
|
||||
)
|
||||
request = factory.post('/')
|
||||
request.versioning_scheme = NamespaceVersioning()
|
||||
self.field._context = {'request': request}
|
||||
|
||||
def test_bug_2489(self):
|
||||
self.field.to_internal_value('/example/3/')
|
||||
self.field.to_internal_value('/v1/example/3/')
|
||||
|
||||
|
||||
class TestHyperlinkedIdentityField(APISimpleTestCase):
|
||||
def setUp(self):
|
||||
self.instance = MockObject(pk=1, name='foo')
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""
|
||||
Blank URLConf just to keep the test suite happy
|
||||
"""
|
||||
from django.conf.urls import patterns
|
||||
from tests import test_relations
|
||||
|
||||
urlpatterns = patterns('')
|
||||
urlpatterns = test_relations.urlpatterns
|
||||
|
|
Loading…
Reference in New Issue
Block a user