Commit 9b8a8819 authored by Grégoire Duvauchelle's avatar Grégoire Duvauchelle Committed by Skia

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

parent a96aeba1
......@@ -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)
......
......@@ -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
......@@ -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------------------------------------*/
......
......@@ -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) }}
......
{% 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 %}
\ No newline at end of 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"),
......
......@@ -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):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment