Skip to content

Commit

Permalink
feat(forum_search): search by model
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Jul 26, 2023
1 parent 7a49fa8 commit 2bf69e9
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lacommunaute/forum_search/forms.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
from django.forms import ChoiceField, RadioSelect
from django.utils.translation import gettext_lazy as _
from haystack.forms import FacetedSearchForm
from machina.core.loading import get_class

from lacommunaute.forum.models import Forum
from lacommunaute.forum_conversation.models import Post


PermissionHandler = get_class("forum_permission.handler", "PermissionHandler")


class SearchForm(FacetedSearchForm):
MODEL_CHOICES = (
("all", "tout le site"),
("post", "les échanges"),
("forum", "la documentation"),
)
m = ChoiceField(choices=MODEL_CHOICES, required=False, widget=RadioSelect)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.fields["m"].label = _("Search in")
self.fields["m"].initial = kwargs.pop("initial", {}).get("m", "all")

self.fields["q"].label = _("Search for keywords")
self.fields["q"].widget.attrs["placeholder"] = _("Keywords or phrase")
self.fields["q"].required = True
Expand All @@ -20,4 +34,9 @@ def search(self):
if not self.is_valid():
return self.no_query_found()

if self.cleaned_data.get("m") == "post":
sqs = sqs.models(Post)
elif self.cleaned_data.get("m") == "forum":
sqs = sqs.models(Forum)

return sqs
60 changes: 60 additions & 0 deletions lacommunaute/forum_search/tests/test_search_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,63 @@ def test_search_with_non_unicode_characters(client, db, search_url):
response = client.get(search_url, {"q": encoded_char})
assert response.status_code == 200
assert "Aucun résultat" in response.content.decode("utf-8")


def test_search_on_post_model_only(client, db, search_url, public_topics, public_forums):
datas = {"m": "post"}

query = public_topics[0].first_post.content.raw.split()[:4]
datas["q"] = " ".join(query)

response = client.get(search_url, datas)
assert response.status_code == 200
for word in query:
assert f'<span class="highlighted">{word}</span>' in response.content.decode("utf-8")

query = public_forums[0].description.raw.split()[:3]
datas["q"] = " ".join(query)

response = client.get(search_url, datas)
assert response.status_code == 200
for word in query:
assert f'<span class="highlighted">{word}</span>' not in response.content.decode("utf-8")


def test_search_on_forum_model_only(client, db, search_url, public_topics, public_forums):
datas = {"m": "forum"}

query = public_topics[0].first_post.content.raw.split()[:4]
datas["q"] = " ".join(query)

response = client.get(search_url, datas)
assert response.status_code == 200
for word in query:
assert f'<span class="highlighted">{word}</span>' not in response.content.decode("utf-8")

query = public_forums[0].description.raw.split()[:3]
datas["q"] = " ".join(query)

response = client.get(search_url, datas)
assert response.status_code == 200
for word in query:
assert f'<span class="highlighted">{word}</span>' in response.content.decode("utf-8")


def test_search_on_both_models(client, db, search_url, public_topics, public_forums):
datas = {"m": "all"}

query = public_topics[0].first_post.content.raw.split()[:4]
datas["q"] = " ".join(query)

response = client.get(search_url, datas)
assert response.status_code == 200
for word in query:
assert f'<span class="highlighted">{word}</span>' in response.content.decode("utf-8")

query = public_forums[0].description.raw.split()[:3]
datas["q"] = " ".join(query)

response = client.get(search_url, datas)
assert response.status_code == 200
for word in query:
assert f'<span class="highlighted">{word}</span>' in response.content.decode("utf-8")
8 changes: 8 additions & 0 deletions lacommunaute/forum_search/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
from haystack import views

from lacommunaute.forum_search.forms import SearchForm


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

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context["selected_choices"] = self.request.GET.getlist("choice")
return context
8 changes: 8 additions & 0 deletions lacommunaute/templates/forum_search/search_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@
</button>
</div>
</div>
<div class="form-row align-items-end">
<div class="col">
{{form.m.label}}
{% for m in form.m %}
{{m}}{{m.label}}
{% endfor %}
</div>
</div>
</form>
Binary file modified locale/fr/LC_MESSAGES/django.mo
Binary file not shown.
3 changes: 3 additions & 0 deletions locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ msgstr "Copier"
msgid "Search"
msgstr "Recherche"

msgid "Search in"
msgstr "Rechercher dans"

#: lacommunaute/templates/machina/board_base.html:28
msgid "Advanced search"
msgstr "Recherche avancée"
Expand Down

0 comments on commit 2bf69e9

Please sign in to comment.