Skip to content

Commit f1e280c

Browse files
committed
WIP support of including blog in the website search.
The blog results should have a property of whether it is included in the search results. We should also limit the blogs that are searchable for a version of Django based on the support end. This will allow us to limit the inclusion of blog posts in the search based on the time the entry was created, keeping the search results relevant to that version of Django.
1 parent b368d0c commit f1e280c

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

blog/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def get_absolute_url(self):
168168
"day": self.pub_date.strftime("%d").lower(),
169169
"slug": self.slug,
170170
}
171-
return reverse("weblog:entry", kwargs=kwargs)
171+
return reverse("weblog:entry", kwargs=kwargs, host="www")
172172

173173
def is_published(self):
174174
"""
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 5.2 on 2025-07-23 16:31
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('docs', '0006_alter_document_metadata_noop'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='documentrelease',
15+
name='support_end',
16+
field=models.DateField(blank=True, help_text='The end of support for this release of Django.', null=True),
17+
),
18+
]

docs/models.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from django.utils.html import strip_tags
2323
from django_hosts.resolvers import reverse
2424

25+
from blog.models import Entry
2526
from releases.models import Release
2627

2728
from . import utils
@@ -95,6 +96,11 @@ class DocumentRelease(models.Model):
9596
on_delete=models.CASCADE,
9697
)
9798
is_default = models.BooleanField(default=False)
99+
support_end = models.DateField(
100+
null=True,
101+
blank=True,
102+
help_text="The end of support for this release of Django.",
103+
)
98104

99105
objects = DocumentReleaseQuerySet.as_manager()
100106

@@ -212,6 +218,32 @@ def sync_to_db(self, decoded_documents):
212218
)
213219
document.save(update_fields=("metadata",))
214220

221+
def _sync_blog_to_db(self):
222+
"""
223+
Sync the blog entries into search based on the release documents
224+
support end date.
225+
"""
226+
if self.lang == "en":
227+
for entry in Entry.objects.published():
228+
Document.objects.create(
229+
release=self,
230+
path=entry.get_absolute_url(),
231+
title=entry.headline,
232+
metadata={
233+
"body": entry.body_html,
234+
"breadcrumbs": [
235+
{"path": "weblog", "title": "News"},
236+
],
237+
"parents": "weblog",
238+
"slug": entry.slug,
239+
"title": entry.headline,
240+
"toc": "",
241+
},
242+
config=TSEARCH_CONFIG_LANGUAGES.get(
243+
self.lang[:2], DEFAULT_TEXT_SEARCH_CONFIG
244+
),
245+
)
246+
215247

216248
def _clean_document_path(path):
217249
# We have to be a bit careful to reverse-engineer the correct
@@ -382,3 +414,16 @@ def body(self):
382414
with open(str(self.full_path)) as fp:
383415
doc = json.load(fp)
384416
return doc["body"]
417+
418+
def document_url(self):
419+
if self.metadata["parents"] == "weblog":
420+
return self.path
421+
return reverse(
422+
"document-detail",
423+
kwargs={
424+
"lang": self.release.lang,
425+
"version": self.release.version,
426+
"url": self.path,
427+
},
428+
host="docs",
429+
)

docs/search.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class DocumentationCategory(TextChoices):
6565
TOPICS = "topics", _("Using Django")
6666
HOWTO = "howto", _("How-to guides")
6767
RELEASE_NOTES = "releases", _("Release notes")
68+
WEBSITE = "weblog", _("Django Website")
6869

6970
@classmethod
7071
def parse(cls, value, default=None):

docs/templates/docs/search_results.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ <h2>{% translate "No search query given" %}</h2>
4343
{% for result in page.object_list %}
4444
<dt>
4545
<h2 class="result-title">
46-
<a href="{% url 'document-detail' lang=result.release.lang version=result.release.version url=result.path host 'docs' %}{% if not start_sel in result.headline %}{{ result.highlight|fragment }}{% endif %}">{{ result.headline|safe }}</a>
46+
<a href="{{ result.document_url }}{% if not start_sel in result.headline %}{{ result.highlight|fragment }}{% endif %}">{{ result.headline|safe }}</a>
4747
</h2>
4848
<span class="meta breadcrumbs">
4949
{% for breadcrumb in result.breadcrumbs %}
50-
<a href="{% url 'document-detail' lang=result.release.lang version=result.release.version url=breadcrumb.path host 'docs' %}">{{ breadcrumb.title }}</a>{% if not forloop.last %} <span class="arrow">»</span>{% endif %}
50+
<a href="{{ result.document_url }}">{{ breadcrumb.title }}</a>{% if not forloop.last %} <span class="arrow">»</span>{% endif %}
5151
{% endfor %}
5252
</span>
5353
</dt>
@@ -60,7 +60,7 @@ <h2 class="result-title">
6060
<ul class="code-links">
6161
{% for name, value in result_code_links.items %}
6262
<li>
63-
<a href="{% url 'document-detail' lang=result.release.lang version=result.release.version url=result.path host 'docs' %}#{{ value.full_path }}">
63+
<a href="{{ result.document_url }}#{{ value.full_path }}">
6464
<div>
6565
<code>{{ name }}</code>
6666
{% if value.module_path %}<div class="meta">{{ value.module_path }}</div>{% endif %}

0 commit comments

Comments
 (0)