Add `alias_of` field into pages API

pull/7749/head
Dmitrii Faiazov 2021-12-02 15:36:55 +02:00 zatwierdzone przez GitHub
rodzic c6b985770d
commit b314f28f37
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
5 zmienionych plików z 30 dodań i 6 usunięć

Wyświetl plik

@ -585,6 +585,10 @@ Pages
Nests some information about the parent page (only available on detail
views)
``meta.alias_of`` (dictionary)
If the page marked as an alias return original page id and full url
Images
------

Wyświetl plik

@ -173,7 +173,7 @@ class TestAdminPageListing(AdminAPITestCase, TestPageListing):
for page in content['items']:
self.assertEqual(set(page.keys()), {'id', 'meta', 'title', 'admin_display_title', 'date', 'related_links', 'tags', 'carousel_items', 'body', 'feed_image', 'feed_image_thumbnail'})
self.assertEqual(set(page['meta'].keys()), {'type', 'detail_url', 'show_in_menus', 'first_published_at', 'seo_title', 'slug', 'parent', 'html_url', 'search_description', 'locale', 'children', 'descendants', 'ancestors', 'translations', 'status', 'latest_revision_created_at'})
self.assertEqual(set(page['meta'].keys()), {'type', 'detail_url', 'show_in_menus', 'first_published_at', 'seo_title', 'slug', 'parent', 'html_url', 'search_description', 'locale', 'alias_of', 'children', 'descendants', 'ancestors', 'translations', 'status', 'latest_revision_created_at'})
def test_all_fields_then_remove_something(self):
response = self.get_response(type='demosite.BlogEntryPage', fields='*,-title,-admin_display_title,-date,-seo_title,-status')
@ -181,7 +181,7 @@ class TestAdminPageListing(AdminAPITestCase, TestPageListing):
for page in content['items']:
self.assertEqual(set(page.keys()), {'id', 'meta', 'related_links', 'tags', 'carousel_items', 'body', 'feed_image', 'feed_image_thumbnail'})
self.assertEqual(set(page['meta'].keys()), {'type', 'detail_url', 'show_in_menus', 'first_published_at', 'slug', 'parent', 'html_url', 'search_description', 'locale', 'children', 'descendants', 'ancestors', 'translations', 'latest_revision_created_at'})
self.assertEqual(set(page['meta'].keys()), {'type', 'detail_url', 'show_in_menus', 'first_published_at', 'slug', 'parent', 'html_url', 'search_description', 'locale', 'alias_of', 'children', 'descendants', 'ancestors', 'translations', 'latest_revision_created_at'})
def test_all_nested_fields(self):
response = self.get_response(type='demosite.BlogEntryPage', fields='feed_image(*)')
@ -699,7 +699,7 @@ class TestAdminPageDetail(AdminAPITestCase, TestPageDetail):
# FIELDS
def test_remove_all_meta_fields(self):
response = self.get_response(16, fields='-type,-detail_url,-slug,-first_published_at,-html_url,-descendants,-latest_revision_created_at,-children,-ancestors,-show_in_menus,-seo_title,-parent,-status,-search_description')
response = self.get_response(16, fields='-type,-detail_url,-slug,-first_published_at,-html_url,-descendants,-latest_revision_created_at,-alias_of,-children,-ancestors,-show_in_menus,-seo_title,-parent,-status,-search_description')
content = json.loads(response.content.decode('UTF-8'))
self.assertNotIn('meta', set(content.keys()))

Wyświetl plik

@ -141,6 +141,23 @@ class PageParentField(relations.RelatedField):
return serializer.to_representation(value)
class PageAliasOfField(relations.RelatedField):
"""
Serializes the "alias_of" field on Page objects.
"""
def get_attribute(self, instance):
return instance
def to_representation(self, page):
if page.alias_of is None:
return None
data = OrderedDict()
data['id'] = page.alias_of.id
data['full_url'] = page.alias_of.full_url
return data
class ChildRelationField(Field):
"""
Serializes child relations.
@ -327,6 +344,7 @@ class PageSerializer(BaseSerializer):
locale = PageLocaleField(read_only=True)
html_url = PageHtmlUrlField(read_only=True)
parent = PageParentField(read_only=True)
alias_of = PageAliasOfField(read_only=True)
def build_relational_field(self, field_name, relation_info):
# Find all relation fields that point to child class and make them use

Wyświetl plik

@ -300,7 +300,7 @@ class TestPageListing(TestCase, WagtailTestUtils):
for page in content['items']:
self.assertEqual(set(page.keys()), {'id', 'meta', 'title', 'date', 'related_links', 'tags', 'carousel_items', 'body', 'feed_image', 'feed_image_thumbnail'})
self.assertEqual(set(page['meta'].keys()), {'type', 'detail_url', 'show_in_menus', 'first_published_at', 'seo_title', 'slug', 'html_url', 'search_description', 'locale'})
self.assertEqual(set(page['meta'].keys()), {'type', 'detail_url', 'show_in_menus', 'first_published_at', 'alias_of', 'seo_title', 'slug', 'html_url', 'search_description', 'locale'})
def test_all_fields_then_remove_something(self):
response = self.get_response(type='demosite.BlogEntryPage', fields='*,-title,-date,-seo_title')
@ -308,7 +308,7 @@ class TestPageListing(TestCase, WagtailTestUtils):
for page in content['items']:
self.assertEqual(set(page.keys()), {'id', 'meta', 'related_links', 'tags', 'carousel_items', 'body', 'feed_image', 'feed_image_thumbnail'})
self.assertEqual(set(page['meta'].keys()), {'type', 'detail_url', 'show_in_menus', 'first_published_at', 'slug', 'html_url', 'search_description', 'locale'})
self.assertEqual(set(page['meta'].keys()), {'type', 'detail_url', 'show_in_menus', 'first_published_at', 'alias_of', 'slug', 'html_url', 'search_description', 'locale'})
def test_remove_all_fields(self):
response = self.get_response(type='demosite.BlogEntryPage', fields='_,id,type')
@ -1064,6 +1064,7 @@ class TestPageDetail(TestCase):
'seo_title',
'search_description',
'first_published_at',
'alias_of',
'parent',
]
self.assertEqual(list(content['meta'].keys()), meta_field_order)
@ -1113,7 +1114,7 @@ class TestPageDetail(TestCase):
self.assertNotIn('html_url', set(content['meta'].keys()))
def test_remove_all_meta_fields(self):
response = self.get_response(16, fields='-type,-detail_url,-slug,-first_published_at,-html_url,-search_description,-show_in_menus,-parent,-seo_title')
response = self.get_response(16, fields='-type,-detail_url,-slug,-first_published_at,-alias_of,-html_url,-search_description,-show_in_menus,-parent,-seo_title')
content = json.loads(response.content.decode('UTF-8'))
self.assertIn('id', set(content.keys()))

Wyświetl plik

@ -391,6 +391,7 @@ class PagesAPIViewSet(BaseAPIViewSet):
'seo_title',
'search_description',
'first_published_at',
'alias_of',
'parent',
'locale',
]