From 9b8a8819140483de0aa7a4ded80a76e981467527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Duvauchelle?= Date: Tue, 2 Oct 2018 12:39:55 +0200 Subject: [PATCH] Add haystack index, and a view for forum search (WIP) --- core/middleware.py | 2 +- core/search_indexes.py | 8 +++++++ core/static/core/style.scss | 18 +++++++++++++++ .../indexes/forum/forummessage_text.txt | 1 + forum/templates/forum/main.jinja | 5 +++++ forum/templates/forum/search.jinja | 22 +++++++++++++++++++ forum/urls.py | 2 ++ forum/views.py | 10 +++++++++ 8 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 core/templates/search/indexes/forum/forummessage_text.txt create mode 100644 forum/templates/forum/search.jinja diff --git a/core/middleware.py b/core/middleware.py index 37c01ff9..d6479009 100644 --- a/core/middleware.py +++ b/core/middleware.py @@ -27,7 +27,7 @@ from django.conf import settings from django.utils.functional import SimpleLazyObject from django.contrib.auth import get_user from django.contrib.auth.middleware import ( - AuthenticationMiddleware as DjangoAuthenticationMiddleware, + AuthenticationMiddleware as DjangoAuthenticationMiddleware ) module, klass = settings.AUTH_ANONYMOUS_MODEL.rsplit(".", 1) diff --git a/core/search_indexes.py b/core/search_indexes.py index f157cddb..7c8a50aa 100644 --- a/core/search_indexes.py +++ b/core/search_indexes.py @@ -27,6 +27,7 @@ from django.db import models from haystack import indexes, signals from core.models import User +from forum.models import ForumMessage class UserIndex(indexes.SearchIndex, indexes.Indexable): @@ -54,3 +55,10 @@ class UserOnlySignalProcessor(signals.BaseSignalProcessor): # Disconnect only for the ``User`` model. models.signals.post_save.disconnect(self.handle_save, sender=User) models.signals.post_delete.disconnect(self.handle_delete, sender=User) + + +class ForumMessageIndex(indexes.SearchIndex, indexes.Indexable): + text = indexes.CharField(document=True, use_template=True) + + def get_model(self): + return ForumMessage diff --git a/core/static/core/style.scss b/core/static/core/style.scss index dc6ef753..ae172e95 100644 --- a/core/static/core/style.scss +++ b/core/static/core/style.scss @@ -1476,6 +1476,24 @@ textarea { } } } + + .search-results { + width: 100%; + .result-box { + width: 100%; + } + .result-topic { + display: inline-block; + background: #9f9f9f; + width: 19%; + } + .result-message { + display: inline-block; + background: #dbdbdb; + width: 80%; + + } + } } /*------------------------------SAS------------------------------------*/ diff --git a/core/templates/search/indexes/forum/forummessage_text.txt b/core/templates/search/indexes/forum/forummessage_text.txt new file mode 100644 index 00000000..2df962ae --- /dev/null +++ b/core/templates/search/indexes/forum/forummessage_text.txt @@ -0,0 +1 @@ +{{ object.message }} diff --git a/forum/templates/forum/main.jinja b/forum/templates/forum/main.jinja index 46c8e966..065007f8 100644 --- a/forum/templates/forum/main.jinja +++ b/forum/templates/forum/main.jinja @@ -21,6 +21,10 @@ {% trans %}New forum{% endtrans %}

{% endif %} +
{% trans %}Title{% endtrans %} @@ -34,6 +38,7 @@
+ {% for f in forum_list %}
{{ display_forum(f, user, True) }} diff --git a/forum/templates/forum/search.jinja b/forum/templates/forum/search.jinja new file mode 100644 index 00000000..465f20bb --- /dev/null +++ b/forum/templates/forum/search.jinja @@ -0,0 +1,22 @@ +{% extends "core/base.jinja" %} + +{% from 'forum/macros.jinja' import display_message %} + +{% block content %} +
+
+ {% for m in object_list %} + {% if user.can_view(m) %} +
+
+ {{ m }} +
+
+ {{ m.message }} +
+
+ {% endif %} + {% endfor %} +
+
+{% endblock %} \ No newline at end of file diff --git a/forum/urls.py b/forum/urls.py index 27ff5718..9a504034 100644 --- a/forum/urls.py +++ b/forum/urls.py @@ -26,8 +26,10 @@ from django.conf.urls import url from forum.views import * + urlpatterns = [ url(r"^$", ForumMainView.as_view(), name="main"), + url(r"^search/$", ForumSearchView.as_view(), name="search"), url(r"^new_forum$", ForumCreateView.as_view(), name="new_forum"), url(r"^mark_all_as_read$", ForumMarkAllAsRead.as_view(), name="mark_all_as_read"), url(r"^last_unread$", ForumLastUnread.as_view(), name="last_unread"), diff --git a/forum/views.py b/forum/views.py index 946d563c..d0296016 100644 --- a/forum/views.py +++ b/forum/views.py @@ -39,6 +39,16 @@ from ajax_select import make_ajax_field from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin from core.views.forms import MarkdownInput from forum.models import Forum, ForumMessage, ForumTopic, ForumMessageMeta +from haystack.query import SearchQuerySet + + +class ForumSearchView(ListView): + template_name = "forum/search.jinja" + + def get_queryset(self): + query = self.request.GET.get("query", "") + q = SearchQuerySet().models(ForumMessage).filter(text=query) + return [r.object for r in q] class ForumMainView(ListView):