Skip to content

Commit

Permalink
add unanswered topics in homepage, and form to answer them
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Nov 25, 2024
1 parent fe3b64e commit 89ff9a4
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lacommunaute/pages/tests/test_homepage.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import re

import pytest
from dateutil.relativedelta import relativedelta
from django.urls import reverse
from django.utils import timezone
from pytest_django.asserts import assertContains, assertNotContains

from lacommunaute.event.factories import EventFactory
from lacommunaute.forum.factories import ForumFactory
from lacommunaute.forum_conversation.factories import TopicFactory
from lacommunaute.utils.testing import parse_response_to_soup


Expand Down Expand Up @@ -49,3 +51,26 @@ def test_events(db, client):
assertContains(response, reverse("event:detail", kwargs={"pk": future_event.pk}))
assertNotContains(response, unvisible_future_event.name)
assertContains(response, reverse("event:current"))


@pytest.mark.parametrize("nb_topics,nb_expected", [(i, i) for i in range(5)] + [(i, 4) for i in range(5, 7)])
def test_unsanswered_topics(db, client, nb_topics, nb_expected, snapshot):
TopicFactory.create_batch(nb_topics, with_post=True, with_tags=["Pirmadienis", "Poniedziałek", "Lundi", "Montag"])
response = client.get(reverse("pages:home"))
assert len(response.context_data["unanswered_topics"]) == nb_expected
if nb_expected > 0:
assert '<p class="h1 text-tertiary">Des professionnels ont besoin de votre aide !</p>' in str(response.content)


def test_numqueries(db, client, django_assert_num_queries):
savepoint_queries = 4
session_queries = 2
with django_assert_num_queries(
savepoint_queries
+ session_queries
+ 1 # get first public forum
+ 1 # get most recent updated forums in documentation
+ 1 # get unanswered topics
+ 1 # get upcoming events
):
client.get(reverse("pages:home"))
4 changes: 4 additions & 0 deletions lacommunaute/pages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from lacommunaute.event.models import Event
from lacommunaute.forum.models import Forum
from lacommunaute.forum_conversation.forms import PostForm
from lacommunaute.forum_conversation.models import Topic


logger = logging.getLogger(__name__)
Expand All @@ -28,6 +30,8 @@ def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
context["forums_category"] = Forum.objects.filter(parent__type=1).order_by("-updated")[:4]
context["forum"] = Forum.objects.get_main_forum()
context["upcoming_events"] = Event.objects.filter(date__gte=timezone.now()).order_by("date")[:4]
context["unanswered_topics"] = Topic.objects.unanswered()[:4]
context["form"] = PostForm(user=self.request.user)
return context


Expand Down
62 changes: 62 additions & 0 deletions lacommunaute/templates/forum_conversation/topic_simple_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{% load i18n %}
<div id="topicsarea">
{% if topics or not hide_if_empty %}
<hr>
{% for topic in topics %}
<div class="row">
<div class="col-12">
<div id="{{ topic.pk }}" class="post mb-3">
<div class="mb-1 d-flex flex-column flex-md-row align-items-md-center">
<p class="h4 mb-0 flex-grow-1">
<a href="{% url 'forum_conversation:topic' topic.forum.slug topic.forum.pk topic.slug topic.pk %}"
class="matomo-event"
data-matomo-category="engagement"
data-matomo-action="view"
data-matomo-option="topic">{{ topic.subject }}</a>
</p>
</div>
<div class="pt-0">
<div class="row">
<div class="col-12 post-content-wrapper mb-1">
{% include "forum_conversation/partials/poster.html" with post=topic.first_post topic=topic is_topic_head=True forum=forum %}
</div>
<div class="col-12 post-content">
<div id="showmoretopicsarea{{ topic.pk }}">
{% include 'partials/rendered_md.html' with content=topic.first_post.content truncatechars=1 only %}
{% if topic.first_post.content.rendered|length > 200 %}
<a hx-get="{% url 'forum_conversation_extension:showmore_topic' topic.forum.slug topic.forum.pk topic.slug topic.pk %}"
id="showmoretopic-button{{ topic.pk }}"
hx-target="#showmoretopicsarea{{ topic.pk }}"
hx-swap="outerHTML"
class="btn btn-link p-0 mh-auto matomo-event"
data-matomo-category="engagement"
data-matomo-action="showmore"
data-matomo-option="topic">{% trans "+ show more" %}</a>
{% endif %}
</div>
</div>
</div>
</div>
</div>
<div id="showmorepostsarea{{ topic.pk }}">
<div id="postinfeedarea{{ topic.pk }}">
{% include "forum_conversation/partials/post_feed_form_collapsable.html" with post_form=form %}
</div>
</div>
</div>
</div>
<div class="row align-items-sm-center mb-3">
<div class="col-12 col-sm">
{% include "forum_conversation/partials/topic_detail_actions.html" with posts_count=topic.posts_count %}
</div>
</div>
<hr>
{% endfor %}
{% endif %}
</div>
<script nonce="{{ request.csp_nonce }}">
var showmorepostsButtons = document.querySelectorAll('.showmoreposts-button')
showmorepostsButtons.forEach((button) => button.addEventListener('click', function() {
button.classList.add('d-none');
}));
</script>
14 changes: 14 additions & 0 deletions lacommunaute/templates/pages/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ <h1 class="h1-hero">L’espace d’entraide des professionnels de l’inclusion<
</div>
</div>
</section>
{% if unanswered_topics %}
<section class="s-section mt-0">
<div class="s-section__container container">
<div class="s-section__row row">
<div class="s-section__col col-12">
<p class="h1 text-tertiary">Des professionnels ont besoin de votre aide !</p>
{% with topics=unanswered_topics %}
{% include "forum_conversation/topic_simple_list.html" with active_tag=active_tag %}
{% endwith %}
</div>
</div>
</div>
</section>
{% endif %}
<section class="s-section my-5 my-md-10">
<div class="s-section__container container container-max-lg">
<div class="s-section__row row">
Expand Down

0 comments on commit 89ff9a4

Please sign in to comment.