From 45d2b6f5f683d7a02c0cc74107c0cd849d1ab9e4 Mon Sep 17 00:00:00 2001 From: vincent porte Date: Wed, 21 Feb 2024 14:24:33 +0100 Subject: [PATCH] [DSP] add links to related forums --- config/settings/base.py | 2 ++ lacommunaute/surveys/tests/test_views.py | 23 ++++++++++++++++++- lacommunaute/surveys/views.py | 8 +++++++ .../templates/surveys/dsp_detail.html | 5 ++++ lacommunaute/templates/surveys/dsp_form.html | 7 ++++++ .../surveys/partials/related_forums.html | 8 +++++++ 6 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 lacommunaute/templates/surveys/partials/related_forums.html diff --git a/config/settings/base.py b/config/settings/base.py index abeffbeed..e910f01d9 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -334,6 +334,8 @@ # Django sites framework SITE_ID = 1 +DSP_FORUM_RELATED_ID = 108 + # MATOMO # --------------------------------------- MATOMO_BASE_URL = os.getenv("MATOMO_BASE_URL", None) diff --git a/lacommunaute/surveys/tests/test_views.py b/lacommunaute/surveys/tests/test_views.py index 2111c8c54..ed7ab3ea5 100644 --- a/lacommunaute/surveys/tests/test_views.py +++ b/lacommunaute/surveys/tests/test_views.py @@ -1,7 +1,10 @@ import pytest # noqa +from django.contrib.auth.models import Permission +from django.test import override_settings from django.urls import reverse from pytest_django.asserts import assertContains, assertNotContains -from django.contrib.auth.models import Permission + +from lacommunaute.forum.factories import CategoryForumFactory from lacommunaute.surveys.factories import DSPFactory from lacommunaute.surveys.models import DSP from lacommunaute.users.factories import UserFactory @@ -54,6 +57,14 @@ def test_form_fields(self, db, client): for field in dsp_choices_list: assert field in response.context["form"].fields + def test_related_forums(self, db, client): + forum = CategoryForumFactory(with_child=True) + url = reverse("surveys:dsp_create") + with override_settings(DSP_FORUM_RELATED_ID=forum.id): + response = client.get(url) + for related_forum in forum.get_children(): + assertContains(response, related_forum.name) + def test_form_valid(self, db, client): url = reverse("surveys:dsp_create") client.force_login(UserFactory()) @@ -86,6 +97,16 @@ def test_user_can_view_own_survey(self, db, client): response = client.get(url) assert response.status_code == 200 + def test_related_forums(self, db, client): + forum = CategoryForumFactory(with_child=True) + dsp = DSPFactory() + client.force_login(dsp.user) + url = reverse("surveys:dsp_detail", kwargs={"pk": dsp.pk}) + with override_settings(DSP_FORUM_RELATED_ID=forum.id): + response = client.get(url) + for related_forum in forum.get_children(): + assertContains(response, related_forum.name) + class TestHomeView: def test_link_to_dsp(self, db, client): diff --git a/lacommunaute/surveys/views.py b/lacommunaute/surveys/views.py index 1101d062e..638596b38 100644 --- a/lacommunaute/surveys/views.py +++ b/lacommunaute/surveys/views.py @@ -1,10 +1,12 @@ from collections import defaultdict +from django.conf import settings from django.contrib.auth.mixins import LoginRequiredMixin from django.urls import reverse_lazy from django.views.generic.detail import DetailView from django.views.generic.edit import CreateView +from lacommunaute.forum.models import Forum from lacommunaute.surveys.forms import DSPForm from lacommunaute.surveys.models import DSP @@ -21,6 +23,11 @@ def form_valid(self, form): form.instance.user = self.request.user return super().form_valid(form) + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["related_forums"] = Forum.objects.filter(parent__id=settings.DSP_FORUM_RELATED_ID).order_by("lft") + return context + class DSPDetailView(LoginRequiredMixin, DetailView): model = DSP @@ -42,4 +49,5 @@ def get_context_data(self, **kwargs): display_method = getattr(self.object, f"get_{field}_display") dsp_fields[modelfield.verbose_name] = display_method() context["dsp_fields"] = dsp_fields + context["related_forums"] = Forum.objects.filter(parent__id=settings.DSP_FORUM_RELATED_ID).order_by("lft") return context diff --git a/lacommunaute/templates/surveys/dsp_detail.html b/lacommunaute/templates/surveys/dsp_detail.html index 3cecbc45b..3190f8915 100644 --- a/lacommunaute/templates/surveys/dsp_detail.html +++ b/lacommunaute/templates/surveys/dsp_detail.html @@ -56,6 +56,11 @@

Rappel de votre diagnostic

{% endfor %} +
+
+ {% include "surveys/partials/related_forums.html" with related_forums=related_forums only %} +
+