diff --git a/django_client_reverse/serializers.py b/django_client_reverse/serializers.py index 9791fef..ef99599 100644 --- a/django_client_reverse/serializers.py +++ b/django_client_reverse/serializers.py @@ -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) diff --git a/django_client_reverse/test_urls.py b/django_client_reverse/test_urls.py index 19c780a..50b5847 100644 --- a/django_client_reverse/test_urls.py +++ b/django_client_reverse/test_urls.py @@ -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[0-9a-f-]+)/$', Reverser.as_view(), name="uuid") ] diff --git a/django_client_reverse/tests.py b/django_client_reverse/tests.py index 72e68a1..47bbb64 100644 --- a/django_client_reverse/tests.py +++ b/django_client_reverse/tests.py @@ -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)) diff --git a/django_client_reverse/views.py b/django_client_reverse/views.py index 4c6cf93..fc9ace7 100644 --- a/django_client_reverse/views.py +++ b/django_client_reverse/views.py @@ -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)