Skip to content

Commit

Permalink
(search) add ask_a_question button on search page
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Jan 23, 2024
1 parent 990b53b commit 59f1b02
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 3 deletions.
164 changes: 164 additions & 0 deletions lacommunaute/forum_search/tests/__snapshots__/test_search_indexes.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# serializer version: 1
# name: test_extra_context[no_query]
'''
<main class="s-main" id="main" role="main">





<section class="s-title-01 mt-lg-5">
<div class="s-title-01__container container">
<div class="s-title-01__row row">
<div class="s-title-01__col col-12">
<h1>Chercher dans la communauté</h1>
</div>
</div>
</div>
</section>
<section class="s-section">
<div class="s-section__container container">
<div class="row">
<div class="col-12">
<div class="c-box">
<form action="." class="form" method="get" name="search_form">
<div class="form-row align-items-end">
<div class="col">

<div class="form-group" id="div_id_q">

<label class="control-label" for="id_q">
Rechercher par mots clés

</label>


<input class="form-control" id="id_q" name="q" placeholder="Mots clés ou phrase" required="" type="search"/>




</div>
</div>
<div class="col-auto mb-3">
<button class="btn btn-primary btn-ico matomo-event" data-matomo-action="search" data-matomo-category="engagement" data-matomo-option="submit_query" type="submit">
<i class="ri-search-line"></i>
<span>Rechercher</span>
</button>
</div>
</div>
<div class="form-row align-items-end">
<div class="col-auto">Rechercher dans</div>
<div class="col">
<ul class="list-inline mb-0">
<li class="list-inline-item me-3"><label for="id_m_0"><input checked="" id="id_m_0" name="m" type="radio" value="all"/>
tout le site</label></li><li class="list-inline-item me-3"><label for="id_m_1"><input id="id_m_1" name="m" type="radio" value="post"/>
les échanges</label></li><li class="list-inline-item me-3"><label for="id_m_2"><input id="id_m_2" name="m" type="radio" value="forum"/>
la documentation</label></li>
</ul>
</div>
</div>
</form>
</div>
</div>
</div>

</div>
</section>


</main>
'''
# ---
# name: test_extra_context[no_results]
'''
<main class="s-main" id="main" role="main">





<section class="s-title-01 mt-lg-5">
<div class="s-title-01__container container">
<div class="s-title-01__row row">
<div class="s-title-01__col col-12">
<h1>Chercher dans la communauté</h1>
</div>
</div>
</div>
</section>
<section class="s-section">
<div class="s-section__container container">
<div class="row">
<div class="col-12">
<div class="c-box">
<form action="." class="form" method="get" name="search_form">
<div class="form-row align-items-end">
<div class="col">

<div class="form-group" id="div_id_q">

<label class="control-label" for="id_q">
Rechercher par mots clés

</label>


<input class="form-control" id="id_q" name="q" placeholder="Mots clés ou phrase" required="" type="search" value="Bubba Gump Shrimp Co."/>




</div>
</div>
<div class="col-auto mb-3">
<button class="btn btn-primary btn-ico matomo-event" data-matomo-action="search" data-matomo-category="engagement" data-matomo-option="submit_query" type="submit">
<i class="ri-search-line"></i>
<span>Rechercher</span>
</button>
</div>
</div>
<div class="form-row align-items-end">
<div class="col-auto">Rechercher dans</div>
<div class="col">
<ul class="list-inline mb-0">
<li class="list-inline-item me-3"><label for="id_m_0"><input id="id_m_0" name="m" type="radio" value="all"/>
tout le site</label></li><li class="list-inline-item me-3"><label for="id_m_1"><input checked="" id="id_m_1" name="m" type="radio" value="post"/>
les échanges</label></li><li class="list-inline-item me-3"><label for="id_m_2"><input id="id_m_2" name="m" type="radio" value="forum"/>
la documentation</label></li>
</ul>
</div>
</div>
</form>
</div>
</div>
</div>

<div class="row mt-3">
<div class="col-12">
<div class="c-box">

<div class="row align-items-center">
<div class="col">
<h3 class="h4 mb-0">Aucun résultat ? Posez votre question dans l'espace d'échanges !</h3>
</div>
<div class="col-12 col-md-auto mt-3 mt-md-0 d-flex align-items-center justify-content-center">
<a class="btn btn-outline-primary btn-ico btn-block matomo-event" data-matomo-action="contribute" data-matomo-category="engagement" data-matomo-option="new_topic_after_search" href="/forum/forrest-gump-42/topic/create/" rel="nofollow">
<i class="ri-chat-new-line ri-lg"></i>
<span>Poser une question</span>
</a>
</div>
</div>


</div>
</div>
</div>

</div>
</section>


</main>
'''
# ---
22 changes: 22 additions & 0 deletions lacommunaute/forum_search/tests/test_search_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from lacommunaute.forum.enums import Kind as Forum_Kind
from lacommunaute.forum.factories import ForumFactory
from lacommunaute.forum_conversation.factories import PostFactory, TopicFactory
from lacommunaute.utils.testing import parse_response_to_soup


@pytest.fixture(scope="session", autouse=True)
Expand Down Expand Up @@ -96,21 +97,25 @@ def test_search_on_forum(client, db, search_url, public_forums):


def test_search_with_no_query(client, db, search_url):
ForumFactory()
response = client.get(search_url)
assertContains(response, '<input type="search" name="q"')


def test_empty_search(client, db, search_url):
ForumFactory()
response = client.get(search_url, {"q": ""})
assertContains(response, '<input type="search" name="q"')


def test_search_with_no_results(client, db, search_url):
ForumFactory()
response = client.get(search_url, {"q": "test"})
assertContains(response, "Aucun résultat")


def test_search_with_non_unicode_characters(client, db, search_url):
ForumFactory()
encoded_char = urllib.parse.quote("\x1f")
response = client.get(search_url, {"q": encoded_char})
assertContains(response, "Aucun résultat")
Expand Down Expand Up @@ -170,6 +175,7 @@ def test_search_on_both_models(client, db, search_url, public_topics, public_for


def test_non_public_forums_are_excluded(client, db, search_url):
ForumFactory()
for i, kind in enumerate([kind for kind in Forum_Kind if kind != Forum_Kind.PUBLIC_FORUM]):
ForumFactory(kind=kind, name=f"invisible {i}")
call_command("rebuild_index", noinput=True, interactive=False)
Expand All @@ -178,6 +184,7 @@ def test_non_public_forums_are_excluded(client, db, search_url):


def test_posts_from_non_public_forums_are_excluded(client, db, search_url):
ForumFactory()
for i, kind in enumerate([kind for kind in Forum_Kind if kind != Forum_Kind.PUBLIC_FORUM]):
TopicFactory(forum=ForumFactory(kind=kind), subject=f"invisible {i}", with_post=True)
call_command("rebuild_index", noinput=True, interactive=False)
Expand All @@ -202,3 +209,18 @@ def test_unapproved_post_is_exclude(client, db, search_url):
call_command("rebuild_index", noinput=True, interactive=False)
response = client.get(search_url, {"q": "emplois"})
assertContains(response, "Aucun résultat")


def test_extra_context(client, db, search_url, snapshot):
forum = ForumFactory()

response = client.get(search_url)
content = parse_response_to_soup(response, selector="main")
assert str(content) == snapshot(name="no_query")

datas = {"m": "post", "q": " ".join(["Bubba", "Gump", "Shrimp", "Co."])}
response = client.get(search_url, datas)
content = parse_response_to_soup(
response, selector="main", replace_in_href=[(forum.slug, "forrest-gump"), (str(forum.pk), "42")]
)
assert str(content) == snapshot(name="no_results")
7 changes: 7 additions & 0 deletions lacommunaute/forum_search/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
from haystack import views

from lacommunaute.forum.enums import Kind as ForumKind
from lacommunaute.forum.models import Forum
from lacommunaute.forum_search.forms import SearchForm


class FacetedSearchView(views.FacetedSearchView):
form_class = SearchForm
template = "forum_search/search.html"

def extra_context(self):
extra = super().extra_context()
extra["forum"] = Forum.objects.filter(kind=ForumKind.PUBLIC_FORUM, lft=1, level=0).first()
return extra
34 changes: 31 additions & 3 deletions lacommunaute/templates/forum_search/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,22 @@ <h1>{% trans "Search forums" %}</h1>
<div class="col-12">
<div class="c-box">
{% if page.paginator.count %}
<h3 class="h4 mb-0">{{ page.paginator.count }} {% trans "result" %}{{ page.paginator.count|pluralizefr }}</h3>
<div class="row">
<div class="col">
<h3 class="h4 mb-0">{{ page.paginator.count }} {% trans "result" %}{{ page.paginator.count|pluralizefr }}</h3>
</div>
<div class="col-12 col-md-auto mt-3 mt-md-0 d-flex align-items-center justify-content-center">
<a href="{% url 'forum_conversation:topic_create' forum.slug forum.pk %}"
rel="nofollow"
class="btn btn-outline-primary btn-ico btn-block matomo-event"
data-matomo-category="engagement"
data-matomo-action="contribute"
data-matomo-option="new_topic_after_search">
<i class="ri-chat-new-line ri-lg"></i>
<span>Poser une question</span>
</a>
</div>
</div>
<div class="table-responsive-lg">
<table class="table table-hover mt-3 mt-md-4">
<caption class="visually-hidden">Liste de résultats</caption>
Expand Down Expand Up @@ -84,8 +99,21 @@ <h3 class="h4 mb-0">{{ page.paginator.count }} {% trans "result" %}{{ page.pagin
</table>
</div>
{% else %}
<div class="d-flex flex-column flex-md-row align-items-md-center justify-content-md-between">
<h3 class="h4 mb-0">{% trans "No results." %}</h3>
<div class="row">
<div class="col">
<h3 class="h4 mb-0">Aucun résultat ? Posez votre question dans l'espace d'échanges !</h3>
</div>
<div class="col-12 col-md-auto mt-3 mt-md-0 d-flex align-items-center justify-content-center">
<a href="{% url 'forum_conversation:topic_create' forum.slug forum.pk %}"
rel="nofollow"
class="btn btn-outline-primary btn-ico btn-block matomo-event"
data-matomo-category="engagement"
data-matomo-action="contribute"
data-matomo-option="new_topic_after_search">
<i class="ri-chat-new-line ri-lg"></i>
<span>Poser une question</span>
</a>
</div>
</div>
{% endif %}
{% if page.has_previous or page.has_next %}
Expand Down

0 comments on commit 59f1b02

Please sign in to comment.