From 59bd9bd25c2e4772d64a35ff3dcf7a07afe72f14 Mon Sep 17 00:00:00 2001 From: vincent porte Date: Mon, 30 Sep 2024 18:18:53 +0200 Subject: [PATCH 1/5] simplify content_summary template --- lacommunaute/templates/documentation/category_list.html | 2 +- .../templates/documentation/partials/content_summary.html | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) 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/partials/content_summary.html b/lacommunaute/templates/documentation/partials/content_summary.html index 10446561..1f47138b 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 %} {% 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/document_list.html b/lacommunaute/templates/documentation/document_list.html new file mode 100644 index 00000000..1dfad49d --- /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 obj in documents %} + {% include 'documentation/partials/content_summary.html' with obj=obj category=category only %} + {% endfor %} +
+
+
+
+
+
From b5720e85387475597dcd344b1c21c4d934df5dbb Mon Sep 17 00:00:00 2001 From: vincent porte Date: Mon, 30 Sep 2024 18:21:37 +0200 Subject: [PATCH 3/5] add filtering on tag in CategoryDetailView --- .../tests_category_detail_view.ambr | 77 +++++++++++-------- .../tests/tests_category_detail_view.py | 4 + lacommunaute/documentation/views.py | 13 +++- 3 files changed, 59 insertions(+), 35 deletions(-) diff --git a/lacommunaute/documentation/tests/__snapshots__/tests_category_detail_view.ambr b/lacommunaute/documentation/tests/__snapshots__/tests_category_detail_view.ambr index abd73949..634669ab 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 @@ +
+ @@ -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 c19e48ea..628cdc44 100644 --- a/lacommunaute/documentation/tests/tests_category_detail_view.py +++ b/lacommunaute/documentation/tests/tests_category_detail_view.py @@ -20,6 +20,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 @@ -52,3 +53,6 @@ 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] + + +# test numqueries with and without tag diff --git a/lacommunaute/documentation/views.py b/lacommunaute/documentation/views.py index c26f05f2..748cbe0d 100644 --- a/lacommunaute/documentation/views.py +++ b/lacommunaute/documentation/views.py @@ -24,6 +24,16 @@ def get_template_names(self): 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), @@ -36,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 From 9b6874bbf122af2b03cb8ec129a12957c9e10440 Mon Sep 17 00:00:00 2001 From: vincent porte Date: Mon, 30 Sep 2024 18:42:40 +0200 Subject: [PATCH 4/5] add a test on queries count --- .../tests/tests_category_detail_view.py | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lacommunaute/documentation/tests/tests_category_detail_view.py b/lacommunaute/documentation/tests/tests_category_detail_view.py index 628cdc44..632d4211 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") @@ -55,4 +56,21 @@ def test_template_name(client, db, category, headers, expected_template_name): assert response.template_name == [expected_template_name] -# test numqueries with and without tag +@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 From 1286377c09e53500d76e9d78cccda23130383e8b Mon Sep 17 00:00:00 2001 From: vincent porte Date: Tue, 1 Oct 2024 11:51:45 +0200 Subject: [PATCH 5/5] thanks to tests, fix content_summary template --- .../tests_category_detail_view.ambr | 18 +++++++++--------- .../tests/tests_category_detail_view.py | 3 ++- .../templates/documentation/document_list.html | 4 ++-- .../partials/content_summary.html | 2 +- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lacommunaute/documentation/tests/__snapshots__/tests_category_detail_view.ambr b/lacommunaute/documentation/tests/__snapshots__/tests_category_detail_view.ambr index 634669ab..7dad9a05 100644 --- a/lacommunaute/documentation/tests/__snapshots__/tests_category_detail_view.ambr +++ b/lacommunaute/documentation/tests/__snapshots__/tests_category_detail_view.ambr @@ -65,11 +65,11 @@
- + - +
@@ -101,7 +101,7 @@