From a02a3d1c3cb7df14cc0d3abdf460a5caa647e24c Mon Sep 17 00:00:00 2001 From: Marco Federighi Date: Thu, 15 Oct 2015 22:29:32 +0200 Subject: [PATCH] Update index and settings --- cms_helper.py | 5 +++- djangocms_blog/search_indexes.py | 20 +++++++++++++--- tests/base.py | 8 +++++++ tests/test_search.py | 39 ++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 tests/test_search.py diff --git a/cms_helper.py b/cms_helper.py index 2fb45e1..a74b8a1 100755 --- a/cms_helper.py +++ b/cms_helper.py @@ -89,7 +89,10 @@ HELPER_SETTINGS = dict( 'easy_thumbnails.processors.filters', ), FILE_UPLOAD_TEMP_DIR=mkdtemp(), - SITE_ID=1 + SITE_ID=1, + HAYSTACK_CONNECTIONS = { + "default": {} + } ) try: diff --git a/djangocms_blog/search_indexes.py b/djangocms_blog/search_indexes.py index ac492ac..a4433fa 100644 --- a/djangocms_blog/search_indexes.py +++ b/djangocms_blog/search_indexes.py @@ -4,6 +4,8 @@ from django.utils.encoding import force_text from aldryn_search.helpers import get_plugin_index_data from aldryn_search.utils import get_index_base, strip_tags +from haystack import indexes + from .models import Post from .settings import get_setting @@ -13,11 +15,21 @@ class PostIndex(get_index_base()): index_title = True + author = indexes.CharField(indexed=True) + #category_ids = indexes.MultiValueField(null=True) + #category_titles = indexes.MultiValueField(null=True) + + def get_author(self, post): + return post.get_author() + def get_title(self, post): return post.safe_translation_getter('title') def get_description(self, post): - return post.safe_translation_getter('abstract') + return post.get_description() + + def prepare_pub_date(self, post): + return post.date_published.strftime("%Y-%m-%d %H:%M:%S") def index_queryset(self, using=None): self._get_backend(using) @@ -36,8 +48,10 @@ class PostIndex(get_index_base()): return Post def get_search_data(self, post, language, request): - description = post.safe_translation_getter('abstract') - text_bits = [strip_tags(description)] + abstract = post.safe_translation_getter('abstract') + text_bits = [strip_tags(abstract)] + text_bits.append(post.get_description()) + text_bits.append(get_keywords) for category in post.categories.all(): text_bits.append( force_text(category.safe_translation_getter('name'))) diff --git a/tests/base.py b/tests/base.py index fc96f81..fd5f24a 100644 --- a/tests/base.py +++ b/tests/base.py @@ -7,6 +7,8 @@ from cmsplugin_filer_image.models import ThumbnailOption from django.contrib.auth import get_user_model from django.contrib.sites.models import Site from djangocms_helper.base_test import BaseTestCase +from haystack import connections +from haystack.constants import DEFAULT_ALIAS from parler.utils.context import smart_override from djangocms_blog.cms_appconfig import BlogConfig @@ -183,3 +185,9 @@ class BaseTest(BaseTestCase): post1.save() posts.append(post1) return posts + + def get_post_index(self): + search_conn = connections[DEFAULT_ALIAS] + unified_index = search_conn.get_unified_index() + index = unified_index.get_index(Post) + return index diff --git a/tests/test_search.py b/tests/test_search.py new file mode 100644 index 0000000..4aae878 --- /dev/null +++ b/tests/test_search.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, print_function, unicode_literals + +from djangocms_blog.search_indexes import PostIndex +from . import BaseTest + +from haystack.constants import DEFAULT_ALIAS + + +class PluginIndexingTests(BaseTest): + + def setUp(self): + self.index = PostIndex() + + def test_blog_post_is_indexed_using_prepare(self): + """This tests the indexing path way used by update_index mgmt command""" + post = self._get_post(self._post_data[0]['en']) + post = self._get_post(self._post_data[0]['it'], post, 'it') + index = self.get_post_index() + index.index_queryset(DEFAULT_ALIAS) # initialises index._backend_alias + indexed = index.prepare(post) + print(indexed) + self.assertEqual('First post', indexed['title']) + self.assertEqual('This is the description', indexed['description']) + self.assertEqual('First post first line This is the description category 1', indexed['text']) + self.assertEqual('/en/page-two/2015/10/15/first-post/', indexed['url']) + + def test_blog_post_is_indexed_using_update_object(self): + """This tests the indexing path way used by the RealTimeSignalProcessor""" + post = self._get_post(self._post_data[0]['en']) + post = self._get_post(self._post_data[0]['it'], post, 'it') + index = self.get_post_index() + index.update_object(post, using=DEFAULT_ALIAS) + indexed = index.prepared_data + self.assertEqual('First post', indexed['title']) + self.assertEqual('This is the description', indexed['description']) + self.assertEqual('First post first line This is the description category 1', indexed['text']) + self.assertEqual('/en/page-two/2015/10/15/first-post/', indexed['url']) +