0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Rejig find_model_detail_view for easier customization of available endpoints.

This commit is contained in:
Tom Christie 2015-07-23 09:39:12 +01:00
parent 1905585e62
commit 5cfeaa437d
2 changed files with 15 additions and 7 deletions

View File

@ -110,6 +110,15 @@ class BaseAPIEndpoint(GenericViewSet):
'show_details': True
}
def get_renderer_context(self):
context = super(BaseAPIEndpoint, self).get_renderer_context()
context['endpoints'] = [
PagesAPIEndpoint,
ImagesAPIEndpoint,
DocumentsAPIEndpoint
]
return context
@classmethod
def get_urlpatterns(cls):
"""

View File

@ -19,10 +19,8 @@ def get_full_url(request, path):
return base_url + path
def find_model_detail_view(model):
from .endpoints import PagesAPIEndpoint, ImagesAPIEndpoint, DocumentsAPIEndpoint
for endpoint in [PagesAPIEndpoint, ImagesAPIEndpoint, DocumentsAPIEndpoint]:
def find_model_detail_view(model, endpoints):
for endpoint in endpoints:
if endpoint.has_model(model):
return 'wagtailapi_v1:%s:detail' % endpoint.name
@ -33,6 +31,7 @@ class WagtailJSONRenderer(renderers.BaseRenderer):
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):
@ -43,10 +42,10 @@ class WagtailJSONRenderer(renderers.BaseRenderer):
elif isinstance(o, URLPath):
return get_full_url(request, o.path)
elif isinstance(o, ObjectDetailURL):
view = find_model_detail_view(o.model)
detail_view = find_model_detail_view(o.model, endpoints)
if view:
return get_full_url(request, reverse(view, args=(o.pk, )))
if detail_view:
return get_full_url(request, reverse(detail_view, args=(o.pk, )))
else:
return None
elif isinstance(o, StreamValue):