diff --git a/django_client_reverse/serializers.py b/django_client_reverse/serializers.py new file mode 100644 index 0000000..9791fef --- /dev/null +++ b/django_client_reverse/serializers.py @@ -0,0 +1,5 @@ +from rest_framework import serializers + + +class ReverserInputSerializer(serializers.Serializer): + ident = serializers.CharField(allow_blank=False) diff --git a/django_client_reverse/test_urls.py b/django_client_reverse/test_urls.py index 270c13d..19c780a 100644 --- a/django_client_reverse/test_urls.py +++ b/django_client_reverse/test_urls.py @@ -2,5 +2,5 @@ from django.conf.urls import url from django_client_reverse.views import Reverser urlpatterns = [ - url(r'^$', Reverser.as_view(), name="reverser") + url(r'^$', Reverser.as_view(), name="root") ] diff --git a/django_client_reverse/tests.py b/django_client_reverse/tests.py index a19d405..054f6f7 100644 --- a/django_client_reverse/tests.py +++ b/django_client_reverse/tests.py @@ -7,5 +7,5 @@ class ReverserTestCase(APITestCase): super().setUp() def test_thing(self): - response = self.client.get(reverse('reverser')) - print(response) + response = self.client.post(reverse('reverser'), data={'ident': 'tests:root'}) + self.assertEqual(response.data, '/tests/') diff --git a/django_client_reverse/views.py b/django_client_reverse/views.py index e1f3032..cc2606f 100644 --- a/django_client_reverse/views.py +++ b/django_client_reverse/views.py @@ -1,10 +1,15 @@ from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.renderers import JSONRenderer +from django.core.urlresolvers import reverse +from .serializers import ReverserInputSerializer class Reverser(APIView): renderer_classes = (JSONRenderer,) - def get(self, request, format=None): - return Response("Response") + def post(self, request, format=None): + input_serializer = ReverserInputSerializer(data=request.data) + input_serializer.is_valid(raise_exception=True) + url = reverse(input_serializer.validated_data['ident']) + return Response(url)