kopia lustrzana https://github.com/wagtail/wagtail
Rename Endpoints to ViewSets
For consistency with Django REST Frameworkpull/5677/head
rodzic
0dbe25c512
commit
16d5b5c99e
|
@ -45,13 +45,13 @@ can hook into the rest of your project.
|
|||
|
||||
Wagtail provides three endpoint classes you can use:
|
||||
|
||||
- Pages :class:`wagtail.api.v2.endpoints.PagesAPIEndpoint`
|
||||
- Images :class:`wagtail.images.api.v2.endpoints.ImagesAPIEndpoint`
|
||||
- Documents :class:`wagtail.documents.api.v2.endpoints.DocumentsAPIEndpoint`
|
||||
- Pages :class:`wagtail.api.v2.endpoints.PagesAPIViewSet`
|
||||
- Images :class:`wagtail.images.api.v2.endpoints.ImagesAPIViewSet`
|
||||
- Documents :class:`wagtail.documents.api.v2.endpoints.DocumentsAPIViewSet`
|
||||
|
||||
You can subclass any of these endpoint classes to customise their functionality.
|
||||
Additionally, there is a base endpoint class you can use for adding different
|
||||
content types to the API: :class:`wagtail.api.v2.endpoints.BaseAPIEndpoint`
|
||||
content types to the API: :class:`wagtail.api.v2.endpoints.BaseAPIViewSet`
|
||||
|
||||
For this example, we will create an API that includes all three builtin content
|
||||
types in their default configuration:
|
||||
|
@ -60,10 +60,10 @@ types in their default configuration:
|
|||
|
||||
# api.py
|
||||
|
||||
from wagtail.api.v2.endpoints import PagesAPIEndpoint
|
||||
from wagtail.api.v2.endpoints import PagesAPIViewSet
|
||||
from wagtail.api.v2.router import WagtailAPIRouter
|
||||
from wagtail.images.api.v2.endpoints import ImagesAPIEndpoint
|
||||
from wagtail.documents.api.v2.endpoints import DocumentsAPIEndpoint
|
||||
from wagtail.images.api.v2.endpoints import ImagesAPIViewSet
|
||||
from wagtail.documents.api.v2.endpoints import DocumentsAPIViewSet
|
||||
|
||||
# Create the router. "wagtailapi" is the URL namespace
|
||||
api_router = WagtailAPIRouter('wagtailapi')
|
||||
|
@ -72,9 +72,9 @@ types in their default configuration:
|
|||
# The first parameter is the name of the endpoint (eg. pages, images). This
|
||||
# is used in the URL of the endpoint
|
||||
# The second parameter is the endpoint class that handles the requests
|
||||
api_router.register_endpoint('pages', PagesAPIEndpoint)
|
||||
api_router.register_endpoint('images', ImagesAPIEndpoint)
|
||||
api_router.register_endpoint('documents', DocumentsAPIEndpoint)
|
||||
api_router.register_endpoint('pages', PagesAPIViewSet)
|
||||
api_router.register_endpoint('images', ImagesAPIViewSet)
|
||||
api_router.register_endpoint('documents', DocumentsAPIViewSet)
|
||||
|
||||
Next, register the URLs so Django can route requests into the API:
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ from collections import OrderedDict
|
|||
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
||||
from wagtail.api.v2.endpoints import PagesAPIEndpoint
|
||||
from wagtail.api.v2.endpoints import PagesAPIViewSet
|
||||
from wagtail.api.v2.filters import (
|
||||
ChildOfFilter, DescendantOfFilter, FieldsFilter, ForExplorerFilter, OrderingFilter,
|
||||
SearchFilter)
|
||||
|
@ -13,7 +13,7 @@ from .filters import HasChildrenFilter
|
|||
from .serializers import AdminPageSerializer
|
||||
|
||||
|
||||
class PagesAdminAPIEndpoint(PagesAPIEndpoint):
|
||||
class PagesAdminAPIViewSet(PagesAPIViewSet):
|
||||
base_serializer_class = AdminPageSerializer
|
||||
authentication_classes = [SessionAuthentication]
|
||||
|
||||
|
@ -29,7 +29,7 @@ class PagesAdminAPIEndpoint(PagesAPIEndpoint):
|
|||
SearchFilter,
|
||||
]
|
||||
|
||||
meta_fields = PagesAPIEndpoint.meta_fields + [
|
||||
meta_fields = PagesAPIViewSet.meta_fields + [
|
||||
'latest_revision_created_at',
|
||||
'status',
|
||||
'children',
|
||||
|
@ -38,11 +38,11 @@ class PagesAdminAPIEndpoint(PagesAPIEndpoint):
|
|||
'ancestors',
|
||||
]
|
||||
|
||||
body_fields = PagesAPIEndpoint.body_fields + [
|
||||
body_fields = PagesAPIViewSet.body_fields + [
|
||||
'admin_display_title',
|
||||
]
|
||||
|
||||
listing_default_fields = PagesAPIEndpoint.listing_default_fields + [
|
||||
listing_default_fields = PagesAPIViewSet.listing_default_fields + [
|
||||
'latest_revision_created_at',
|
||||
'status',
|
||||
'children',
|
||||
|
@ -52,7 +52,7 @@ class PagesAdminAPIEndpoint(PagesAPIEndpoint):
|
|||
# Allow the parent field to appear on listings
|
||||
detail_only_fields = []
|
||||
|
||||
known_query_parameters = PagesAPIEndpoint.known_query_parameters.union([
|
||||
known_query_parameters = PagesAPIViewSet.known_query_parameters.union([
|
||||
'for_explorer',
|
||||
'has_children'
|
||||
])
|
||||
|
|
|
@ -3,10 +3,10 @@ from django.conf.urls import url
|
|||
from wagtail.api.v2.router import WagtailAPIRouter
|
||||
from wagtail.core import hooks
|
||||
|
||||
from .endpoints import PagesAdminAPIEndpoint
|
||||
from .endpoints import PagesAdminAPIViewSet
|
||||
|
||||
admin_api = WagtailAPIRouter('wagtailadmin_api_v1')
|
||||
admin_api.register_endpoint('pages', PagesAdminAPIEndpoint)
|
||||
admin_api.register_endpoint('pages', PagesAdminAPIViewSet)
|
||||
|
||||
for fn in hooks.get_hooks('construct_admin_api'):
|
||||
fn(admin_api)
|
||||
|
|
|
@ -24,7 +24,7 @@ from .utils import (
|
|||
parse_fields_parameter)
|
||||
|
||||
|
||||
class BaseAPIEndpoint(GenericViewSet):
|
||||
class BaseAPIViewSet(GenericViewSet):
|
||||
renderer_classes = [JSONRenderer, BrowsableAPIRenderer]
|
||||
|
||||
pagination_class = WagtailPagination
|
||||
|
@ -269,7 +269,7 @@ class BaseAPIEndpoint(GenericViewSet):
|
|||
# Get a serializer class for the related object
|
||||
child_model = django_field.related_model
|
||||
child_endpoint_class = router.get_model_endpoint(child_model)
|
||||
child_endpoint_class = child_endpoint_class[1] if child_endpoint_class else BaseAPIEndpoint
|
||||
child_endpoint_class = child_endpoint_class[1] if child_endpoint_class else BaseAPIViewSet
|
||||
child_serializer_classes[field_name] = child_endpoint_class._get_serializer_class(router, child_model, child_sub_fields, nested=True)
|
||||
|
||||
else:
|
||||
|
@ -362,7 +362,7 @@ class BaseAPIEndpoint(GenericViewSet):
|
|||
return reverse(url_name, args=(pk, ))
|
||||
|
||||
|
||||
class PagesAPIEndpoint(BaseAPIEndpoint):
|
||||
class PagesAPIViewSet(BaseAPIViewSet):
|
||||
base_serializer_class = PageSerializer
|
||||
filter_backends = [
|
||||
FieldsFilter,
|
||||
|
@ -371,15 +371,15 @@ class PagesAPIEndpoint(BaseAPIEndpoint):
|
|||
OrderingFilter,
|
||||
SearchFilter
|
||||
]
|
||||
known_query_parameters = BaseAPIEndpoint.known_query_parameters.union([
|
||||
known_query_parameters = BaseAPIViewSet.known_query_parameters.union([
|
||||
'type',
|
||||
'child_of',
|
||||
'descendant_of',
|
||||
])
|
||||
body_fields = BaseAPIEndpoint.body_fields + [
|
||||
body_fields = BaseAPIViewSet.body_fields + [
|
||||
'title',
|
||||
]
|
||||
meta_fields = BaseAPIEndpoint.meta_fields + [
|
||||
meta_fields = BaseAPIViewSet.meta_fields + [
|
||||
'html_url',
|
||||
'slug',
|
||||
'show_in_menus',
|
||||
|
@ -388,13 +388,13 @@ class PagesAPIEndpoint(BaseAPIEndpoint):
|
|||
'first_published_at',
|
||||
'parent',
|
||||
]
|
||||
listing_default_fields = BaseAPIEndpoint.listing_default_fields + [
|
||||
listing_default_fields = BaseAPIViewSet.listing_default_fields + [
|
||||
'title',
|
||||
'html_url',
|
||||
'slug',
|
||||
'first_published_at',
|
||||
]
|
||||
nested_default_fields = BaseAPIEndpoint.nested_default_fields + [
|
||||
nested_default_fields = BaseAPIViewSet.nested_default_fields + [
|
||||
'title',
|
||||
]
|
||||
detail_only_fields = ['parent']
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from ..v2.endpoints import DocumentsAPIEndpoint
|
||||
from ..v2.endpoints import DocumentsAPIViewSet
|
||||
|
||||
|
||||
class DocumentsAdminAPIEndpoint(DocumentsAPIEndpoint):
|
||||
class DocumentsAdminAPIViewSet(DocumentsAPIViewSet):
|
||||
pass
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
from wagtail.api.v2.endpoints import BaseAPIEndpoint
|
||||
from wagtail.api.v2.endpoints import BaseAPIViewSet
|
||||
from wagtail.api.v2.filters import FieldsFilter, OrderingFilter, SearchFilter
|
||||
|
||||
from ... import get_document_model
|
||||
from .serializers import DocumentSerializer
|
||||
|
||||
|
||||
class DocumentsAPIEndpoint(BaseAPIEndpoint):
|
||||
class DocumentsAPIViewSet(BaseAPIViewSet):
|
||||
base_serializer_class = DocumentSerializer
|
||||
filter_backends = [FieldsFilter, OrderingFilter, SearchFilter]
|
||||
body_fields = BaseAPIEndpoint.body_fields + ['title']
|
||||
meta_fields = BaseAPIEndpoint.meta_fields + ['tags', 'download_url']
|
||||
listing_default_fields = BaseAPIEndpoint.listing_default_fields + ['title', 'tags', 'download_url']
|
||||
nested_default_fields = BaseAPIEndpoint.nested_default_fields + ['title', 'download_url']
|
||||
body_fields = BaseAPIViewSet.body_fields + ['title']
|
||||
meta_fields = BaseAPIViewSet.meta_fields + ['tags', 'download_url']
|
||||
listing_default_fields = BaseAPIViewSet.listing_default_fields + ['title', 'tags', 'download_url']
|
||||
nested_default_fields = BaseAPIViewSet.nested_default_fields + ['title', 'download_url']
|
||||
name = 'documents'
|
||||
model = get_document_model()
|
||||
|
|
|
@ -17,7 +17,7 @@ from wagtail.core import hooks
|
|||
from wagtail.core.models import BaseViewRestriction
|
||||
from wagtail.core.wagtail_hooks import require_wagtail_login
|
||||
from wagtail.documents import admin_urls, get_document_model
|
||||
from wagtail.documents.api.admin.endpoints import DocumentsAdminAPIEndpoint
|
||||
from wagtail.documents.api.admin.endpoints import DocumentsAdminAPIViewSet
|
||||
from wagtail.documents.forms import GroupDocumentPermissionFormSet
|
||||
from wagtail.documents.permissions import permission_policy
|
||||
from wagtail.documents.rich_text import DocumentLinkHandler
|
||||
|
@ -34,7 +34,7 @@ def register_admin_urls():
|
|||
|
||||
@hooks.register('construct_admin_api')
|
||||
def construct_admin_api(router):
|
||||
router.register_endpoint('documents', DocumentsAdminAPIEndpoint)
|
||||
router.register_endpoint('documents', DocumentsAdminAPIViewSet)
|
||||
|
||||
|
||||
class DocumentsMenuItem(MenuItem):
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
from ..v2.endpoints import ImagesAPIEndpoint
|
||||
from ..v2.endpoints import ImagesAPIViewSet
|
||||
from .serializers import AdminImageSerializer
|
||||
|
||||
|
||||
class ImagesAdminAPIEndpoint(ImagesAPIEndpoint):
|
||||
class ImagesAdminAPIViewSet(ImagesAPIViewSet):
|
||||
base_serializer_class = AdminImageSerializer
|
||||
|
||||
body_fields = ImagesAPIEndpoint.body_fields + [
|
||||
body_fields = ImagesAPIViewSet.body_fields + [
|
||||
'thumbnail',
|
||||
]
|
||||
|
||||
listing_default_fields = ImagesAPIEndpoint.listing_default_fields + [
|
||||
listing_default_fields = ImagesAPIViewSet.listing_default_fields + [
|
||||
'width',
|
||||
'height',
|
||||
'thumbnail',
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
from wagtail.api.v2.endpoints import BaseAPIEndpoint
|
||||
from wagtail.api.v2.endpoints import BaseAPIViewSet
|
||||
from wagtail.api.v2.filters import FieldsFilter, OrderingFilter, SearchFilter
|
||||
|
||||
from ... import get_image_model
|
||||
from .serializers import ImageSerializer
|
||||
|
||||
|
||||
class ImagesAPIEndpoint(BaseAPIEndpoint):
|
||||
class ImagesAPIViewSet(BaseAPIViewSet):
|
||||
base_serializer_class = ImageSerializer
|
||||
filter_backends = [FieldsFilter, OrderingFilter, SearchFilter]
|
||||
body_fields = BaseAPIEndpoint.body_fields + ['title', 'width', 'height']
|
||||
meta_fields = BaseAPIEndpoint.meta_fields + ['tags', 'download_url']
|
||||
listing_default_fields = BaseAPIEndpoint.listing_default_fields + ['title', 'tags', 'download_url']
|
||||
nested_default_fields = BaseAPIEndpoint.nested_default_fields + ['title', 'download_url']
|
||||
body_fields = BaseAPIViewSet.body_fields + ['title', 'width', 'height']
|
||||
meta_fields = BaseAPIViewSet.meta_fields + ['tags', 'download_url']
|
||||
listing_default_fields = BaseAPIViewSet.listing_default_fields + ['title', 'tags', 'download_url']
|
||||
nested_default_fields = BaseAPIViewSet.nested_default_fields + ['title', 'download_url']
|
||||
name = 'images'
|
||||
model = get_image_model()
|
||||
|
|
|
@ -13,7 +13,7 @@ from wagtail.admin.site_summary import SummaryItem
|
|||
from wagtail.admin.staticfiles import versioned_static
|
||||
from wagtail.core import hooks
|
||||
from wagtail.images import admin_urls, get_image_model, image_operations
|
||||
from wagtail.images.api.admin.endpoints import ImagesAdminAPIEndpoint
|
||||
from wagtail.images.api.admin.endpoints import ImagesAdminAPIViewSet
|
||||
from wagtail.images.forms import GroupImagePermissionFormSet
|
||||
from wagtail.images.permissions import permission_policy
|
||||
from wagtail.images.rich_text import ImageEmbedHandler
|
||||
|
@ -30,7 +30,7 @@ def register_admin_urls():
|
|||
|
||||
@hooks.register('construct_admin_api')
|
||||
def construct_admin_api(router):
|
||||
router.register_endpoint('images', ImagesAdminAPIEndpoint)
|
||||
router.register_endpoint('images', ImagesAdminAPIViewSet)
|
||||
|
||||
|
||||
class ImagesMenuItem(MenuItem):
|
||||
|
|
|
@ -2,23 +2,23 @@ from django.conf.urls import include, url
|
|||
from django.http import HttpResponse
|
||||
|
||||
from wagtail.admin import urls as wagtailadmin_urls
|
||||
from wagtail.api.v2.endpoints import PagesAPIEndpoint
|
||||
from wagtail.api.v2.endpoints import PagesAPIViewSet
|
||||
from wagtail.api.v2.router import WagtailAPIRouter
|
||||
from wagtail.contrib.sitemaps import views as sitemaps_views
|
||||
from wagtail.contrib.sitemaps import Sitemap
|
||||
from wagtail.core import urls as wagtail_urls
|
||||
from wagtail.documents import urls as wagtaildocs_urls
|
||||
from wagtail.documents.api.v2.endpoints import DocumentsAPIEndpoint
|
||||
from wagtail.documents.api.v2.endpoints import DocumentsAPIViewSet
|
||||
from wagtail.images import urls as wagtailimages_urls
|
||||
from wagtail.images.api.v2.endpoints import ImagesAPIEndpoint
|
||||
from wagtail.images.api.v2.endpoints import ImagesAPIViewSet
|
||||
from wagtail.images.tests import urls as wagtailimages_test_urls
|
||||
from wagtail.tests.testapp import urls as testapp_urls
|
||||
from wagtail.tests.testapp.models import EventSitemap
|
||||
|
||||
api_router = WagtailAPIRouter('wagtailapi_v2')
|
||||
api_router.register_endpoint('pages', PagesAPIEndpoint)
|
||||
api_router.register_endpoint('images', ImagesAPIEndpoint)
|
||||
api_router.register_endpoint('documents', DocumentsAPIEndpoint)
|
||||
api_router.register_endpoint('pages', PagesAPIViewSet)
|
||||
api_router.register_endpoint('images', ImagesAPIViewSet)
|
||||
api_router.register_endpoint('documents', DocumentsAPIViewSet)
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
|
|
Ładowanie…
Reference in New Issue