Compare commits
4 Commits
main
...
wip/search
Author | SHA1 | Date |
---|---|---|
Frédéric Péters | 189285f22b | |
Frédéric Péters | 3c5644e768 | |
Frédéric Péters | 81f8a9a089 | |
Frédéric Péters | 7997a2fab9 |
|
@ -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
|
|
@ -25,6 +25,7 @@ from django.db import models
|
|||
from django.db.models import Max
|
||||
from django.forms import models as model_forms
|
||||
from django import template
|
||||
from django.utils.html import strip_tags
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
|
@ -160,8 +161,11 @@ class Page(models.Model):
|
|||
def is_visible(self, user=None):
|
||||
return element_is_visible(self, user=user)
|
||||
|
||||
def get_cells(self):
|
||||
return CellBase.get_cells(page_id=self.id)
|
||||
|
||||
def get_serialized_page(self):
|
||||
cells = CellBase.get_cells(page_id=self.id)
|
||||
cells = self.get_cells()
|
||||
serialized_page = json.loads(serializers.serialize('json', [self]))[0]
|
||||
del serialized_page['pk']
|
||||
del serialized_page['model']
|
||||
|
@ -342,6 +346,11 @@ class CellBase(models.Model):
|
|||
tmpl = template.loader.get_template(self.template_name)
|
||||
return tmpl.render(context)
|
||||
|
||||
def render_for_search(self):
|
||||
from django.template import Context
|
||||
from HTMLParser import HTMLParser
|
||||
return HTMLParser().unescape(strip_tags(self.render(Context())))
|
||||
|
||||
|
||||
@register_cell_class
|
||||
class TextCell(CellBase):
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
# combo - content management system
|
||||
# Copyright (C) 2014-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 haystack import indexes
|
||||
|
||||
from .models import Page, CellBase
|
||||
|
||||
class PageIndex(indexes.SearchIndex, indexes.Indexable):
|
||||
title = indexes.CharField(model_attr='title', boost=1.5)
|
||||
text = indexes.CharField(document=True, use_template=True,
|
||||
template_name='combo/search/page.txt')
|
||||
url = indexes.CharField(indexed=False)
|
||||
|
||||
def get_model(self):
|
||||
return Page
|
||||
|
||||
def prepare_url(self, obj):
|
||||
return obj.get_online_url()
|
|
@ -0,0 +1,5 @@
|
|||
{{object.title}}
|
||||
|
||||
{% for cell in object.get_cells %}
|
||||
{{ cell.render_for_search }}
|
||||
{% endfor %}
|
|
@ -55,6 +55,7 @@ INSTALLED_APPS = (
|
|||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'ckeditor',
|
||||
'haystack',
|
||||
'gadjo',
|
||||
'cmsplugin_blurp',
|
||||
'combo.data',
|
||||
|
@ -62,6 +63,7 @@ INSTALLED_APPS = (
|
|||
'combo.public',
|
||||
'combo.apps.wcs',
|
||||
'combo.apps.publik',
|
||||
'combo.apps.search',
|
||||
)
|
||||
|
||||
INSTALLED_APPS = plugins.register_plugins_apps(INSTALLED_APPS)
|
||||
|
@ -122,6 +124,13 @@ MEDIA_URL = '/media/'
|
|||
|
||||
CKEDITOR_UPLOAD_PATH = 'uploads/'
|
||||
|
||||
HAYSTACK_CONNECTIONS = {
|
||||
'default': {
|
||||
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
|
||||
'PATH': os.path.join(BASE_DIR, 'whoosh_index'),
|
||||
},
|
||||
}
|
||||
|
||||
CKEDITOR_CONFIGS = {
|
||||
'default': {
|
||||
'toolbar_Own': [['Source', 'Format', '-', 'Bold', 'Italic'],
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
$(function() {
|
||||
$(document).on('publik:environment-loaded', function() {
|
||||
var $content = $('#portal-agent-content');
|
||||
if ($content.length == 0) return false;
|
||||
console.log('hello2');
|
||||
});
|
||||
});
|
|
@ -15,6 +15,7 @@ $(function() {
|
|||
window.sessionStorage.hobo_environment = JSON.stringify(COMBO_KNOWN_SERVICES);
|
||||
window.sessionStorage.hobo_environment_timestamp = Date.now();
|
||||
create_menu_items();
|
||||
$(document).trigger('publik:environment-loaded');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,25 +27,13 @@ $(function() {
|
|||
if (service.data === undefined || service.data.length == 0) {
|
||||
return;
|
||||
}
|
||||
if (service.service_id == 'wcs' && service.uniq === false) {
|
||||
/* as wcs comes with many menu entries, if it's not the only instance
|
||||
* in the environment, we only insert all the items if it's the
|
||||
* currently active site.
|
||||
*/
|
||||
var that_hostname = $('<a>').attr('href', service.backoffice_menu_url)[0].hostname;
|
||||
var this_hostname = window.location.hostname;
|
||||
if (that_hostname != this_hostname) {
|
||||
more_entries.push({
|
||||
title: service.title,
|
||||
url: service.data[0].url});
|
||||
return;
|
||||
}
|
||||
}
|
||||
$(service.data).each(function(idx, element) {
|
||||
var li = $('<li><a href="#">' + element.label + '</a></li>').appendTo(menu_links);
|
||||
$(li).find('a').attr('href', element.url);
|
||||
if (element.icon !== undefined) {
|
||||
$(li).find('a').addClass('icon-' + element.icon);
|
||||
} else if (element.slug !== undefined) {
|
||||
$(li).find('a').addClass('icon-' + element.slug);
|
||||
}
|
||||
if (window.location.href.indexOf(element.url) == 0) {
|
||||
$(li).addClass('active');
|
||||
|
@ -61,14 +50,28 @@ $(function() {
|
|||
if (window.sessionStorage.hobo_environment &&
|
||||
parseInt(window.sessionStorage.hobo_environment_timestamp) > Date.now()-600000) {
|
||||
COMBO_KNOWN_SERVICES = JSON.parse(window.sessionStorage.hobo_environment);
|
||||
$(document).trigger('publik:environment-loaded');
|
||||
create_menu_items();
|
||||
} else {
|
||||
var this_hostname = window.location.hostname;
|
||||
$(COMBO_KNOWN_SERVICES).each(function(index, element) {
|
||||
if (element.backoffice_menu_url === null) {
|
||||
element.data = Array();
|
||||
check_all_done();
|
||||
return;
|
||||
}
|
||||
if (element.service_id === 'wcs' && element.uniq === false) {
|
||||
/* as wcs comes with many menu entries, if it's not the only instance
|
||||
* in the environment, we simply skip it if it's not the active site.
|
||||
*/
|
||||
var that_hostname = $('<a>').attr('href', element.backoffice_menu_url)[0].hostname;
|
||||
if (that_hostname != this_hostname) {
|
||||
element.data = Array();
|
||||
check_all_done();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$.ajax({url: element.backoffice_menu_url,
|
||||
xhrFields: { withCredentials: true },
|
||||
async: true,
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
{% block extrascripts %}
|
||||
<script src="/__services.js"></script>
|
||||
<script src="{% static "js/agent-portal.js" %}"></script>
|
||||
<script src="{% static "js/publik.js" %}"></script>
|
||||
{% endblock %}
|
||||
|
||||
|
@ -16,9 +17,11 @@
|
|||
{% block logout-url %}{% url 'auth_logout' %}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div id="portal-agent-content">
|
||||
{% block combo-content %}
|
||||
{% placeholder "content" %}
|
||||
{% endblock %}
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
{% block more-user-links %}
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
{% extends "gadjo/root.html" %}
|
||||
{% load static %}
|
||||
|
||||
{% block extrascripts %}
|
||||
<script src="/__services.js"></script>
|
||||
<script src="{% static "js/publik.js" %}"></script>
|
||||
{% endblock %}
|
Loading…
Reference in New Issue