Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Jul 25, 2023
1 parent 7a49fa8 commit 5c3562a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lacommunaute/forum_search/forms.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
from django.forms import CheckboxSelectMultiple, Form, MultipleChoiceField, 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):
CHOICES = (
("post", "post"),
("forum", "forum"),
)
choice = MultipleChoiceField(choices=CHOICES, required=False, widget=CheckboxSelectMultiple)

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

Expand All @@ -20,4 +30,16 @@ def search(self):
if not self.is_valid():
return self.no_query_found()

selected_choices = self.cleaned_data.get("choice")
if selected_choices:
if "post" in selected_choices:
selected_choices.remove("post")
selected_choices.append(Post)

if "forum" in selected_choices:
selected_choices.remove("forum")
selected_choices.append(Forum)

sqs = sqs.models(*selected_choices)

return sqs
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
12 changes: 12 additions & 0 deletions lacommunaute/templates/forum_search/search_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
<div class="col">
{% include "partials/form_field.html" with field=form.q %}
</div>
<div class="col">

{% for choice, label in form.choice.field.choices %}

<label>
<input type="checkbox" name="choice" value="{{ choice }}" {% if choice in selected_choices %}checked{% endif %}>
{{ label }}
</label>

{% endfor %}

</div>
<div class="col-auto mb-3">
<button type="submit"
class="btn btn-primary btn-ico matomo-event"
Expand Down

0 comments on commit 5c3562a

Please sign in to comment.