summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--combo/apps/search/__init__.py26
-rw-r--r--combo/apps/search/migrations/0001_initial.py31
-rw-r--r--combo/apps/search/migrations/__init__.py0
-rw-r--r--combo/apps/search/models.py30
-rw-r--r--combo/apps/search/static/js/search.js18
-rw-r--r--combo/apps/search/templates/combo/search-cell.html8
-rw-r--r--combo/apps/search/urls.py21
-rw-r--r--combo/apps/search/views.py41
-rw-r--r--combo/settings.py1
9 files changed, 176 insertions, 0 deletions
diff --git a/combo/apps/search/__init__.py b/combo/apps/search/__init__.py
new file mode 100644
index 0000000..913f043
--- /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 <http://www.gnu.org/licenses/>.
+
+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 0000000..6f16936
--- /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 0000000..e69de29
--- /dev/null
+++ b/combo/apps/search/migrations/__init__.py
diff --git a/combo/apps/search/models.py b/combo/apps/search/models.py
new file mode 100644
index 0000000..daf1f8a
--- /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 <http://www.gnu.org/licenses/>.
+
+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 0000000..a28934b
--- /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) {
+ $('<li><a href="' + elem.url + '">' + elem.title + '</a>' +
+ '<p>' + elem.highlighted + '</p></li>').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 0000000..e4a8fce
--- /dev/null
+++ b/combo/apps/search/templates/combo/search-cell.html
@@ -0,0 +1,8 @@
+{% load i18n %}
+<script type="text/javascript" src="{{ STATIC_URL }}js/search.js"></script>
+<form id="search-cell-{{cell.id}}" class="search-form"
+ data-search-api-url="{% url 'search' pk=cell.id %}">
+ {% trans 'Search:' %} <input name="q"> <button>→</button>
+ <ul class="result">
+ </ul>
+</form>
diff --git a/combo/apps/search/urls.py b/combo/apps/search/urls.py
new file mode 100644
index 0000000..af26e1b
--- /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 <http://www.gnu.org/licenses/>.
+
+from django.conf.urls import patterns, url
+
+from . import views
+
+urlpatterns = patterns('', url('^api/search/(?P<pk>\w+)/', views.search, name='search'))
diff --git a/combo/apps/search/views.py b/combo/apps/search/views.py
new file mode 100644
index 0000000..e4cd971
--- /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 <http://www.gnu.org/licenses/>.
+
+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 63b5cad..40b61d9 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)