Skip to content

Commit 2bf75cd

Browse files
committed
Add a method to sync the views to the database for searching.
1 parent 14dcb98 commit 2bf75cd

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

docs/models.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from django.db import models, transaction
1919
from django.db.models import Q
2020
from django.db.models.fields.json import KeyTextTransform
21+
from django.test import RequestFactory
22+
from django.urls import resolve, reverse as reverse_path
2123
from django.utils.functional import cached_property
2224
from django.utils.html import strip_tags
2325
from django_hosts.resolvers import reverse
@@ -218,6 +220,9 @@ def sync_to_db(self, decoded_documents):
218220
)
219221
document.save(update_fields=("metadata",))
220222

223+
self._sync_blog_to_db()
224+
self._sync_views_to_db()
225+
221226
def _sync_blog_to_db(self):
222227
"""
223228
Sync the blog entries into search based on the release documents
@@ -244,6 +249,47 @@ def _sync_blog_to_db(self):
244249
),
245250
)
246251

252+
def _sync_views_to_db(self):
253+
"""
254+
Sync the blog entries into search based on the release documents
255+
support end date.
256+
"""
257+
if self.lang == "en":
258+
# The request needs to come through as a valid one, it's best if it
259+
# matches the exact host we're looking for.
260+
www_hosts = [
261+
host for host in settings.ALLOWED_HOSTS if host.startswith("www.")
262+
]
263+
if not www_hosts or not (www_host := www_hosts[0]):
264+
return
265+
synced_views = [
266+
# Page title, url name, url kwargs
267+
("Django's Ecosystem", "community-ecosystem", {}),
268+
]
269+
for title, url_name, kwargs in synced_views:
270+
absolute_url = reverse(url_name, kwargs=kwargs, host="www")
271+
path = reverse_path(url_name, kwargs=kwargs)
272+
request = RequestFactory().get(path, HTTP_HOST=www_host)
273+
body = resolve(path).func(request).render().text
274+
Document.objects.create(
275+
release=self,
276+
path=absolute_url,
277+
title=title,
278+
metadata={
279+
"body": body,
280+
"breadcrumbs": [
281+
{"path": "weblog", "title": "Website"},
282+
],
283+
"parents": "weblog",
284+
"slug": url_name,
285+
"title": title,
286+
"toc": "",
287+
},
288+
config=TSEARCH_CONFIG_LANGUAGES.get(
289+
self.lang[:2], DEFAULT_TEXT_SEARCH_CONFIG
290+
),
291+
)
292+
247293

248294
def _clean_document_path(path):
249295
# We have to be a bit careful to reverse-engineer the correct

0 commit comments

Comments
 (0)