kopia lustrzana https://github.com/wagtail/wagtail
Added WagtailAPIRouter
rodzic
a3473544b8
commit
1e1077f891
|
@ -0,0 +1,40 @@
|
|||
import functools
|
||||
|
||||
from django.conf.urls import url, include
|
||||
|
||||
from wagtail.utils.urlpatterns import decorate_urlpatterns
|
||||
|
||||
|
||||
class WagtailAPIRouter(object):
|
||||
def __init__(self, url_namespace):
|
||||
self.url_namespace = url_namespace
|
||||
self._endpoints = {}
|
||||
|
||||
def register_endpoint(self, name, class_):
|
||||
self._endpoints[name] = class_
|
||||
|
||||
def wrap_view(self, func):
|
||||
@functools.wraps(func)
|
||||
def wrapped(request, *args, **kwargs):
|
||||
request.wagtailapi_router = self
|
||||
return func(request, *args, **kwargs)
|
||||
|
||||
return wrapped
|
||||
|
||||
def get_urlpatterns(self):
|
||||
urlpatterns = []
|
||||
|
||||
for name, class_ in self._endpoints.items():
|
||||
pattern = url(
|
||||
r'^{}/'.format(name),
|
||||
include(class_.get_urlpatterns(), namespace=name)
|
||||
)
|
||||
urlpatterns.append(pattern)
|
||||
|
||||
decorate_urlpatterns(urlpatterns, self.wrap_view)
|
||||
|
||||
return urlpatterns
|
||||
|
||||
@property
|
||||
def urls(self):
|
||||
return self.get_urlpatterns(), self.url_namespace, self.url_namespace
|
|
@ -1,17 +1,16 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
from django.conf.urls import url, include
|
||||
from django.conf.urls import url
|
||||
|
||||
from .endpoints import PagesAPIEndpoint, ImagesAPIEndpoint, DocumentsAPIEndpoint
|
||||
from .router import WagtailAPIRouter
|
||||
|
||||
|
||||
v1 = [
|
||||
url(r'^pages/', include(PagesAPIEndpoint.get_urlpatterns(), namespace='pages')),
|
||||
url(r'^images/', include(ImagesAPIEndpoint.get_urlpatterns(), namespace='images')),
|
||||
url(r'^documents/', include(DocumentsAPIEndpoint.get_urlpatterns(), namespace='documents'))
|
||||
]
|
||||
|
||||
v1 = WagtailAPIRouter('wagtailapi_v1')
|
||||
v1.register_endpoint('pages', PagesAPIEndpoint)
|
||||
v1.register_endpoint('images', ImagesAPIEndpoint)
|
||||
v1.register_endpoint('documents', DocumentsAPIEndpoint)
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^v1/', include(v1, namespace='wagtailapi_v1')),
|
||||
url(r'^v1/', v1.urls),
|
||||
]
|
||||
|
|
Ładowanie…
Reference in New Issue