diff --git a/requirements.txt b/requirements.txt index 0017216..1d21623 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ Django>=1.7 gadjo django-select2 django-ckeditor +django-haystack diff --git a/setup.py b/setup.py index 0c87733..7bfda08 100644 --- a/setup.py +++ b/setup.py @@ -101,6 +101,7 @@ setup( 'gadjo', 'django-select2', 'django-ckeditor', + 'django-haystack', ], zip_safe=False, cmdclass={ diff --git a/welco/kb/search_indexes.py b/welco/kb/search_indexes.py new file mode 100644 index 0000000..4549fde --- /dev/null +++ b/welco/kb/search_indexes.py @@ -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 . + +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)) diff --git a/welco/kb/templates/kb/page_list.html b/welco/kb/templates/kb/page_list.html index e4f37b8..ad6c6dd 100644 --- a/welco/kb/templates/kb/page_list.html +++ b/welco/kb/templates/kb/page_list.html @@ -9,6 +9,11 @@ {% block content %} {% if object_list %} + +
{% for page in object_list %}
diff --git a/welco/kb/templates/kb/page_search.html b/welco/kb/templates/kb/page_search.html new file mode 100644 index 0000000..aa7d629 --- /dev/null +++ b/welco/kb/templates/kb/page_search.html @@ -0,0 +1,25 @@ +{% extends "kb/base.html" %} +{% load i18n %} + +{% block appbar %} +

{% trans 'Knowledge Base' %}

+{% endblock %} + +{% block content %} + + +{% if object_list %} +
+ {% for page in object_list %} +
+ {{ page.title }} +
+ {% endfor %} +
+{% endif %} + + +{% endblock %} diff --git a/welco/kb/views.py b/welco/kb/views.py index 2ba9eab..7946a2c 100644 --- a/welco/kb/views.py +++ b/welco/kb/views.py @@ -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() diff --git a/welco/settings.py b/welco/settings.py index 57e70ed..a5ae869 100644 --- a/welco/settings.py +++ b/welco/settings.py @@ -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): diff --git a/welco/static/css/style.css b/welco/static/css/style.css index 6aed542..683254a 100644 --- a/welco/static/css/style.css +++ b/welco/static/css/style.css @@ -140,4 +140,6 @@ div.objects-list > div.level-1 { margin-left: 25px; } - +form#kb-search { + margin-bottom: 2em; +} diff --git a/welco/urls.py b/welco/urls.py index 9812ea1..cb52d0f 100644 --- a/welco/urls.py +++ b/welco/urls.py @@ -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[\w-]+)/$', 'welco.kb.views.page_detail', name='kb-page-view'), url(r'^kb/(?P[\w-]+)/edit$', 'welco.kb.views.page_edit', name='kb-page-edit'), url(r'^kb/(?P[\w-]+)/delete$', 'welco.kb.views.page_delete', name='kb-page-delete'),