mirror of
https://github.com/wagtail/wagtail.git
synced 2024-12-01 11:41:20 +01:00
Don't show all fields by default
This commit is contained in:
parent
782f7d1a03
commit
4df84ef231
@ -55,6 +55,7 @@ class BaseAPIEndpoint(GenericViewSet):
|
||||
])
|
||||
extra_body_fields = []
|
||||
extra_meta_fields = []
|
||||
default_fields = []
|
||||
name = None # Set on subclass.
|
||||
|
||||
def get_queryset(self):
|
||||
@ -109,6 +110,9 @@ class BaseAPIEndpoint(GenericViewSet):
|
||||
def get_available_fields(self, model):
|
||||
return self.get_body_fields(model) + self.get_meta_fields(model)
|
||||
|
||||
def get_default_fields(self, model):
|
||||
return self.default_fields
|
||||
|
||||
def check_query_parameters(self, queryset):
|
||||
"""
|
||||
Ensure that only valid query paramters are included in the URL.
|
||||
@ -143,7 +147,7 @@ class BaseAPIEndpoint(GenericViewSet):
|
||||
if 'fields' in request.GET:
|
||||
fields = set(request.GET['fields'].split(','))
|
||||
else:
|
||||
fields = set(all_fields)
|
||||
fields = set(self.get_default_fields(model))
|
||||
|
||||
unknown_fields = fields - set(all_fields)
|
||||
|
||||
@ -221,6 +225,11 @@ class PagesAPIEndpoint(BaseAPIEndpoint):
|
||||
'search_description',
|
||||
'first_published_at',
|
||||
]
|
||||
default_fields = [
|
||||
'title',
|
||||
'slug',
|
||||
'first_published_at',
|
||||
]
|
||||
name = 'pages'
|
||||
model = Page
|
||||
|
||||
@ -262,6 +271,7 @@ class ImagesAPIEndpoint(BaseAPIEndpoint):
|
||||
filter_backends = [FieldsFilter, OrderingFilter, SearchFilter]
|
||||
extra_body_fields = ['title', 'width', 'height']
|
||||
extra_meta_fields = ['tags']
|
||||
default_fields = ['title', 'tags']
|
||||
name = 'images'
|
||||
model = get_image_model()
|
||||
|
||||
@ -270,6 +280,7 @@ class DocumentsAPIEndpoint(BaseAPIEndpoint):
|
||||
base_serializer_class = DocumentSerializer
|
||||
filter_backends = [FieldsFilter, OrderingFilter, SearchFilter]
|
||||
extra_body_fields = ['title']
|
||||
extra_meta_fields = ['tags']
|
||||
extra_meta_fields = ['tags', ]
|
||||
default_fields = ['title', 'tags']
|
||||
name = 'documents'
|
||||
model = Document
|
||||
|
@ -64,7 +64,7 @@ class TestImageListing(TestCase):
|
||||
content = json.loads(response.content.decode('UTF-8'))
|
||||
|
||||
for image in content['items']:
|
||||
self.assertEqual(set(image.keys()), {'id', 'meta', 'title', 'width', 'height'})
|
||||
self.assertEqual(set(image.keys()), {'id', 'meta', 'title'})
|
||||
self.assertEqual(set(image['meta'].keys()), {'type', 'detail_url', 'tags'})
|
||||
|
||||
def test_fields(self):
|
||||
|
@ -53,11 +53,11 @@ class TestPageListing(TestCase):
|
||||
self.assertIn('items', content)
|
||||
self.assertIsInstance(content['items'], list)
|
||||
|
||||
# Check that each page has a meta section with type, detail_url and html_url attributes
|
||||
# Check that each page has a meta section with type, detail_url, html_url, slug and first_published_at attributes
|
||||
for page in content['items']:
|
||||
self.assertIn('meta', page)
|
||||
self.assertIsInstance(page['meta'], dict)
|
||||
self.assertEqual(set(page['meta'].keys()), {'type', 'detail_url', 'html_url', 'seo_title', 'slug', 'first_published_at', 'show_in_menus', 'search_description'})
|
||||
self.assertEqual(set(page['meta'].keys()), {'type', 'detail_url', 'html_url', 'slug', 'first_published_at'})
|
||||
|
||||
def test_unpublished_pages_dont_appear_in_list(self):
|
||||
total_count = get_total_page_count()
|
||||
@ -92,8 +92,8 @@ class TestPageListing(TestCase):
|
||||
for page in content['items']:
|
||||
self.assertEqual(page['meta']['type'], 'demosite.BlogEntryPage')
|
||||
|
||||
# All fields in specific type available
|
||||
self.assertEqual(set(page.keys()), {'id', 'meta', 'title', 'related_links', 'date', 'body', 'tags', 'feed_image', 'carousel_items'})
|
||||
# No specific fields available by default
|
||||
self.assertEqual(set(page.keys()), {'id', 'meta', 'title'})
|
||||
|
||||
def test_type_filter_total_count(self):
|
||||
response = self.get_response(type='demosite.BlogEntryPage')
|
||||
@ -144,7 +144,8 @@ class TestPageListing(TestCase):
|
||||
content = json.loads(response.content.decode('UTF-8'))
|
||||
|
||||
for page in content['items']:
|
||||
self.assertEqual(set(page.keys()), {'id', 'meta', 'title', 'date', 'related_links', 'feed_image', 'body', 'carousel_items', 'tags'})
|
||||
self.assertEqual(set(page.keys()), {'id', 'meta', 'title'})
|
||||
self.assertEqual(set(page['meta'].keys()), {'type', 'detail_url', 'html_url', 'slug', 'first_published_at'})
|
||||
|
||||
def test_fields(self):
|
||||
response = self.get_response(type='demosite.BlogEntryPage', fields='title,date,feed_image')
|
||||
|
Loading…
Reference in New Issue
Block a user