From 76608c8a37218798d409ca1ff989c72601e4fefb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sat, 6 Jun 2015 16:16:19 +0200 Subject: [PATCH 1/2] add indexing of pages (#6793) --- combo/apps/newsletters/models.py | 6 +++-- combo/apps/notifications/models.py | 6 ++--- combo/data/models.py | 20 +++++++++++++++- combo/data/search_indexes.py | 37 ++++++++++++++++++++++++++++++ combo/data/templates/combo/search/page.txt | 5 ++++ combo/settings.py | 10 ++++++++ 6 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 combo/data/search_indexes.py create mode 100644 combo/data/templates/combo/search/page.txt diff --git a/combo/apps/newsletters/models.py b/combo/apps/newsletters/models.py index 6e07119..9ce410c 100644 --- a/combo/apps/newsletters/models.py +++ b/combo/apps/newsletters/models.py @@ -166,5 +166,7 @@ class NewslettersCell(CellBase): context['form'] = form return super(NewslettersCell, self).render(context) - def is_relevant(self, context): - return bool(context.get('user').is_authenticated()) + def is_visible(self, user=None): + if user is None or not user.is_authenticated(): + return False + return super(NotificationsCell, self).is_visible(user) diff --git a/combo/apps/notifications/models.py b/combo/apps/notifications/models.py index f7f9435..080acc2 100644 --- a/combo/apps/notifications/models.py +++ b/combo/apps/notifications/models.py @@ -110,10 +110,10 @@ class NotificationsCell(CellBase): class Meta: verbose_name = _('User Notifications') - def is_relevant(self, context): - if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated()): + def is_visible(self, user=None): + if user is None or not user.is_authenticated(): return False - return True + return super(NotificationsCell, self).is_visible(user) def get_cell_extra_context(self, context): extra_context = super(NotificationsCell, self).get_cell_extra_context(context) diff --git a/combo/data/models.py b/combo/data/models.py index 65e850e..70a5d48 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -34,6 +34,7 @@ from django.db.models import Max from django.forms import models as model_forms from django import forms from django import template +from django.utils.html import strip_tags from django.utils.safestring import mark_safe from django.utils.text import slugify from django.utils.translation import ugettext_lazy as _ @@ -206,8 +207,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], use_natural_foreign_keys=True, use_natural_primary_keys=True))[0] del serialized_page['model'] @@ -464,6 +468,17 @@ class CellBase(models.Model): '''Apply changes to the template context that must visible to all cells in the page''' return context + def render_for_search(self): + context = Context({'synchronous': True}) + if not self.is_enabled(): + return + if not self.is_visible(user=None): + return + if not self.is_relevant(context): + return + from HTMLParser import HTMLParser + return HTMLParser().unescape(strip_tags(self.render(context))) + @register_cell_class class TextCell(CellBase): @@ -588,6 +603,9 @@ class MenuCell(CellBase): root_page=self.root_page, depth=self.depth) return ctx + def render_for_search(self): + return '' + @register_cell_class class LinkCell(CellBase): diff --git a/combo/data/search_indexes.py b/combo/data/search_indexes.py new file mode 100644 index 0000000..b48fb00 --- /dev/null +++ b/combo/data/search_indexes.py @@ -0,0 +1,37 @@ +# combo - content management system +# Copyright (C) 2014-2017 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 haystack import indexes +from haystack.exceptions import SkipDocument + +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() + + def prepare(self, obj): + if not obj.is_visible(user=None): + raise SkipDocument() + return super(PageIndex, self).prepare(obj) diff --git a/combo/data/templates/combo/search/page.txt b/combo/data/templates/combo/search/page.txt new file mode 100644 index 0000000..9d03682 --- /dev/null +++ b/combo/data/templates/combo/search/page.txt @@ -0,0 +1,5 @@ +{{object.title}} + +{% for cell in object.get_cells %} + {{ cell.render_for_search }} +{% endfor %} diff --git a/combo/settings.py b/combo/settings.py index 93cd60b..8c06f89 100644 --- a/combo/settings.py +++ b/combo/settings.py @@ -57,6 +57,7 @@ INSTALLED_APPS = ( 'django.contrib.staticfiles', 'rest_framework', 'ckeditor', + 'haystack', 'gadjo', 'cmsplugin_blurp', 'combo.data', @@ -134,6 +135,7 @@ MEDIA_URL = '/media/' CKEDITOR_UPLOAD_PATH = 'uploads/' CKEDITOR_IMAGE_BACKEND = 'pillow' + CKEDITOR_CONFIGS = { 'default': { 'allowedContent': True, @@ -150,6 +152,14 @@ CKEDITOR_CONFIGS = { }, } +HAYSTACK_CONNECTIONS = { + 'default': { + 'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine', + 'PATH': os.path.join(BASE_DIR, 'whoosh_index'), + }, +} + + COMBO_DEFAULT_PUBLIC_TEMPLATE = 'standard' COMBO_PUBLIC_TEMPLATES = { 'standard': { -- 2.11.0