add search cell (#6793)
This commit is contained in:
parent
3c5644e768
commit
189285f22b
|
@ -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'
|
|
@ -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,),
|
||||||
|
),
|
||||||
|
]
|
|
@ -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')
|
|
@ -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;
|
||||||
|
});
|
||||||
|
});
|
|
@ -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>
|
|
@ -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'))
|
|
@ -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
|
|
@ -63,6 +63,7 @@ INSTALLED_APPS = (
|
||||||
'combo.public',
|
'combo.public',
|
||||||
'combo.apps.wcs',
|
'combo.apps.wcs',
|
||||||
'combo.apps.publik',
|
'combo.apps.publik',
|
||||||
|
'combo.apps.search',
|
||||||
)
|
)
|
||||||
|
|
||||||
INSTALLED_APPS = plugins.register_plugins_apps(INSTALLED_APPS)
|
INSTALLED_APPS = plugins.register_plugins_apps(INSTALLED_APPS)
|
||||||
|
|
Loading…
Reference in New Issue