Skip to content

Commit

Permalink
Added documentation type filtering to search.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahboyce committed Feb 9, 2025
1 parent 3619319 commit 8febaf6
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
36 changes: 36 additions & 0 deletions djangoproject/scss/_style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2563,6 +2563,42 @@ table.docutils th {
}
}

.tabs {
display: flex;
gap: 10px;
border-bottom: 2px solid var(--hairline-color);
overflow-x: auto;
white-space: nowrap;
padding-bottom: 0;
position: relative;

a:not(.active):focus,
a:not(.active):active,
a:not(.active):hover {
outline: none;
border-bottom: 3px solid var(--hairline-color);
}

.tab {
padding: 10px 20px;
text-decoration: none;
background: transparent;
border: none;
border-bottom: 3px solid transparent;
transition: color 0.3s ease, border-bottom 0.3s ease;
font-size: 16px;
color: var(--text-light);
flex-shrink: 0;
}

.active {
color: var(--body-fg);
font-weight: bold;
border-bottom: 3px solid var(--primary);
}
}


.search-links {
@extend .list-links;

Expand Down
7 changes: 7 additions & 0 deletions docs/templates/docs/search_results.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ <h2>{% trans "No search query given" %}</h2>

{% if query %}
<div id="docs-content">
<div class="tabs">
<a class="tab{% if not type %} active{% endif %}" href="{% querystring type=None %}">{% trans "All" %}</a>
<a class="tab{% if type == 'ref' %} active{% endif %}" href="{% querystring type='ref' %}">{% trans "API Reference" %}</a>
<a class="tab{% if type == 'topics' %} active{% endif %}" href="{% querystring type='topics' %}">{% trans "Using Django" %}</a>
<a class="tab{% if type == 'howto' %} active{% endif %}" href="{% querystring type='howto' %}">{% trans "How-to guides" %}</a>
<a class="tab{% if type == 'releases' %} active{% endif %}" href="{% querystring type='releases' %}">{% trans "Release notes" %}</a>
</div>
<dl class="search-links">
{% for result in page.object_list %}
<dt>
Expand Down
62 changes: 62 additions & 0 deletions docs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,68 @@ def test_empty_get(self):
)
self.assertEqual(response.status_code, 200)

def test_search_type_filter(self):
release = Release.objects.create(version="5.1")
doc_release = DocumentRelease.objects.create(release=release)
types = [
("topics", "Using Django"),
("releases", "Release notes"),
("howto", "How-to guides"),
("ref", "API Reference"),
]

for path, title in types:
Document.objects.create(
**{
"metadata": {
"body": "Generic Views",
"breadcrumbs": [
{"path": path, "title": title},
],
"parents": path,
"slug": "generic-views",
"title": "Generic views",
"toc": '<ul>\n<li><a class="reference internal" href="#">Generic views</a></li>\n</ul>\n',
},
"path": f"{path}/generic-views",
"release": doc_release,
"title": "Generic views",
}
)

for path, title in types:
with self.subTest(type=path):
response = self.client.get(
f"/en/5.1/search/?q=generic&type={path}",
headers={"host": "docs.djangoproject.localhost:8000"},
)
self.assertEqual(response.status_code, 200)
self.assertContains(
response,
"Only 1 result for <em>generic</em> in version 5.1",
html=True,
)
self.assertContains(response, "tab active", count=1)
self.assertContains(
response,
f'<a class="tab active" href="?q=generic&amp;type={path}">{title}</a>',
html=True,
)

with self.subTest(type="all"):
response = self.client.get(
"/en/5.1/search/?q=generic",
headers={"host": "docs.djangoproject.localhost:8000"},
)
self.assertEqual(response.status_code, 200)
self.assertContains(
response, "4 results for <em>generic</em> in version 5.1", html=True
)
self.assertContains(response, "tab active", count=1)
self.assertContains(
response, '<a class="tab active" href="?q=generic">All</a>', html=True
)


class TemplateTagTests(TestCase):
fixtures = ["doc_test_fixtures"]
Expand Down
5 changes: 5 additions & 0 deletions docs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ def search_results(request, lang, version, per_page=10, orphans=3):
return redirect(exact)

results = Document.objects.search(q, release)
type_value = request.GET.get("type", None)
filtered_by_type = type_value in ["ref", "releases", "howto", "topics"]
if filtered_by_type:
results = results.filter(metadata__parents__startswith=type_value)

page_number = request.GET.get("page") or 1
paginator = Paginator(results, per_page=per_page, orphans=orphans)
Expand Down Expand Up @@ -217,6 +221,7 @@ def search_results(request, lang, version, per_page=10, orphans=3):
"page": page,
"paginator": paginator,
"start_sel": START_SEL,
"type": type_value if filtered_by_type else None,
}
)

Expand Down

0 comments on commit 8febaf6

Please sign in to comment.