Add haystack index, and a view for forum search (WIP)

This commit is contained in:
Grégoire Duvauchelle 2018-10-02 12:39:55 +02:00 committed by Skia
parent a96aeba1fa
commit 9b8a881914
8 changed files with 67 additions and 1 deletions

View File

@ -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)

View File

@ -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

View File

@ -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------------------------------------*/

View File

@ -0,0 +1 @@
{{ object.message }}

View File

@ -21,6 +21,10 @@
<a href="{{ url('forum:new_forum') }}">{% trans %}New forum{% endtrans %}</a>
</p>
{% endif %}
<form action="{{ url('forum:search') }}" method="GET" id="header_search">
<input type="text" placeholder="{% trans %}Search{% endtrans %}" name="query" id="search" />
<input type="submit" value="{% trans %}Search{% endtrans %}" style="display: none;" />
</form>
<div>
<div class="ib w_big">
{% trans %}Title{% endtrans %}
@ -34,6 +38,7 @@
</div>
</div>
</div>
{% for f in forum_list %}
<div>
{{ display_forum(f, user, True) }}

View File

@ -0,0 +1,22 @@
{% extends "core/base.jinja" %}
{% from 'forum/macros.jinja' import display_message %}
{% block content %}
<div id="forum">
<div class="search-results">
{% for m in object_list %}
{% if user.can_view(m) %}
<div class="result-box">
<div class="result-topic">
{{ m }}
</div>
<div class="result-message">
{{ m.message }}
</div>
</div>
{% endif %}
{% endfor %}
</div>
</div>
{% endblock %}

View File

@ -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"),

View File

@ -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):