Removed WagtailJSONRenderer

pull/1789/head
Karl Hobley 2015-10-03 18:00:42 +01:00
rodzic 789cbabebf
commit de689215b0
3 zmienionych plików z 3 dodań i 56 usunięć

Wyświetl plik

@ -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

Wyświetl plik

@ -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

Wyświetl plik

@ -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)