1
Fork 0

Added args and kwargs parsing

This commit is contained in:
Jake Howard 2015-12-10 15:07:19 +00:00
parent 2620ccac96
commit e111a7041c
4 changed files with 28 additions and 4 deletions

View file

@ -3,3 +3,5 @@ from rest_framework import serializers
class ReverserInputSerializer(serializers.Serializer):
ident = serializers.CharField(allow_blank=False)
args = serializers.ListField(required=False, default=None)
kwargs = serializers.JSONField(required=False, default=None)

View file

@ -2,5 +2,6 @@ from django.conf.urls import url
from django_client_reverse.views import Reverser
urlpatterns = [
url(r'^$', Reverser.as_view(), name="root")
url(r'^$', Reverser.as_view(), name="root"),
url(r'(?P<uuid>[0-9a-f-]+)/$', Reverser.as_view(), name="uuid")
]

View file

@ -1,5 +1,6 @@
from rest_framework.test import APITestCase
from django.core.urlresolvers import reverse
from uuid import uuid4
class ReverserTestCase(APITestCase):
@ -13,3 +14,19 @@ class ReverserTestCase(APITestCase):
def test_invalid(self):
response = self.client.post(reverse('reverser'), data={'ident': 'tests:not_a_thing'})
self.assertEqual(response.status_code, 404)
def test_kwrgs_pass(self):
kwargs = {'uuid': str(uuid4())}
response = self.client.post(reverse('reverser'), data={
'ident': 'tests:uuid',
'kwargs': kwargs
}, format="json")
self.assertEqual(response.data, reverse('tests:uuid', kwargs=kwargs))
def test_args_pass(self):
args = (str(uuid4()),)
response = self.client.post(reverse('reverser'), data={
'ident': 'tests:uuid',
'args': args
}, format="json")
self.assertEqual(response.data, reverse('tests:uuid', args=args))

View file

@ -12,7 +12,11 @@ class Reverser(APIView):
input_serializer = ReverserInputSerializer(data=request.data)
input_serializer.is_valid(raise_exception=True)
try:
url = reverse(input_serializer.validated_data['ident'])
except NoReverseMatch:
return Response(status=404)
url = reverse(
input_serializer.validated_data['ident'],
args=input_serializer.validated_data['args'],
kwargs=input_serializer.validated_data['kwargs']
)
except NoReverseMatch as e:
return Response(str(e), status=404)
return Response(url, 302)