From fc1d49da217ae3cb1e229ace4d805fb69bb552b9 Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Mon, 16 Aug 2021 14:15:38 +0200 Subject: [PATCH] Add proper xkey tags to docs 404 pages Since we purge docs pages based on the version they are for, we need to tag the 404 pages with version as well, when available. Without that, any page that had been requested returning a 404 (such as somebody or some tool polling for release notes on a version that hasn't been released yet) would not get purged when new docs are loaded, which results in the 404 staying around even after the actual docs are updated. --- docs/xkey.rst | 4 +++- pgweb/docs/views.py | 21 +++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/docs/xkey.rst b/docs/xkey.rst index 2e4f48ac..cb3d3485 100644 --- a/docs/xkey.rst +++ b/docs/xkey.rst @@ -17,7 +17,9 @@ pgdocs_current Set on all documentation pages that are in the current version at the time of setting. pgdocs_all - Set on documentation pages that are cross-version, such as index pages. + Set on documentation pages that are cross-version, such as index pages, + as well as on 404 pages for versions that is unknown (in case this version + is loaded later). pgdocs_ Set on documentation of the specified version. pgdocs_pdf diff --git a/pgweb/docs/views.py b/pgweb/docs/views.py index 6da47d68..34033e32 100644 --- a/pgweb/docs/views.py +++ b/pgweb/docs/views.py @@ -1,5 +1,5 @@ from django.shortcuts import render, get_object_or_404 -from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect +from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect, HttpResponseNotFound from django.http import HttpResponse, Http404 from pgweb.util.decorators import login_required, allow_frames, content_sources from django.template.defaultfilters import strip_tags @@ -21,6 +21,12 @@ from .models import DocPage, DocPageRedirect from .forms import DocCommentForm +def _versioned_404(msg, version): + r = HttpResponseNotFound(msg) + r['xkey'] = 'pgdocs_{}'.format(version) + return r + + @allow_frames @content_sources('style', "'unsafe-inline'") def docpage(request, version, filename): @@ -33,7 +39,7 @@ def docpage(request, version, filename): else: ver = Decimal(version) if ver == Decimal(0): - raise Http404("Version not found") + return _versioned_404("Version not found", "all") if ver < Decimal("7.1") and ver > Decimal(0): extension = "htm" @@ -103,9 +109,12 @@ def docpage(request, version, filename): # if the page does not exist but there is a special page redirect, check # for the existence of that. if that does not exist, then we're really # done and can 404 - page_redirect = get_object_or_404(DocPageRedirect, redirect_from=fullname) - url = "/docs/{}/{}".format(version, page_redirect.redirect_to) - return HttpResponsePermanentRedirect(url) + try: + page_redirect = DocPageRedirect.objects.get(redirect_from=fullname) + url = "/docs/{}/{}".format(version, page_redirect.redirect_to) + return HttpResponsePermanentRedirect(url) + except DocPageRedirect.DoesNotExist: + return _versioned_404("Page not found", ver) versions = DocPage.objects.select_related('version').extra( where=["file=%s OR file IN (SELECT file2 FROM docsalias WHERE file1=%s) OR file IN (SELECT file1 FROM docsalias WHERE file2=%s)"], @@ -151,7 +160,7 @@ def docsvg(request, version, filename): else: ver = Decimal(version) if ver == Decimal(0): - raise Http404("Version not found") + return _versioned_404("Version not found", "all") if ver < Decimal(12) and ver > Decimal(0): raise Http404("SVG images don't exist in this version")