From 86e1a60ad2bf2ef4c14b503f2f9e2138c9dc4f8d Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 21 Jul 2015 11:43:19 +0100 Subject: [PATCH] Refactor find_model_detail_view --- wagtail/contrib/wagtailapi/endpoints.py | 28 +++++++++---------------- wagtail/contrib/wagtailapi/renderers.py | 10 ++++++++- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/wagtail/contrib/wagtailapi/endpoints.py b/wagtail/contrib/wagtailapi/endpoints.py index 669569e03f..469691df62 100644 --- a/wagtail/contrib/wagtailapi/endpoints.py +++ b/wagtail/contrib/wagtailapi/endpoints.py @@ -115,20 +115,9 @@ class BaseAPIEndpoint(GenericViewSet): url(r'^(\d+)/$', cls.as_view({'get': 'detail_view'}), name='detail'), ] - def find_model_detail_view(self, model): - # TODO: Needs refactoring. This is currently duplicated, and also - # does a bit of a dance around instantiating these classes. - endpoints = { - 'pages': PagesAPIEndpoint(), - 'images': ImagesAPIEndpoint(), - 'documents': DocumentsAPIEndpoint(), - } - for endpoint_name, endpoint in endpoints.items(): - if endpoint.has_model(model): - return 'wagtailapi_v1:%s:detail' % endpoint_name - - def has_model(self, model): - return False + @classmethod + def has_model(cls, model): + return NotImplemented class PagesAPIEndpoint(BaseAPIEndpoint): @@ -191,7 +180,8 @@ class PagesAPIEndpoint(BaseAPIEndpoint): serializer = self.get_serializer(page) return Response(serializer.data) - def has_model(self, model): + @classmethod + def has_model(cls, model): return issubclass(model, Page) @@ -224,8 +214,9 @@ class ImagesAPIEndpoint(BaseAPIEndpoint): serializer = self.get_serializer(image) return Response(serializer.data) - def has_model(self, model): - return model == self.model + @classmethod + def has_model(cls, model): + return model == cls.model class DocumentsAPIEndpoint(BaseAPIEndpoint): @@ -254,5 +245,6 @@ class DocumentsAPIEndpoint(BaseAPIEndpoint): serializer = self.get_serializer(document) return Response(serializer.data) - def has_model(self, model): + @classmethod + def has_model(cls, model): return model == Document diff --git a/wagtail/contrib/wagtailapi/renderers.py b/wagtail/contrib/wagtailapi/renderers.py index c58ea32c63..25031a93dd 100644 --- a/wagtail/contrib/wagtailapi/renderers.py +++ b/wagtail/contrib/wagtailapi/renderers.py @@ -18,6 +18,14 @@ 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]: + if endpoint.has_model(model): + return 'wagtailapi_v1:%s:detail' % endpoint.name + + class WagtailJSONRenderer(renderers.BaseRenderer): media_type = 'application/json' charset = None @@ -35,7 +43,7 @@ class WagtailJSONRenderer(renderers.BaseRenderer): elif isinstance(o, URLPath): return get_full_url(request, o.path) elif isinstance(o, ObjectDetailURL): - view = endpoint.find_model_detail_view(o.model) + view = find_model_detail_view(o.model) if view: return get_full_url(request, reverse(view, args=(o.pk, )))