kopia lustrzana https://github.com/wagtail/wagtail
Merge branch 'fix/streamfield-api'
commit
1ac4b822ed
|
@ -10,6 +10,7 @@ from taggit.managers import _TaggableManager
|
|||
from taggit.models import Tag
|
||||
|
||||
from wagtail.utils.urlpatterns import decorate_urlpatterns
|
||||
from wagtail.wagtailcore.blocks import StreamValue
|
||||
|
||||
from .endpoints import URLPath, ObjectDetailURL, PagesAPIEndpoint, ImagesAPIEndpoint, DocumentsAPIEndpoint
|
||||
from .utils import BadRequestError, get_base_url
|
||||
|
@ -47,6 +48,8 @@ class API(object):
|
|||
return get_full_url(request, reverse(view, args=(o.pk, )))
|
||||
else:
|
||||
return None
|
||||
elif isinstance(o, StreamValue):
|
||||
return o.stream_block.get_prep_value(o)
|
||||
else:
|
||||
return super(WagtailAPIJSONEncoder, self).default(o)
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ from wagtail.wagtailcore.models import Page
|
|||
from wagtail.contrib.wagtailapi import signal_handlers
|
||||
|
||||
from wagtail.tests.demosite import models
|
||||
from wagtail.tests.testapp.models import StreamPage
|
||||
|
||||
|
||||
def get_total_page_count():
|
||||
|
@ -594,6 +595,47 @@ class TestPageDetail(TestCase):
|
|||
self.assertEqual(content['feed_image'], None)
|
||||
|
||||
|
||||
class TestPageDetailWithStreamField(TestCase):
|
||||
fixtures = ['test.json']
|
||||
|
||||
def setUp(self):
|
||||
self.homepage = Page.objects.get(url_path='/home/')
|
||||
|
||||
def make_stream_page(self, body):
|
||||
stream_page = StreamPage(
|
||||
title='stream page',
|
||||
slug='stream-page',
|
||||
body=body
|
||||
)
|
||||
return self.homepage.add_child(instance=stream_page)
|
||||
|
||||
def test_can_fetch_streamfield_content(self):
|
||||
stream_page = self.make_stream_page('[{"type": "text", "value": "foo"}]')
|
||||
|
||||
response_url = reverse('wagtailapi_v1:pages:detail', args=(stream_page.id, ))
|
||||
response = self.client.get(response_url)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response['content-type'], 'application/json')
|
||||
|
||||
content = json.loads(response.content.decode('utf-8'))
|
||||
|
||||
self.assertIn('id', content)
|
||||
self.assertEqual(content['id'], stream_page.id)
|
||||
self.assertIn('body', content)
|
||||
self.assertEqual(content['body'], [{'type': 'text', 'value': 'foo'}])
|
||||
|
||||
def test_image_block(self):
|
||||
stream_page = self.make_stream_page('[{"type": "image", "value": 1}]')
|
||||
|
||||
response_url = reverse('wagtailapi_v1:pages:detail', args=(stream_page.id, ))
|
||||
response = self.client.get(response_url)
|
||||
content = json.loads(response.content.decode('utf-8'))
|
||||
|
||||
# ForeignKeys in a StreamField shouldn't be translated into dictionary representation
|
||||
self.assertEqual(content['body'], [{'type': 'image', 'value': 1}])
|
||||
|
||||
|
||||
@override_settings(
|
||||
WAGTAILFRONTENDCACHE={
|
||||
'varnish': {
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
import wagtail.wagtailcore.fields
|
||||
import wagtail.wagtailcore.blocks
|
||||
import wagtail.wagtailimages.blocks
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('wagtailcore', '0001_squashed_0016_change_page_url_path_to_text_field'),
|
||||
('tests', '0004_streammodel_richtext'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='StreamPage',
|
||||
fields=[
|
||||
('page_ptr', models.OneToOneField(auto_created=True, to='wagtailcore.Page', serialize=False, parent_link=True, primary_key=True)),
|
||||
('body', wagtail.wagtailcore.fields.StreamField((('text', wagtail.wagtailcore.blocks.CharBlock()), ('rich_text', wagtail.wagtailcore.blocks.RichTextBlock()), ('image', wagtail.wagtailimages.blocks.ImageChooserBlock())))),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
bases=('wagtailcore.page',),
|
||||
),
|
||||
]
|
|
@ -410,3 +410,13 @@ class StreamModel(models.Model):
|
|||
('rich_text', RichTextBlock()),
|
||||
('image', ImageChooserBlock()),
|
||||
])
|
||||
|
||||
|
||||
class StreamPage(Page):
|
||||
body = StreamField([
|
||||
('text', CharBlock()),
|
||||
('rich_text', RichTextBlock()),
|
||||
('image', ImageChooserBlock()),
|
||||
])
|
||||
|
||||
api_fields = ('body',)
|
||||
|
|
Ładowanie…
Reference in New Issue