diff options
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/management/commands/update_docs.py | 3 | ||||
| -rw-r--r-- | docs/migrations/0008_documentrelease_global_context.py | 44 | ||||
| -rw-r--r-- | docs/models.py | 7 | ||||
| -rw-r--r-- | docs/templates/docs/doc.html | 6 | ||||
| -rw-r--r-- | docs/tests/test_templates.py | 2 | ||||
| -rw-r--r-- | docs/views.py | 17 |
6 files changed, 64 insertions, 15 deletions
diff --git a/docs/management/commands/update_docs.py b/docs/management/commands/update_docs.py index b5b01131..649c8e0a 100644 --- a/docs/management/commands/update_docs.py +++ b/docs/management/commands/update_docs.py @@ -278,6 +278,9 @@ class Command(BaseCommand): json_built_dir = parent_build_dir / "_built" / "json" documents = gen_decoded_documents(json_built_dir) + with open(json_built_dir / "globalcontext.json") as context: + release.global_context = json.load(context) + release.save(update_fields=["global_context"]) release.sync_to_db(documents) def update_git(self, url, destdir, changed_dir="."): diff --git a/docs/migrations/0008_documentrelease_global_context.py b/docs/migrations/0008_documentrelease_global_context.py new file mode 100644 index 00000000..79dfe154 --- /dev/null +++ b/docs/migrations/0008_documentrelease_global_context.py @@ -0,0 +1,44 @@ +# Generated by Django 6.0.4 on 2026-05-06 09:20 + +import json + +from django.conf import settings +from django.db import migrations, models + + +def set_documentrelease_global_context(apps, schema_editor): + DocumentRelease = apps.get_model("docs", "DocumentRelease") + for doc_release in DocumentRelease.objects.filter(release__isnull=False): + if not doc_release.global_context: + try: + with open( + settings.DOCS_BUILD_ROOT + / doc_release.lang + / doc_release.release.version + / "_built" + / "json" + / "globalcontext.json" + ) as context: + doc_release.global_context = json.load(context) + except FileNotFoundError: + pass + else: + doc_release.save(update_fields=["global_context"]) + + +class Migration(migrations.Migration): + + dependencies = [ + ("docs", "0007_add_docs_search_vector"), + ] + + operations = [ + migrations.AddField( + model_name="documentrelease", + name="global_context", + field=models.JSONField(default=dict), + ), + migrations.RunPython( + set_documentrelease_global_context, migrations.RunPython.noop, elidable=True + ), + ] diff --git a/docs/models.py b/docs/models.py index 8dd7268c..3021a079 100644 --- a/docs/models.py +++ b/docs/models.py @@ -1,6 +1,5 @@ import datetime import html -import json import logging import operator from functools import partial, reduce @@ -111,6 +110,7 @@ class DocumentRelease(models.Model): limit_choices_to={"status": "f"}, on_delete=models.CASCADE, ) + global_context = models.JSONField(default=dict) is_default = models.BooleanField(default=False) objects = DocumentReleaseQuerySet.as_manager() @@ -469,7 +469,4 @@ class Document(models.Model): @cached_property def body(self): - """The document's body""" - with self.full_path.open() as fp: - doc = json.load(fp) - return doc["body"] + return self.metadata["body"] diff --git a/docs/templates/docs/doc.html b/docs/templates/docs/doc.html index 73655fd4..87952e27 100644 --- a/docs/templates/docs/doc.html +++ b/docs/templates/docs/doc.html @@ -166,11 +166,11 @@ <ul> <li> <a href="{% url 'document-index' lang=lang version=version host 'docs' %}">{% blocktranslate %}Django {{ version }} documentation{% endblocktranslate %}</a> - {% for p in doc.parents %} - <ul><li><a href="{{ p.link }}">{{ p.title|safe }}</a> + {% for p in doc.breadcrumbs %} + <ul><li><a href="{% url 'document-detail' lang=lang version=version url=p.path host 'docs' %}">{{ p.title|safe }}</a> {% endfor %} <ul><li>{% block current-page-title %}{{ doc.title|safe }}{% endblock %}</li></ul> - {% for p in doc.parents %}</li></ul>{% endfor %} + {% for p in doc.breadcrumbs %}</li></ul>{% endfor %} </li> </ul> </nav> diff --git a/docs/tests/test_templates.py b/docs/tests/test_templates.py index 22abd057..5f28fc86 100644 --- a/docs/tests/test_templates.py +++ b/docs/tests/test_templates.py @@ -214,12 +214,12 @@ class TemplateTestCase(TestCase): def test_opengraph_title(self): doc = Document.objects.create( + metadata={"body": "test body"}, release=DocumentRelease.objects.create( lang=settings.DEFAULT_LANGUAGE_CODE, release=Release.objects.create(version="5.0"), ), ) - doc.body = "test body" # avoids trying to load the underlying physical file for title, expected in [ ("test title", "test title"), diff --git a/docs/views.py b/docs/views.py index 03c53cf4..b5a7a39e 100644 --- a/docs/views.py +++ b/docs/views.py @@ -1,4 +1,3 @@ -import datetime import json from django.conf import settings @@ -79,9 +78,18 @@ def document(request, lang, version, url): version ) + try: + doc = Document.objects.get( + release=release, path__in=[url, f"{url}index"] + ).metadata + except Document.DoesNotExist: + # We won't find e.g. the genindex page nor partially + # translated documents in the database. + doc = load_json_file(doc_path) + context = { - "doc": load_json_file(doc_path), - "env": load_json_file(docroot / "globalcontext.json"), + "doc": doc, + "env": release.global_context, "lang": lang, "version": version, "canonical_version": canonical_version, @@ -90,9 +98,6 @@ def document(request, lang, version, url): "release": release, "rtd_version": rtd_version, "docurl": url, - "update_date": datetime.datetime.fromtimestamp( - (docroot / "last_build").stat().st_mtime - ), "redirect_from": request.GET.get("from", None), } response = render(request, template_names, context) |
