diff --git a/lacommunaute/forum/tests/tests_views.py b/lacommunaute/forum/tests/tests_views.py index 72a411b2d..8149b3f61 100644 --- a/lacommunaute/forum/tests/tests_views.py +++ b/lacommunaute/forum/tests/tests_views.py @@ -234,7 +234,7 @@ def test_cannot_submit_post(self, *args): def test_queries(self): TopicFactory.create_batch(20, with_post=True) self.client.force_login(self.user) - with self.assertNumQueries(21): + with self.assertNumQueries(22): self.client.get(self.url) def test_certified_post_display(self): @@ -356,3 +356,54 @@ def test_share_buttons(self): self.assertContains( response, 'div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuSocialShare">' ) + + def test_has_upvoted(self): + forum = CategoryForumFactory(with_public_perms=True, with_child=True) + child_forum = forum.get_children().first() + + # anonymous + response = self.client.get( + reverse("forum_extension:forum", kwargs={"pk": child_forum.pk, "slug": child_forum.slug}) + ) + self.assertEqual(response.status_code, 200) + self.assertContains(response, '') + + # authenticated + self.client.force_login(self.user) + + response = self.client.get(reverse("forum_extension:forum", kwargs={"pk": child_forum.pk, "slug": forum.slug})) + self.assertEqual(response.status_code, 200) + self.assertContains(response, '') + + child_forum.upvotes.create(voter=self.user) + response = self.client.get( + reverse("forum_extension:forum", kwargs={"pk": child_forum.pk, "slug": child_forum.slug}) + ) + self.assertEqual(response.status_code, 200) + self.assertContains(response, '') + + def test_upvotes_count(self): + forum = CategoryForumFactory(with_public_perms=True, with_child=True) + child_forum = forum.get_children().first() + + response = self.client.get( + reverse("forum_extension:forum", kwargs={"pk": child_forum.pk, "slug": child_forum.slug}) + ) + self.assertEqual(response.status_code, 200) + self.assertContains(response, '0') + + child_forum.upvotes.create(voter=self.user) + + response = self.client.get( + reverse("forum_extension:forum", kwargs={"pk": child_forum.pk, "slug": child_forum.slug}) + ) + self.assertEqual(response.status_code, 200) + self.assertContains(response, '1') + + child_forum.upvotes.create(voter=UserFactory()) + + response = self.client.get( + reverse("forum_extension:forum", kwargs={"pk": child_forum.pk, "slug": child_forum.slug}) + ) + self.assertEqual(response.status_code, 200) + self.assertContains(response, '2') diff --git a/lacommunaute/forum/views.py b/lacommunaute/forum/views.py index a5e197eca..accec45c4 100644 --- a/lacommunaute/forum/views.py +++ b/lacommunaute/forum/views.py @@ -1,6 +1,7 @@ import logging from django.conf import settings +from django.contrib.contenttypes.models import ContentType from django.db.models import Count, Exists, OuterRef from django.db.models.query import QuerySet from django.urls import reverse @@ -12,6 +13,7 @@ from lacommunaute.forum.models import Forum from lacommunaute.forum_conversation.forms import PostForm from lacommunaute.forum_conversation.models import Topic +from lacommunaute.forum_upvote.models import UpVote from lacommunaute.users.models import User @@ -31,7 +33,18 @@ def get_queryset(self): def get_context_data(self, **kwargs): forum = self.get_forum() + + if self.request.user.is_authenticated: + forum.has_upvoted = bool( + UpVote.objects.filter( + object_id=forum.id, + voter=self.request.user, + content_type_id=ContentType.objects.get_for_model(forum).id, + ) + ) + context = super().get_context_data(**kwargs) + context["forum"] = forum context["FORUM_NUMBER_POSTS_PER_TOPIC"] = settings.FORUM_NUMBER_POSTS_PER_TOPIC context["next_url"] = reverse("forum_extension:forum", kwargs={"pk": forum.pk, "slug": self.forum.slug}) context["loadmoretopic_url"] = reverse( diff --git a/lacommunaute/templates/forum/forum_detail.html b/lacommunaute/templates/forum/forum_detail.html index 66590ff08..0251822b9 100644 --- a/lacommunaute/templates/forum/forum_detail.html +++ b/lacommunaute/templates/forum/forum_detail.html @@ -39,15 +39,12 @@

Certifié par la Plateforme de l'Inclusion le {{ forum.updated|date:"d/m/Y" }} - - {{forum.description.rendered|urlizetrunc_target_blank:30}} -
-
-
+ {% include "partials/upvotes.html" with obj=forum kind="forum"%} {% include "partials/social_share_buttons.html" with text=forum.name instance=forum %}
+ {{forum.description.rendered|urlizetrunc_target_blank:30}}