Skip to content

Commit e2120f0

Browse files
Jonathan S. Katzjkatz
Jonathan S. Katz
authored andcommitted
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.
1 parent 2239cc1 commit e2120f0

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

pgweb/docs/admin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.contrib import admin
22

3-
from .models import DocPageAlias
3+
from .models import DocPageAlias, DocPageRedirect
44

55
admin.site.register(DocPageAlias)
6+
admin.site.register(DocPageRedirect)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Generated by Django 2.2.12 on 2020-04-24 23:16
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('docs', '0003_docs_alias'),
10+
]
11+
12+
operations = [
13+
migrations.CreateModel(
14+
name='DocPageRedirect',
15+
fields=[
16+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
17+
('redirect_from', models.CharField(max_length=64, unique=True, help_text='Page to redirect from, e.g. "old_page.html"')),
18+
('redirect_to', models.CharField(max_length=64, unique=True, help_text='Page to redirect to, e.g. "new_page.html"')),
19+
],
20+
options={
21+
'verbose_name_plural': 'Doc page redirects',
22+
},
23+
),
24+
]

pgweb/docs/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,14 @@ def __str__(self):
3333
class Meta:
3434
db_table = 'docsalias'
3535
verbose_name_plural = 'Doc page aliases'
36+
37+
38+
class DocPageRedirect(models.Model):
39+
"""DocPageRedirect offers the ability to redirect from a page that has been
40+
completely removed from the PostgreSQL documentation
41+
"""
42+
redirect_from = models.CharField(max_length=64, null=False, blank=False, unique=True, help_text='Page to redirect from, e.g. "old_page.html"')
43+
redirect_to = models.CharField(max_length=64, null=False, blank=False, unique=True, help_text='Page to redirect from, e.g. "new_page.html"')
44+
45+
class Meta:
46+
verbose_name_plural = "Doc page redirects"

pgweb/docs/views.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from pgweb.core.models import Version
1717
from pgweb.util.db import exec_to_dict
1818

19-
from .models import DocPage
19+
from .models import DocPage, DocPageRedirect
2020
from .forms import DocCommentForm
2121

2222

@@ -94,7 +94,18 @@ def docpage(request, version, filename):
9494
url += "{}/{}".format(release_version, fullname)
9595
return HttpResponsePermanentRedirect(url)
9696

97-
page = get_object_or_404(DocPage, version=ver, file=fullname)
97+
# try to get the page outright. If it's not found, check to see if it's a
98+
# doc alias with a redirect, and if so, redirect to that page
99+
try:
100+
page = DocPage.objects.get(version=ver, file=fullname)
101+
except DocPage.DoesNotExist:
102+
# if the page does not exist but there is a special pgae redirect, check
103+
# for the existence of that. if that does not exist, then we're really
104+
# done and can 404
105+
page_redirect = get_object_or_404(DocPageRedirect, redirect_from=fullname)
106+
url = "/docs/{}/{}".format(version, page_redirect.redirect_to)
107+
return HttpResponsePermanentRedirect(url)
108+
98109
versions = DocPage.objects.extra(
99110
where=["file=%s OR file IN (SELECT file2 FROM docsalias WHERE file1=%s) OR file IN (SELECT file1 FROM docsalias WHERE file2=%s)"],
100111
params=[fullname, fullname, fullname],

0 commit comments

Comments
 (0)