0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Updated to use APIField class in api_fields

Thanks to @BertrandBordage for the suggestion!
This commit is contained in:
Karl Hobley 2017-03-30 11:08:33 +01:00
parent 268899b471
commit 161d55565b
4 changed files with 32 additions and 15 deletions

View File

@ -1 +1,4 @@
from .conf import APIField # noqa
default_app_config = 'wagtail.api.apps.WagtailAPIAppConfig'

13
wagtail/api/conf.py Normal file
View File

@ -0,0 +1,13 @@
from __future__ import absolute_import, unicode_literals
class APIField(object):
def __init__(self, name, serializer=None):
self.name = name
self.serializer = serializer
def __hash__(self):
return hash(self.name)
def __repr__(self):
return '<APIField {}>'.format(self.name)

View File

@ -13,6 +13,7 @@ from rest_framework.renderers import BrowsableAPIRenderer, JSONRenderer
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet
from wagtail.api import APIField
from wagtail.wagtailcore.models import Page
from .filters import (
@ -103,7 +104,7 @@ class BaseAPIEndpoint(GenericViewSet):
if hasattr(model, 'api_fields'):
fields.extend([
field[0] if isinstance(field, tuple) else field
field.name if isinstance(field, APIField) else field
for field in model.api_fields
])
@ -119,7 +120,7 @@ class BaseAPIEndpoint(GenericViewSet):
if hasattr(model, 'api_meta_fields'):
meta_fields.extend([
field[0] if isinstance(field, tuple) else field
field.name if isinstance(field, APIField) else field
for field in model.api_meta_fields
])
@ -131,16 +132,16 @@ class BaseAPIEndpoint(GenericViewSet):
if hasattr(model, 'api_fields'):
configs.update({
field[0]: field[1]
field.name: field.serializer
for field in model.api_fields
if isinstance(field, tuple)
if isinstance(field, APIField) and field.serializer is not None
})
if hasattr(model, 'api_meta_fields'):
configs.update({
field[0]: field[1]
field.name: field.serializer
for field in model.api_meta_fields
if isinstance(field, tuple)
if isinstance(field, APIField) and field.serializer is not None
})
return configs

View File

@ -7,17 +7,17 @@ from modelcluster.contrib.taggit import ClusterTaggableManager
from modelcluster.fields import ParentalKey
from taggit.models import TaggedItemBase
from wagtail.api import APIField
from wagtail.utils.pagination import paginate
from wagtail.wagtailadmin.edit_handlers import (
FieldPanel, InlinePanel, MultiFieldPanel, PageChooserPanel)
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailcore.models import Orderable, Page
from wagtail.wagtaildocs.edit_handlers import DocumentChooserPanel
from wagtail.wagtailimages.api.fields import ImageRenditionField
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
from wagtail.wagtailsearch import index
from wagtail.wagtailimages.api.fields import ImageRenditionField
# ABSTRACT MODELS
# =============================
@ -278,13 +278,13 @@ class BlogEntryPage(Page):
)
api_fields = (
'body',
'tags',
'date',
'feed_image',
('feed_image_thumbnail', ImageRenditionField('fill-300x300', source='feed_image')),
'carousel_items',
'related_links',
APIField('body'),
APIField('tags'),
APIField('date'),
APIField('feed_image'),
APIField('feed_image_thumbnail', serializer=ImageRenditionField('fill-300x300', source='feed_image')),
APIField('carousel_items'),
APIField('related_links'),
)
search_fields = Page.search_fields + [