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

Refactor find_model_detail_view

This commit is contained in:
Tom Christie 2015-07-21 11:43:19 +01:00
parent 7d01beffff
commit 86e1a60ad2
2 changed files with 19 additions and 19 deletions

View File

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

View File

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