welco/welco/kb/views.py

99 lines
2.6 KiB
Python
Raw Normal View History

# 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/>.
2015-07-13 22:43:12 +02:00
import json
from django.core.urlresolvers import reverse_lazy
2015-07-13 22:43:12 +02:00
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import (DetailView, CreateView, UpdateView,
2015-07-13 22:43:12 +02:00
ListView, DeleteView, TemplateView)
2015-07-12 20:01:04 +02:00
from haystack.forms import SearchForm
from haystack.generic_views import SearchView
2015-07-13 22:43:12 +02:00
from haystack.query import SearchQuerySet
2015-07-12 20:01:04 +02:00
from .models import Page
class PageListView(ListView):
model = Page
2015-07-12 20:01:04 +02:00
def get_context_data(self, **kwargs):
context = super(PageListView, self).get_context_data(**kwargs)
context['form'] = SearchForm()
return context
page_list = PageListView.as_view()
class PageAddView(CreateView):
model = Page
page_add = PageAddView.as_view()
class PageEditView(UpdateView):
model = Page
page_edit = PageEditView.as_view()
class PageDetailView(DetailView):
model = Page
page_detail = PageDetailView.as_view()
class PageDeleteView(DeleteView):
model = Page
success_url = reverse_lazy('kb-home')
page_delete = PageDeleteView.as_view()
2015-07-12 20:01:04 +02:00
class PageSearchView(SearchView):
template_name = 'kb/page_search.html'
form_class = SearchForm
page_search = PageSearchView.as_view()
2015-07-13 22:43:12 +02:00
class KbZone(TemplateView):
template_name = 'kb/zone.html'
def get_context_data(self, **kwargs):
context = super(KbZone, self).get_context_data(**kwargs)
context['form'] = SearchForm()
return context
zone = csrf_exempt(KbZone.as_view())
def page_search_json(request):
query = request.GET.get('q')
searchqueryset = SearchQuerySet()
2015-07-13 22:51:57 +02:00
sqs = searchqueryset.autocomplete(text_auto=query)
2015-07-13 22:43:12 +02:00
sqs.load_all()
result = []
for item in sqs:
result.append({'title': item.title, 'pk': item.pk});
response = HttpResponse(content_type='application/json')
json.dump({'data': result}, response, indent=2)
return response