diff --git a/lacommunaute/documentation/tests/__snapshots__/tests_category_detail_view.ambr b/lacommunaute/documentation/tests/__snapshots__/tests_category_detail_view.ambr index abd73949..7dad9a05 100644 --- a/lacommunaute/documentation/tests/__snapshots__/tests_category_detail_view.ambr +++ b/lacommunaute/documentation/tests/__snapshots__/tests_category_detail_view.ambr @@ -54,6 +54,7 @@ +
@@ -64,11 +65,11 @@
- + - +
@@ -87,6 +88,32 @@
+
+ + +
+
+
+ @@ -175,6 +204,7 @@ +
@@ -185,13 +215,13 @@
- - +
@@ -223,7 +253,7 @@
+ + @@ -298,6 +330,7 @@ +
@@ -308,11 +341,11 @@
- + - +
@@ -328,37 +361,13 @@
-
- -
- -
+ + diff --git a/lacommunaute/documentation/tests/tests_category_detail_view.py b/lacommunaute/documentation/tests/tests_category_detail_view.py index cd2df81e..9368ecdf 100644 --- a/lacommunaute/documentation/tests/tests_category_detail_view.py +++ b/lacommunaute/documentation/tests/tests_category_detail_view.py @@ -1,8 +1,9 @@ import pytest # noqa - +from django.db import connection from lacommunaute.documentation.factories import CategoryFactory, DocumentFactory from lacommunaute.users.factories import UserFactory from lacommunaute.utils.testing import parse_response_to_soup +from django.test.utils import CaptureQueriesContext @pytest.fixture(name="category") @@ -20,6 +21,7 @@ def fixture_category(): ) def test_category_detail_view_with_tagged_documents(client, db, category, active_tag, snapshot_name, snapshot): DocumentFactory(category=category, with_tags=["tag1", "tag2"], for_snapshot=True) + DocumentFactory(category=category, for_snapshot=True, name="Document without tags") url = f"{category.get_absolute_url()}?tag={active_tag}" if active_tag else category.get_absolute_url() response = client.get(url) assert response.status_code == 200 @@ -27,7 +29,8 @@ def test_category_detail_view_with_tagged_documents(client, db, category, active response, selector="main", replace_img_src=True, - replace_in_href=[category] + [doc for doc in category.documents.all()], + replace_in_href=[(category.get_absolute_url(), "[Category detail view url]")] + + [(doc.get_absolute_url(), "[Document detail view url]") for doc in category.documents.all()], ) assert str(content) == snapshot(name=snapshot_name) @@ -42,3 +45,33 @@ def test_link_to_update_view(client, db, category, user, link_is_visible): response = client.get(category.get_absolute_url()) assert response.status_code == 200 assert bool(category.get_update_url() in response.content.decode()) == link_is_visible + + +@pytest.mark.parametrize( + "headers,expected_template_name", + [(None, "documentation/category_detail.html"), ({"HX-Request": True}, "documentation/document_list.html")], +) +def test_template_name(client, db, category, headers, expected_template_name): + response = client.get(category.get_absolute_url(), headers=headers) + assert response.status_code == 200 + assert response.template_name == [expected_template_name] + + +@pytest.fixture(name="category_with_tons_of_documents") +def category_with_tons_of_documents(): + category = CategoryFactory() + DocumentFactory(category=category, with_tags=[f"tag{i}" for i in range(50)]) + DocumentFactory.create_batch(25, category=category) + return category + + +@pytest.mark.parametrize("tag,expected_count", [(None, 10), ("tag1", 12)]) +def test_numqueries(client, db, category_with_tons_of_documents, tag, expected_count): + url = ( + f"{category_with_tons_of_documents.get_absolute_url()}?tag={tag}" + if tag + else category_with_tons_of_documents.get_absolute_url() + ) + with CaptureQueriesContext(connection) as queries: + client.get(url) + assert len(queries) == expected_count diff --git a/lacommunaute/documentation/views.py b/lacommunaute/documentation/views.py index 509339ae..748cbe0d 100644 --- a/lacommunaute/documentation/views.py +++ b/lacommunaute/documentation/views.py @@ -17,9 +17,23 @@ class CategoryListView(ListView): class CategoryDetailView(DetailView): model = Category - template_name = "documentation/category_detail.html" context_object_name = "category" + def get_template_names(self): + if self.request.META.get("HTTP_HX_REQUEST"): + return ["documentation/document_list.html"] + return ["documentation/category_detail.html"] + + def get_active_tag_slug(self): + if not hasattr(self, "active_tag_slug"): + self.active_tag_slug = self.request.GET.get("tag") or None + return self.active_tag_slug + + def get_filtered_documents(self): + if self.get_active_tag_slug(): + return self.object.documents.filter(tags__slug=self.get_active_tag_slug()).prefetch_related("tags") + return self.object.documents.all() + def get_tags_of_documents(self): return Tag.objects.filter( taggit_taggeditem_items__content_type=ContentType.objects.get_for_model(Document), @@ -32,7 +46,8 @@ def get_queryset(self): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["tags"] = self.get_tags_of_documents() - context["active_tag_slug"] = self.request.GET.get("tag") or None + context["active_tag_slug"] = self.get_active_tag_slug() + context["documents"] = self.get_filtered_documents() return context diff --git a/lacommunaute/templates/documentation/category_detail.html b/lacommunaute/templates/documentation/category_detail.html index a3634c34..923777a1 100644 --- a/lacommunaute/templates/documentation/category_detail.html +++ b/lacommunaute/templates/documentation/category_detail.html @@ -16,60 +16,5 @@ {% endif %} - {% if tags %} -
-
-
-
-
-
Afficher les fiches contenant l'étiquette
-
- {% for tag in tags %} - {% if tag.slug == active_tag_slug %} - - {% else %} - - {% endif %} - {% endfor %} -
-
-
-
-
-
- {% endif %} -
-
-
-
-
- {% for obj in category.documents.all %} - {% include 'documentation/partials/content_summary.html' with obj=obj kind='document' only %} - {% endfor %} -
-
-
-
-
+ {% include 'documentation/document_list.html' with category=category documents=documents tags=tags active_tag_slug=active_tag_slug only %} {% endblock content %} diff --git a/lacommunaute/templates/documentation/category_list.html b/lacommunaute/templates/documentation/category_list.html index 6597f842..f323e23a 100644 --- a/lacommunaute/templates/documentation/category_list.html +++ b/lacommunaute/templates/documentation/category_list.html @@ -23,7 +23,7 @@

{% for category in categories %} - {% include 'documentation/partials/content_summary.html' with obj=category kind='category' only %} + {% include 'documentation/partials/content_summary.html' with obj=category only %} {% endfor %}
diff --git a/lacommunaute/templates/documentation/document_list.html b/lacommunaute/templates/documentation/document_list.html new file mode 100644 index 00000000..910c323b --- /dev/null +++ b/lacommunaute/templates/documentation/document_list.html @@ -0,0 +1,60 @@ +
+ {% if tags %} +
+
+
+
+
+
Afficher les fiches contenant l'étiquette
+
+ {% for tag in tags %} + {% if tag.slug == active_tag_slug %} + + {% else %} + + {% endif %} + {% endfor %} +
+
+
+
+
+
+ {% endif %} +
+
+
+
+
+ {% for document in documents %} + {% include 'documentation/partials/content_summary.html' with obj=document category=category only %} + {% endfor %} +
+
+
+
+
+
diff --git a/lacommunaute/templates/documentation/partials/content_summary.html b/lacommunaute/templates/documentation/partials/content_summary.html index 10446561..ce07e536 100644 --- a/lacommunaute/templates/documentation/partials/content_summary.html +++ b/lacommunaute/templates/documentation/partials/content_summary.html @@ -13,8 +13,8 @@ {% if obj.short_description %}
{{ obj.short_description }}
{% endif %}