diff --git a/combo/apps/search/__init__.py b/combo/apps/search/__init__.py new file mode 100644 index 00000000..913f0437 --- /dev/null +++ b/combo/apps/search/__init__.py @@ -0,0 +1,26 @@ +# combo - content management system +# 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 . + +import django.apps + +class AppConfig(django.apps.AppConfig): + name = 'combo.apps.search' + + def get_after_urls(self): + from . import urls + return urls.urlpatterns + +default_app_config = 'combo.apps.search.AppConfig' diff --git a/combo/apps/search/migrations/0001_initial.py b/combo/apps/search/migrations/0001_initial.py new file mode 100644 index 00000000..6f169367 --- /dev/null +++ b/combo/apps/search/migrations/0001_initial.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('auth', '0001_initial'), + ('data', '0009_auto_20150529_2247'), + ] + + operations = [ + migrations.CreateModel( + name='SearchCell', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('placeholder', models.CharField(max_length=20)), + ('order', models.PositiveIntegerField()), + ('slug', models.SlugField(verbose_name='Slug', blank=True)), + ('public', models.BooleanField(default=True, verbose_name='Public')), + ('groups', models.ManyToManyField(to='auth.Group', verbose_name='Groups', blank=True)), + ('page', models.ForeignKey(to='data.Page')), + ], + options={ + 'verbose_name': 'Search', + }, + bases=(models.Model,), + ), + ] diff --git a/combo/apps/search/migrations/__init__.py b/combo/apps/search/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/combo/apps/search/models.py b/combo/apps/search/models.py new file mode 100644 index 00000000..daf1f8aa --- /dev/null +++ b/combo/apps/search/models.py @@ -0,0 +1,30 @@ +# combo - content management system +# 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 django import template +from django.db import models +from django.forms import models as model_forms +from django.utils.translation import ugettext_lazy as _ + +from combo.data.models import CellBase +from combo.data.library import register_cell_class + +@register_cell_class +class SearchCell(CellBase): + template_name = 'combo/search-cell.html' + + class Meta: + verbose_name = _('Search') diff --git a/combo/apps/search/static/js/search.js b/combo/apps/search/static/js/search.js new file mode 100644 index 00000000..a28934bf --- /dev/null +++ b/combo/apps/search/static/js/search.js @@ -0,0 +1,18 @@ +$(function() { + $('.search-form').on('submit', function() { + var q = $(this).find('input').val(); + var search_result_ul = $(this).find('ul.result'); + search_result_ul.empty(); + $.getJSON($(this).data('search-api-url'), + {'q': q}, + function (response) { + console.log('success!', response); + $(response.data).each(function(idx, elem) { + $('
  • ' + elem.title + '' + + '

    ' + elem.highlighted + '

  • ').appendTo(search_result_ul); + }); + } + ); + return false; + }); +}); diff --git a/combo/apps/search/templates/combo/search-cell.html b/combo/apps/search/templates/combo/search-cell.html new file mode 100644 index 00000000..e4a8fcee --- /dev/null +++ b/combo/apps/search/templates/combo/search-cell.html @@ -0,0 +1,8 @@ +{% load i18n %} + +
    + {% trans 'Search:' %} +
      +
    +
    diff --git a/combo/apps/search/urls.py b/combo/apps/search/urls.py new file mode 100644 index 00000000..af26e1b2 --- /dev/null +++ b/combo/apps/search/urls.py @@ -0,0 +1,21 @@ +# combo - content management system +# 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 django.conf.urls import patterns, url + +from . import views + +urlpatterns = patterns('', url('^api/search/(?P\w+)/', views.search, name='search')) diff --git a/combo/apps/search/views.py b/combo/apps/search/views.py new file mode 100644 index 00000000..e4cd9711 --- /dev/null +++ b/combo/apps/search/views.py @@ -0,0 +1,41 @@ +# combo - content management system +# 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 . + +import json + +from django.http import HttpResponse +from haystack.query import SearchQuerySet + +from .models import SearchCell + + +def search(request, pk): + cell = SearchCell.objects.get(id=pk) + # TODO: check the cell page is accessible to user, alter search parameters, + # etc. + query = request.GET.get('q') + searchqueryset = SearchQuerySet() + sqs = searchqueryset.auto_query(query).highlight() + sqs.load_all() + + result = [] + for item in sqs: + result.append({'title': item.title, 'url': item.url, + 'highlighted': item.highlighted['text'][0]}) + + response = HttpResponse(content_type='application/json') + json.dump({'data': result}, response, indent=2) + return response diff --git a/combo/settings.py b/combo/settings.py index 63b5cadf..40b61d97 100644 --- a/combo/settings.py +++ b/combo/settings.py @@ -63,6 +63,7 @@ INSTALLED_APPS = ( 'combo.public', 'combo.apps.wcs', 'combo.apps.publik', + 'combo.apps.search', ) INSTALLED_APPS = plugins.register_plugins_apps(INSTALLED_APPS)