kb: add indexing/searching

This commit is contained in:
Frédéric Péters 2015-07-12 20:01:04 +02:00
parent f6c514864a
commit b1d9f2b951
9 changed files with 96 additions and 1 deletions

View File

@ -2,3 +2,4 @@ Django>=1.7
gadjo
django-select2
django-ckeditor
django-haystack

View File

@ -101,6 +101,7 @@ setup(
'gadjo',
'django-select2',
'django-ckeditor',
'django-haystack',
],
zip_safe=False,
cmdclass={

View File

@ -0,0 +1,34 @@
# welco - multichannel request processing
# Copyright (C) 2015 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from HTMLParser import HTMLParser
from django.utils.html import strip_tags
from haystack import indexes
from .models import Page
class PageIndex(indexes.SearchIndex, indexes.Indexable):
title = indexes.CharField(model_attr='title', boost=1.5)
text = indexes.CharField(document=True, model_attr='content')
slug = indexes.CharField(model_attr='slug', indexed=False)
def get_model(self):
return Page
def prepare_text(self, obj):
return HTMLParser().unescape(strip_tags(obj.content))

View File

@ -9,6 +9,11 @@
{% block content %}
{% if object_list %}
<form id="kb-search" action="search/">
{{ form }}
<button>{% trans 'Search' %}</button>
</form>
<div class="objects-list">
{% for page in object_list %}
<div>

View File

@ -0,0 +1,25 @@
{% extends "kb/base.html" %}
{% load i18n %}
{% block appbar %}
<h2>{% trans 'Knowledge Base' %}</h2>
{% endblock %}
{% block content %}
<form id="kb-search">
{{ form }}
<button>{% trans 'Search' %}</button>
</form>
{% if object_list %}
<div class="objects-list">
{% for page in object_list %}
<div>
<a href="{% url 'kb-page-view' slug=page.slug %}">{{ page.title }}</a>
</div>
{% endfor %}
</div>
{% endif %}
{% endblock %}

View File

@ -18,12 +18,20 @@ from django.core.urlresolvers import reverse_lazy
from django.views.generic import (DetailView, CreateView, UpdateView,
ListView, DeleteView)
from haystack.forms import SearchForm
from haystack.generic_views import SearchView
from .models import Page
class PageListView(ListView):
model = Page
def get_context_data(self, **kwargs):
context = super(PageListView, self).get_context_data(**kwargs)
context['form'] = SearchForm()
return context
page_list = PageListView.as_view()
@ -50,3 +58,10 @@ class PageDeleteView(DeleteView):
success_url = reverse_lazy('kb-home')
page_delete = PageDeleteView.as_view()
class PageSearchView(SearchView):
template_name = 'kb/page_search.html'
form_class = SearchForm
page_search = PageSearchView.as_view()

View File

@ -40,6 +40,7 @@ INSTALLED_APPS = (
'django.contrib.staticfiles',
'django_select2',
'ckeditor',
'haystack',
'welco.sources.mail',
'welco.qualif',
'welco.kb',
@ -148,6 +149,16 @@ MELLON_USERNAME_TEMPLATE = '{attributes[name_id_content]}'
MELLON_IDENTITY_PROVIDERS = []
# indexing
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': os.path.join(BASE_DIR, 'whoosh_index'),
},
}
local_settings_file = os.environ.get('WELCO_SETTINGS_FILE',
os.path.join(os.path.dirname(__file__), 'local_settings.py'))
if os.path.exists(local_settings_file):

View File

@ -140,4 +140,6 @@ div.objects-list > div.level-1 {
margin-left: 25px;
}
form#kb-search {
margin-bottom: 2em;
}

View File

@ -26,6 +26,7 @@ urlpatterns = patterns('',
url(r'^kb/$', 'welco.kb.views.page_list', name='kb-home'),
url(r'^kb/add/$', 'welco.kb.views.page_add', name='kb-page-add'),
url(r'^kb/search/$', 'welco.kb.views.page_search', name='kb-page-search'),
url(r'^kb/(?P<slug>[\w-]+)/$', 'welco.kb.views.page_detail', name='kb-page-view'),
url(r'^kb/(?P<slug>[\w-]+)/edit$', 'welco.kb.views.page_edit', name='kb-page-edit'),
url(r'^kb/(?P<slug>[\w-]+)/delete$', 'welco.kb.views.page_delete', name='kb-page-delete'),