Add proper xkey tags to docs 404 pages
authorMagnus Hagander <magnus@hagander.net>
Mon, 16 Aug 2021 12:15:38 +0000 (14:15 +0200)
committerMagnus Hagander <magnus@hagander.net>
Mon, 16 Aug 2021 12:17:10 +0000 (14:17 +0200)
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
pgweb/docs/views.py

index 2e4f48ac9413ec5c3920c26e69fb24b0b2b861ed..cb3d3485cef28106a768651da6571fe7c00ba86d 100644 (file)
@@ -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_<version>
   Set on documentation of the specified version.
 pgdocs_pdf
index 6da47d680ea1b69c9cd18481e63db9a56ca5de68..34033e329cb97445170a9497314f88121a14e83e 100644 (file)
@@ -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")