Skip to content

Commit

Permalink
feat(forum): add upvotes in forum detail view
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Jul 26, 2023
1 parent db5adb4 commit 9177935
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
53 changes: 52 additions & 1 deletion lacommunaute/forum/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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, '<i class="ri-bookmark-line" aria-hidden="true"></i>')

# 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, '<i class="ri-bookmark-line" aria-hidden="true"></i>')

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, '<i class="ri-bookmark-fill" aria-hidden="true"></i>')

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, '<i class="ri-bookmark-line" aria-hidden="true"></i><span class="ml-1">0</span>')

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, '<i class="ri-bookmark-line" aria-hidden="true"></i><span class="ml-1">1</span>')

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, '<i class="ri-bookmark-line" aria-hidden="true"></i><span class="ml-1">2</span>')
13 changes: 13 additions & 0 deletions lacommunaute/forum/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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


Expand All @@ -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(
Expand Down
7 changes: 2 additions & 5 deletions lacommunaute/templates/forum/forum_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,12 @@ <h2 class="mt-3">
Certifié par la Plateforme de l'Inclusion le {{ forum.updated|date:"d/m/Y" }}
</span>
</div>
</div>
{{forum.description.rendered|urlizetrunc_target_blank:30}}
<div class="row align-items-sm-center mb-3">
<div class="col-12 col-sm">
</div>
<div class="col-12 col-sm-auto">
{% include "partials/upvotes.html" with obj=forum kind="forum"%}
{% include "partials/social_share_buttons.html" with text=forum.name instance=forum %}
</div>
</div>
{{forum.description.rendered|urlizetrunc_target_blank:30}}
</div>
<div class="col-12 col-lg-3">
<a href="{% url 'forum_extension:forum' parent_forum.slug parent_forum.id %}"
Expand Down

0 comments on commit 9177935

Please sign in to comment.