From a24acad07a16fc0e1e6a6485eb18794471d426d4 Mon Sep 17 00:00:00 2001 From: Pawel Kowalski Date: Thu, 16 Aug 2018 15:00:37 +0200 Subject: [PATCH] Add image_block serialization suitable for a SPA, i.e. return path, not the internal id of an image --- server/api/graphene_wagtail.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/server/api/graphene_wagtail.py b/server/api/graphene_wagtail.py index d283b851..51ee130a 100644 --- a/server/api/graphene_wagtail.py +++ b/server/api/graphene_wagtail.py @@ -4,12 +4,41 @@ from graphene.types import Scalar from graphene_django.converter import convert_django_field from wagtail.core.fields import StreamField +from wagtail.images.models import Image class GenericStreamFieldType(Scalar): @staticmethod def serialize(stream_value): - return stream_value.stream_data + stream_data = stream_value.stream_data + + for d in stream_data: + if isinstance(d, dict): + _type = d['type'] + if _type == 'image_block': + _value = d['value'] + value = { + # 'value': _value, + # 'id': d['id'], + 'path': Image.objects.get(id=_value).file.url + } + d['value'] = value + + # value = dict(d['value']) + # if 'document' in value: + # value['document'] = Document.objects.get(id=value['document']).file.url + # if 'image' in value: + # value['image'] = Image.objects.get(id=value['image']).file.url + + # else: + # _type = d[0] + # value = dict(d[1]) + # if 'document' in value: + # value['document'] = value['document'].file.url + # if 'image' in value: + # value['image'] = value['image'].file.url + + return stream_data @convert_django_field.register(StreamField)