mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-04-06 08:01:00 +00:00
Fix AttributeError in listing_view endpoint
Fixes #3967 If there was no site configured, the listing views in the API would break with an AttributeError. Now, the following happens if there is no site record: - In the Admin API, all pages are returned but links in the response do not include a domain name - In the Public API, no pages are returned
This commit is contained in:
parent
cc64f40237
commit
e7312a429d
3 changed files with 15 additions and 3 deletions
|
|
@ -396,7 +396,11 @@ class PagesAPIEndpoint(BaseAPIEndpoint):
|
|||
queryset = queryset.public().live()
|
||||
|
||||
# Filter by site
|
||||
queryset = queryset.descendant_of(request.site.root_page, inclusive=True)
|
||||
if request.site:
|
||||
queryset = queryset.descendant_of(request.site.root_page, inclusive=True)
|
||||
else:
|
||||
# No sites configured
|
||||
queryset = queryset.none()
|
||||
|
||||
return queryset
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ from django.urls import reverse
|
|||
from wagtail.api.v2 import signal_handlers
|
||||
from wagtail.tests.demosite import models
|
||||
from wagtail.tests.testapp.models import StreamPage
|
||||
from wagtail.wagtailcore.models import Page
|
||||
from wagtail.wagtailcore.models import Page, Site
|
||||
|
||||
|
||||
def get_total_page_count():
|
||||
|
|
@ -753,6 +753,14 @@ class TestPageListing(TestCase):
|
|||
self.assertEqual(response['Content-type'], 'application/json')
|
||||
self.assertEqual(content['meta']['total_count'], 0)
|
||||
|
||||
# REGRESSION TESTS
|
||||
|
||||
def test_issue_3967(self):
|
||||
# The API crashed whenever the listing view was called without a site configured
|
||||
Site.objects.all().delete()
|
||||
response = self.get_response()
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
|
||||
class TestPageDetail(TestCase):
|
||||
fixtures = ['demosite.json']
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ class BadRequestError(Exception):
|
|||
|
||||
|
||||
def get_base_url(request=None):
|
||||
base_url = getattr(settings, 'WAGTAILAPI_BASE_URL', request.site.root_url if request else None)
|
||||
base_url = getattr(settings, 'WAGTAILAPI_BASE_URL', request.site.root_url if request and request.site else None)
|
||||
|
||||
if base_url:
|
||||
# We only want the scheme and netloc
|
||||
|
|
|
|||
Loading…
Reference in a new issue