Introduce documentation redirects for doc pages that are renamed

The web documentation used to suffer from a problem that if a
documentation page were renamed in a newer version, any references
pointing to said documentation would be lost. For example, the feature
known as "Default Roles" was renamed to "Privileged Roles" but caused
a change in the URL.

This patch introduces the ability to create a "DocPageRedirect" by
specifying the previous name of the documentation page (e.g.
"default-roles.html") and the new name (e.g. "privileged-roles.html")
such that the continuity is preserved between versions.
This commit is contained in:
Jonathan S. Katz
2020-02-11 14:41:46 -05:00
committed by Jonathan S. Katz
parent 2239cc1f2c
commit e2120f0a80
4 changed files with 50 additions and 3 deletions

View File

@ -1,5 +1,6 @@
from django.contrib import admin
from .models import DocPageAlias
from .models import DocPageAlias, DocPageRedirect
admin.site.register(DocPageAlias)
admin.site.register(DocPageRedirect)

View File

@ -0,0 +1,24 @@
# Generated by Django 2.2.12 on 2020-04-24 23:16
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('docs', '0003_docs_alias'),
]
operations = [
migrations.CreateModel(
name='DocPageRedirect',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('redirect_from', models.CharField(max_length=64, unique=True, help_text='Page to redirect from, e.g. "old_page.html"')),
('redirect_to', models.CharField(max_length=64, unique=True, help_text='Page to redirect to, e.g. "new_page.html"')),
],
options={
'verbose_name_plural': 'Doc page redirects',
},
),
]

View File

@ -33,3 +33,14 @@ class DocPageAlias(models.Model):
class Meta:
db_table = 'docsalias'
verbose_name_plural = 'Doc page aliases'
class DocPageRedirect(models.Model):
"""DocPageRedirect offers the ability to redirect from a page that has been
completely removed from the PostgreSQL documentation
"""
redirect_from = models.CharField(max_length=64, null=False, blank=False, unique=True, help_text='Page to redirect from, e.g. "old_page.html"')
redirect_to = models.CharField(max_length=64, null=False, blank=False, unique=True, help_text='Page to redirect from, e.g. "new_page.html"')
class Meta:
verbose_name_plural = "Doc page redirects"

View File

@ -16,7 +16,7 @@ from pgweb.util.misc import send_template_mail
from pgweb.core.models import Version
from pgweb.util.db import exec_to_dict
from .models import DocPage
from .models import DocPage, DocPageRedirect
from .forms import DocCommentForm
@ -94,7 +94,18 @@ def docpage(request, version, filename):
url += "{}/{}".format(release_version, fullname)
return HttpResponsePermanentRedirect(url)
page = get_object_or_404(DocPage, version=ver, file=fullname)
# try to get the page outright. If it's not found, check to see if it's a
# doc alias with a redirect, and if so, redirect to that page
try:
page = DocPage.objects.get(version=ver, file=fullname)
except DocPage.DoesNotExist:
# if the page does not exist but there is a special pgae 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)
versions = DocPage.objects.extra(
where=["file=%s OR file IN (SELECT file2 FROM docsalias WHERE file1=%s) OR file IN (SELECT file1 FROM docsalias WHERE file2=%s)"],
params=[fullname, fullname, fullname],