Skip to content

Commit efc51e8

Browse files
committed
Enabled serving of sphinx build files in DEBUG mode only
1 parent e444a76 commit efc51e8

File tree

5 files changed

+61
-54
lines changed

5 files changed

+61
-54
lines changed

docs/management/commands/update_docs.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ def zipfile_inclusion_filter(file_path):
285285
]
286286
)
287287

288+
if release.is_default:
289+
self._setup_stable_symlink(release, built_dir)
290+
288291
#
289292
# Rebuild the imported document list and search index.
290293
#
@@ -360,6 +363,14 @@ def update_git(self, url, destdir, changed_dir="."):
360363
)
361364
return True
362365

366+
def _setup_stable_symlink(self, release, built_dir):
367+
"""
368+
Setup a symbolic link called "stable" pointing to the given release build
369+
"""
370+
stable = built_dir / "stable"
371+
target = built_dir / release.version
372+
stable.symlink_to(target, target_is_directory=True)
373+
363374

364375
def gen_decoded_documents(directory):
365376
"""

docs/urls.py

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
from django.conf import settings
12
from django.urls import path, re_path
23
from django.views.generic import RedirectView
34

4-
from . import views
5+
from . import views, views_debug
56

67
urlpatterns = [
78
path("", views.index, name="homepage"),
@@ -21,29 +22,6 @@
2122
{"url": ""},
2223
name="document-index",
2324
),
24-
re_path(
25-
r"^(?P<lang>[a-z-]+)/(?P<version>[\w.-]+)/_objects/$",
26-
views.objects_inventory,
27-
name="objects-inv",
28-
),
29-
re_path(
30-
r"^(?P<lang>[a-z-]+)/(?P<version>[\w.-]+)/_images/(?P<path>.*)$",
31-
views.sphinx_static,
32-
{"subpath": "_images"},
33-
name="sphinx-images",
34-
),
35-
re_path(
36-
r"^(?P<lang>[a-z-]+)/(?P<version>[\w.-]+)/_source/(?P<path>.*)$",
37-
views.sphinx_static,
38-
{"subpath": "_sources"},
39-
name="sphinx-sources",
40-
),
41-
re_path(
42-
r"^(?P<lang>[a-z-]+)/(?P<version>[\w.-]+)/_downloads/(?P<path>.*)$",
43-
views.sphinx_static,
44-
{"subpath": "_downloads"},
45-
name="sphinx-downloads",
46-
),
4725
re_path("^(.*)/index/$", views.redirect_index),
4826
re_path(
4927
r"^(?P<lang>[a-z-]+)/(?P<version>[\w.-]+)/search/$",
@@ -65,5 +43,20 @@
6543
views.document,
6644
name="document-detail",
6745
),
68-
re_path(r"^pots/(?P<pot_name>\w+\.pot)$", views.pot_file),
6946
]
47+
48+
if settings.DEBUG:
49+
# Patterns for sphinx (in production they are served by nginx directly)
50+
# They need to be inserted at the beginning to take precedence over document-detail
51+
urlpatterns = [
52+
re_path(
53+
r"^(?P<lang>[a-z-]+)/(?P<version>[\w.-]+)/_objects/$",
54+
views_debug.objects_inventory,
55+
),
56+
re_path(
57+
r"^(?P<lang>[a-z-]+)/(?P<version>[\w.-]+)/(?P<subpath>_downloads|_images|_source)/(?P<path>.*)$", # noqa: E501
58+
views_debug.sphinx_static,
59+
),
60+
re_path(r"^pots/(?P<pot_name>\w+\.pot)$", views.pot_file),
61+
*urlpatterns,
62+
]

docs/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
from django.http import Http404
66

77

8-
def get_doc_root(lang, version, subroot="json"):
9-
return settings.DOCS_BUILD_ROOT.joinpath(lang, version, "_built", subroot)
8+
def get_doc_root(lang, version, builder="json"):
9+
return settings.DOCS_BUILD_ROOT.joinpath(lang, version, "_built", builder)
1010

1111

12-
def get_doc_root_or_404(lang, version, subroot="json"):
13-
docroot = get_doc_root(lang, version, subroot)
12+
def get_doc_root_or_404(lang, version, builder="json"):
13+
docroot = get_doc_root(lang, version, builder)
1414
if not docroot.exists():
1515
raise Http404(str(docroot))
1616
return docroot

docs/views.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from django.shortcuts import redirect, render
1111
from django.template.response import TemplateResponse
1212
from django.utils.translation import activate, gettext_lazy as _
13-
from django.views import static
1413
from django.views.decorators.cache import cache_page
1514
from django_hosts.resolvers import reverse
1615

@@ -111,30 +110,6 @@ def load_json_file(path):
111110
)
112111

113112

114-
def pot_file(request, pot_name):
115-
version = DocumentRelease.objects.current().version
116-
doc_root = str(get_doc_root_or_404("en", version, subroot="gettext"))
117-
return static.serve(request, document_root=doc_root, path=pot_name)
118-
119-
120-
def sphinx_static(request, lang, version, path, subpath=None):
121-
"""
122-
Serve Sphinx static assets from a subdir of the build location.
123-
"""
124-
document_root = str(get_doc_root_or_404(lang, version).joinpath(subpath))
125-
return static.serve(request, document_root=document_root, path=path)
126-
127-
128-
def objects_inventory(request, lang, version):
129-
response = static.serve(
130-
request,
131-
document_root=str(get_doc_root_or_404(lang, version)),
132-
path="objects.inv",
133-
)
134-
response["Content-Type"] = "text/plain"
135-
return response
136-
137-
138113
def redirect_index(request, *args, **kwargs):
139114
assert request.path.endswith("index/")
140115
return redirect(request.path[:-6])

docs/views_debug.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from django.views import static
2+
3+
from .models import DocumentRelease
4+
from .utils import get_doc_root_or_404
5+
6+
7+
def sphinx_static(request, lang, version, path, subpath):
8+
"""
9+
Serve Sphinx static assets from a subdir of the build location.
10+
"""
11+
document_root = get_doc_root_or_404(lang, version) / subpath
12+
return static.serve(request, document_root=document_root, path=path)
13+
14+
15+
def objects_inventory(request, lang, version):
16+
response = static.serve(
17+
request,
18+
document_root=get_doc_root_or_404(lang, version),
19+
path="objects.inv",
20+
)
21+
response["Content-Type"] = "text/plain"
22+
return response
23+
24+
25+
def pot_file(request, pot_name):
26+
version = DocumentRelease.objects.current().version
27+
doc_root = get_doc_root_or_404("en", version, builder="gettext")
28+
return static.serve(request, document_root=doc_root, path=pot_name)

0 commit comments

Comments
 (0)