Introduce documentation redirects for doc pages that are renamed github/doc-redirect
authorJonathan S. Katz <jonathan.katz@crunchydata.com>
Tue, 11 Feb 2020 19:41:46 +0000 (14:41 -0500)
committerJonathan S. Katz <jonathan.katz@excoventures.com>
Fri, 24 Apr 2020 23:33:16 +0000 (19:33 -0400)
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.

pgweb/docs/admin.py
pgweb/docs/migrations/0004_docpageredirect.py [new file with mode: 0644]
pgweb/docs/models.py
pgweb/docs/views.py

index d2f2590d3acdb63fa13abed00a8f7b6a2209bf90..79f1d123e0a6688c289c37633cd4f9944b907631 100644 (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)
diff --git a/pgweb/docs/migrations/0004_docpageredirect.py b/pgweb/docs/migrations/0004_docpageredirect.py
new file mode 100644 (file)
index 0000000..d19f959
--- /dev/null
@@ -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',
+            },
+        ),
+    ]
index 87afe1cac1a855a07bfb082d50698beb9c39f132..a4558a6863cd6eb7c144e3691d813f4a11a0727e 100644 (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"
index 056612951a8ddb91d84e3f0f213291aa7e8c9bc2..6763795b9f2ae0990d97357d87ccc601bd599986 100644 (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],