misc: use native 404 page if ~all pages are private (#41514)

This commit is contained in:
Frédéric Péters 2020-04-09 08:30:58 +02:00
parent 14aa0b62fe
commit a40dc2e73f
2 changed files with 14 additions and 1 deletions

View File

@ -36,6 +36,7 @@ from django.utils.encoding import force_text
from django.utils.six.moves.urllib import parse as urlparse
from django.utils.six.moves.urllib import parse as urllib
from django.views.decorators.csrf import csrf_exempt
from django.views.defaults import page_not_found
from django.utils.translation import ugettext as _
from django.forms.widgets import Media
@ -554,7 +555,11 @@ def error404(request, *args, **kwargs):
if not hasattr(request, 'user'):
# this happens when the 404 handler is called early on, for example
# when the given hostname doesn't exist as a tenant
from django.views.defaults import page_not_found
return page_not_found(request, *args, **kwargs)
if Page.objects.exists() and all((not x.is_visible(request.user) for x in Page.objects.filter(parent_id__isnull=True))):
# if none of the first-level pages can be viewed by the user, display
# native django error page.
return page_not_found(request, *args, **kwargs)
try:

View File

@ -474,6 +474,14 @@ def test_404(app):
resp = app.get('/foobar/', status=404)
assert "can't find the requested page" in resp.text
# check native django handler is used if all pages are private
page.public = False
page.save()
resp = app.get('/foobar/', status=404)
assert 'Custom 404 Text' not in resp.text
assert "This page doesn't exist" not in resp.text
def test_style_demo(app, admin_user):
TextCell.objects.all().delete()
Page.objects.all().delete()