From 2a93c05bc27b5ca0be9fce8ae6bbe11010172dff Mon Sep 17 00:00:00 2001 From: Lorenz Padberg Date: Mon, 16 May 2022 16:57:21 +0200 Subject: [PATCH] added wagtail v2 api --- server/config/api.py | 16 +++++++++++++ server/config/settings/base.py | 1 + server/config/urls.py | 4 ++++ server/vbv_lernwelt/learnpath/models.py | 32 +++++++++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 server/config/api.py diff --git a/server/config/api.py b/server/config/api.py new file mode 100644 index 00000000..1647ee43 --- /dev/null +++ b/server/config/api.py @@ -0,0 +1,16 @@ + +from wagtail.api.v2.views import PagesAPIViewSet +from wagtail.api.v2.router import WagtailAPIRouter +from wagtail.images.api.v2.views import ImagesAPIViewSet +from wagtail.documents.api.v2.views import DocumentsAPIViewSet + +# Create the router. "wagtailapi" is the URL namespace +api_router = WagtailAPIRouter('wagtailapi') + +# Add the three endpoints using the "register_endpoint" method. +# 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', PagesAPIViewSet) +api_router.register_endpoint('images', ImagesAPIViewSet) +api_router.register_endpoint('documents', DocumentsAPIViewSet) diff --git a/server/config/settings/base.py b/server/config/settings/base.py index 38375a65..05b64b2c 100644 --- a/server/config/settings/base.py +++ b/server/config/settings/base.py @@ -99,6 +99,7 @@ THIRD_PARTY_APPS = [ 'wagtail.admin', 'wagtail.core', 'wagtail.locales', + 'wagtail.api.v2', 'modelcluster', 'taggit', diff --git a/server/config/urls.py b/server/config/urls.py index d9f489b5..6ed1ab7a 100644 --- a/server/config/urls.py +++ b/server/config/urls.py @@ -21,6 +21,8 @@ from wagtail.admin import urls as wagtailadmin_urls from wagtail.core import urls as wagtail_urls from wagtail.documents import urls as wagtaildocs_urls from grapple import urls as grapple_urls +from .api import api_router + def raise_example_error(request): @@ -57,6 +59,8 @@ if settings.ALLOW_LOCAL_LOGIN: urlpatterns += [ # API base url path("api/", include("config.api_router")), + path('api/v2/', api_router.urls), + # DRF auth token path("auth-token/", obtain_auth_token), path("api/schema/", SpectacularAPIView.as_view(), name="api-schema"), diff --git a/server/vbv_lernwelt/learnpath/models.py b/server/vbv_lernwelt/learnpath/models.py index 179f72c2..55748138 100644 --- a/server/vbv_lernwelt/learnpath/models.py +++ b/server/vbv_lernwelt/learnpath/models.py @@ -4,6 +4,7 @@ from django.utils.text import slugify from wagtail.core.blocks import StreamBlock from wagtail.core.fields import StreamField from wagtail.core.models import Page, Orderable +from wagtail.api import APIField from vbv_lernwelt.learnpath.models_competences import * from vbv_lernwelt.learnpath.models_learning_unit_content import WebBasedTrainingBlock, VideoBlock @@ -57,6 +58,12 @@ class Topic(Orderable): GraphQLBoolean("is_visible"), ] + api_fields = [ + APIField('title'), + APIField('is_visible'), + ] + + # content_panels = Page.content_panels + [ # FieldPanel('is_visible', classname="full"), # PageChooserPanel('learning_path', 'learnpath.LearningPath'), @@ -102,6 +109,13 @@ class Circle(Page, Orderable): GraphQLString("goals"), ] + # Export fields over the API + api_fields = [ + APIField('title'), + APIField('description'), + APIField('learning_sequences'), + ] + def full_clean(self, *args, **kwargs): self.slug = find_available_slug(Circle, slugify(self.title, allow_unicode=True)) @@ -147,6 +161,12 @@ class LearningSequence(Orderable): GraphQLBoolean("category"), ] + api_fields = [ + APIField('title'), + APIField('category'), + APIField('learning_packages'), + ] + class Meta: verbose_name = "Learning Sequence" @@ -176,6 +196,11 @@ class LearningPackage(Orderable): GraphQLString("title", required=False), ] + api_fields = [ + APIField('title'), + APIField('learning_units'), + ] + def full_clean(self, *args, **kwargs): self.slug = find_available_slug(LearningPackage, slugify(self.title, allow_unicode=True)) @@ -216,6 +241,13 @@ class LearningUnit(Page, Orderable): GraphQLStreamfield('contents') ] + + api_fields = [ + APIField('title'), + + + ] + subpage_types = [] class Meta: