Add serializer for media_library models
This commit is contained in:
parent
79c0fb8b11
commit
a7f0d79c0f
|
|
@ -0,0 +1,15 @@
|
|||
from rest_framework import serializers
|
||||
|
||||
from vbv_lernwelt.course.models import CourseCategory, Course
|
||||
|
||||
|
||||
class CourseSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Course
|
||||
fields = ['id', 'name', 'category_name']
|
||||
|
||||
|
||||
class CourseCategorySerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = CourseCategory
|
||||
fields = ['id', 'name', 'general',]
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
import structlog
|
||||
from django.views.decorators.cache import cache_page
|
||||
from rest_framework.decorators import api_view
|
||||
from rest_framework.response import Response
|
||||
from wagtail.models import Page
|
||||
|
|
@ -8,7 +7,7 @@ logger = structlog.get_logger(__name__)
|
|||
|
||||
|
||||
@api_view(['GET'])
|
||||
@cache_page(60 * 60 * 8, cache="api_page_cache")
|
||||
# @cache_page(60 * 60 * 8, cache="api_page_cache")
|
||||
def page_api_view(request, slug):
|
||||
try:
|
||||
page = Page.objects.get(slug=slug, locale__language_code='de-CH')
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import wagtail.api.v2.serializers as wagtail_serializers
|
||||
from rest_framework.fields import SerializerMethodField
|
||||
|
||||
from vbv_lernwelt.course.models import CoursePage
|
||||
from vbv_lernwelt.course.serializers import CourseCategorySerializer, CourseSerializer
|
||||
from vbv_lernwelt.learnpath.utils import get_wagtail_type
|
||||
|
||||
|
||||
|
|
@ -17,6 +19,8 @@ class ItTypeField(wagtail_serializers.TypeField):
|
|||
class ItBaseSerializer(wagtail_serializers.BaseSerializer):
|
||||
type = ItTypeField(read_only=True)
|
||||
children = SerializerMethodField()
|
||||
course = SerializerMethodField()
|
||||
course_category = CourseCategorySerializer(read_only=True)
|
||||
|
||||
meta_fields = []
|
||||
|
||||
|
|
@ -30,6 +34,15 @@ class ItBaseSerializer(wagtail_serializers.BaseSerializer):
|
|||
children = _get_children(self.descendants, obj)
|
||||
return [c.specific.get_serializer_class()(c.specific, descendants=self.descendants).data for c in children]
|
||||
|
||||
def get_course(self, obj):
|
||||
if hasattr(obj, 'course'):
|
||||
return CourseSerializer(obj.course).data
|
||||
else:
|
||||
course_parent_page = obj.get_ancestors().exact_type(CoursePage).last()
|
||||
if course_parent_page:
|
||||
return CourseSerializer(course_parent_page.specific.course).data
|
||||
return ''
|
||||
|
||||
|
||||
def _get_descendants(pages, obj):
|
||||
return [c for c in pages if c.path.startswith(obj.path) and c.depth >= obj.depth]
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ from wagtail.documents.models import AbstractDocument, Document
|
|||
from wagtail.models import Page
|
||||
|
||||
from vbv_lernwelt.core.model_utils import find_available_slug
|
||||
from vbv_lernwelt.learnpath.serializer_helpers import get_it_serializer_class
|
||||
from vbv_lernwelt.media_library.content_blocks import MediaContentCollection
|
||||
|
||||
|
||||
|
|
@ -21,6 +22,16 @@ class MediaLibraryPage(Page):
|
|||
self.slug = find_available_slug(slugify(f"{self.get_parent().slug}-media", allow_unicode=True))
|
||||
super(MediaLibraryPage, self).full_clean(*args, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def get_serializer_class(cls):
|
||||
return get_it_serializer_class(
|
||||
cls, [
|
||||
'id', 'title', 'slug', 'type', 'translation_key',
|
||||
'course',
|
||||
'children',
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
class MediaCategoryPage(Page):
|
||||
"""
|
||||
|
|
@ -49,6 +60,16 @@ class MediaCategoryPage(Page):
|
|||
self.slug = find_available_slug(slugify(f"{self.get_parent()}-cat-{self.title}", allow_unicode=True))
|
||||
super(MediaCategoryPage, self).full_clean(*args, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def get_serializer_class(cls):
|
||||
return get_it_serializer_class(
|
||||
cls, field_names=[
|
||||
'id', 'title', 'slug', 'type', 'translation_key',
|
||||
'course_category',
|
||||
'introduction_text', 'description', 'body',
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
class LibraryDocument(AbstractDocument):
|
||||
# Todo: check https://filepreviews.io/
|
||||
|
|
|
|||
Loading…
Reference in New Issue