Add image_block serialization suitable for a SPA, i.e. return path, not the internal id of an image

This commit is contained in:
Pawel Kowalski 2018-08-16 15:00:37 +02:00
parent 4b03a702fe
commit a24acad07a
1 changed files with 30 additions and 1 deletions

View File

@ -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)