diff --git a/wagtail/contrib/wagtailapi/endpoints.py b/wagtail/contrib/wagtailapi/endpoints.py index 642f7df2ea..2eff00e5f8 100644 --- a/wagtail/contrib/wagtailapi/endpoints.py +++ b/wagtail/contrib/wagtailapi/endpoints.py @@ -9,6 +9,7 @@ from django.core.urlresolvers import reverse from rest_framework import status from rest_framework.response import Response from rest_framework.viewsets import GenericViewSet +from rest_framework.renderers import JSONRenderer from wagtail.wagtailcore.models import Page from wagtail.wagtailimages.models import get_image_model @@ -19,14 +20,13 @@ from .filters import ( FieldsFilter, OrderingFilter, SearchFilter, ChildOfFilter, DescendantOfFilter ) -from .renderers import WagtailJSONRenderer from .pagination import WagtailPagination from .serializers import BaseSerializer, PageSerializer, DocumentSerializer, ImageSerializer, get_serializer_class from .utils import BadRequestError class BaseAPIEndpoint(GenericViewSet): - renderer_classes = [WagtailJSONRenderer] + renderer_classes = [JSONRenderer] pagination_class = WagtailPagination base_serializer_class = BaseSerializer filter_backends = [] @@ -151,11 +151,7 @@ class BaseAPIEndpoint(GenericViewSet): def get_renderer_context(self): context = super(BaseAPIEndpoint, self).get_renderer_context() - context['endpoints'] = [ - PagesAPIEndpoint, - ImagesAPIEndpoint, - DocumentsAPIEndpoint - ] + context['indent'] = 4 return context @classmethod diff --git a/wagtail/contrib/wagtailapi/renderers.py b/wagtail/contrib/wagtailapi/renderers.py deleted file mode 100644 index 978db1c07f..0000000000 --- a/wagtail/contrib/wagtailapi/renderers.py +++ /dev/null @@ -1,43 +0,0 @@ -import json - -from django.core.serializers.json import DjangoJSONEncoder -from django.core.urlresolvers import reverse -from django.utils.six import text_type - -from rest_framework import renderers - -from .utils import ObjectDetailURL, get_full_url - - -def find_model_detail_view(model, endpoints): - for endpoint in endpoints: - if issubclass(model, endpoint.model): - return 'wagtailapi_v1:%s:detail' % endpoint.name - - -class WagtailJSONRenderer(renderers.BaseRenderer): - media_type = 'application/json' - charset = None - - def render(self, data, media_type=None, renderer_context=None): - request = renderer_context['request'] - endpoints = renderer_context['endpoints'] - - class WagtailAPIJSONEncoder(DjangoJSONEncoder): - def default(self, o): - if isinstance(o, ObjectDetailURL): - detail_view = find_model_detail_view(o.model, endpoints) - - if detail_view: - return get_full_url(request, reverse(detail_view, args=(o.pk, ))) - else: - return None - else: - return super(WagtailAPIJSONEncoder, self).default(o) - - ret = json.dumps(data, indent=4, cls=WagtailAPIJSONEncoder) - - # Deal with inconsistent py2/py3 behavior, and always return bytes. - if isinstance(ret, text_type): - return bytes(ret.encode('utf-8')) - return ret diff --git a/wagtail/contrib/wagtailapi/utils.py b/wagtail/contrib/wagtailapi/utils.py index 96ba69d8f4..629b1dc643 100644 --- a/wagtail/contrib/wagtailapi/utils.py +++ b/wagtail/contrib/wagtailapi/utils.py @@ -8,12 +8,6 @@ class BadRequestError(Exception): pass -class ObjectDetailURL(object): - def __init__(self, model, pk): - self.model = model - self.pk = pk - - def get_base_url(request=None): base_url = getattr(settings, 'WAGTAILAPI_BASE_URL', request.site.root_url if request else None)