From 55bf32435b07f4824cc4fedfeed2bdaed3fc2a78 Mon Sep 17 00:00:00 2001 From: Tadas Dailyda Date: Mon, 15 Feb 2016 18:06:04 +0200 Subject: [PATCH 1/4] fix Post.get_keywords to handle empty keywords, cleanup search_indexes --- djangocms_blog/models.py | 2 +- djangocms_blog/search_indexes.py | 49 ++++++++++++++++---------------- tests/test_search.py | 8 ++++-- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/djangocms_blog/models.py b/djangocms_blog/models.py index e0fa27e..d38b504 100644 --- a/djangocms_blog/models.py +++ b/djangocms_blog/models.py @@ -232,7 +232,7 @@ class Post(ModelMeta, TranslatableModel): return title.strip() def get_keywords(self): - return self.safe_translation_getter('meta_keywords').strip().split(',') + return self.safe_translation_getter('meta_keywords', default='').strip().split(',') def get_locale(self): return self.get_current_language() diff --git a/djangocms_blog/search_indexes.py b/djangocms_blog/search_indexes.py index 01baee9..9f56cae 100644 --- a/djangocms_blog/search_indexes.py +++ b/djangocms_blog/search_indexes.py @@ -15,12 +15,9 @@ class PostIndex(get_index_base()): author = indexes.CharField(indexed=True, model_attr='get_author') keywords = indexes.CharField(null=True) - tags = indexes.CharField(null=True) + tags = indexes.CharField(null=True, model_attr='get_tags') post_text = indexes.CharField(null=True) - def get_keywords(self, post): - return ','.join(post.get_keywords()) - def get_title(self, post): return post.safe_translation_getter('title') @@ -28,7 +25,7 @@ class PostIndex(get_index_base()): return post.get_description() def prepare_pub_date(self, post): - return post.date_published.strftime("%Y-%m-%d %H:%M:%S") + return post.date_published def index_queryset(self, using=None): self._get_backend(using) @@ -47,32 +44,36 @@ class PostIndex(get_index_base()): return Post def get_search_data(self, post, language, request): - optional_attributes = [] - abstract = post.safe_translation_getter('abstract') + description = post.get_description() + abstract = strip_tags(post.safe_translation_getter('abstract', default='')) + keywords = post.get_keywords() + text_bits = [post.get_title()] - text_bits.append(strip_tags(abstract)) - text_bits.append(post.get_description()) - text_bits.append(' '.join(post.get_keywords())) + if abstract: + text_bits.append(abstract) + if description: + text_bits.append(description) + if keywords: + text_bits.append(' '.join(keywords)) + self.prepared_data['keywords'] = ','.join(keywords) for category in post.categories.all(): text_bits.append( force_text(category.safe_translation_getter('name'))) for tag in post.tags.all(): text_bits.append(force_text(tag.name)) - if post.content: + + if get_setting('USE_PLACEHOLDER'): plugins = post.content.cmsplugin_set.filter(language=language) + content_bits = [] for base_plugin in plugins: content = get_plugin_index_data(base_plugin, request) - text_bits.append(' '.join(content)) - for attribute in optional_attributes: - value = force_text(getattr(post, attribute)) - if value and value not in text_bits: - text_bits.append(value) - return ' '.join(text_bits) + content_bits.append(' '.join(content)) + post_text = ' '.join(content_bits) + else: + post_text = post.safe_translation_getter('post_text') + if post_text: + post_text = strip_tags(post_text) + self.prepared_data['post_text'] = post_text + text_bits.append(post_text) - def prepare_fields(self, post, language, request): - super(PostIndex, self).prepare_fields(post, language, request) - data = [self.prepared_data['text']] - self.prepared_data['keywords'] = ' '.join(post.get_keywords()) - self.prepared_data['tags'] = ' '.join(post.get_tags()) - self.prepared_data['post_text'] = ' '.join(post.safe_translation_getter('post_text')) - self.prepared_data['text'] = ' '.join(data) + return ' '.join(text_bits) diff --git a/tests/test_search.py b/tests/test_search.py index cf30013..779ceec 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -28,12 +28,12 @@ class BlogIndexingTests(BaseTest): index.index_queryset(DEFAULT_ALIAS) # initialises index._backend_alias indexed = index.prepare(post) - self.assertEqual(post.get_title(), indexed['title']) self.assertEqual(post.get_description(), indexed['description']) + self.assertEqual(post.get_tags(), indexed['tags']) self.assertEqual(self.sample_text, indexed['text']) self.assertEqual(post.get_absolute_url(), indexed['url']) - #self.assertEqual(post.date_published.strftime("%Y-%m-%d %H:%M:%S"), indexed['pub_date']) + self.assertEqual(post.date_published, indexed['pub_date']) def test_blog_post_is_indexed_using_update_object(self): """This tests the indexing path way used by the RealTimeSignalProcessor""" @@ -44,13 +44,15 @@ class BlogIndexingTests(BaseTest): index = self.get_post_index() index.update_object(post, using=DEFAULT_ALIAS) + index = self.get_post_index() indexed = index.prepared_data self.assertEqual(post.get_title(), indexed['title']) self.assertEqual(post.get_description(), indexed['description']) + self.assertEqual(post.get_tags(), indexed['tags']) self.assertEqual(self.sample_text, indexed['text']) self.assertEqual(post.get_absolute_url(), indexed['url']) - #self.assertEqual(post.date_published.strftime("%Y-%m-%d %H:%M:%S"), indexed['pub_date']) + self.assertEqual(post.date_published, indexed['pub_date']) def test_searchqueryset(self): posts = self.get_posts() From a85cc1616f09d8e977c6277622d2c01ba29e5ae7 Mon Sep 17 00:00:00 2001 From: Tadas Dailyda Date: Tue, 16 Feb 2016 20:23:40 +0200 Subject: [PATCH 2/4] remove defunct test --- tests/test_search.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/tests/test_search.py b/tests/test_search.py index 779ceec..8d518ae 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -35,25 +35,6 @@ class BlogIndexingTests(BaseTest): self.assertEqual(post.get_absolute_url(), indexed['url']) self.assertEqual(post.date_published, indexed['pub_date']) - 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') - post.tags.add('a tag') - add_plugin(post.content, 'TextPlugin', language='en', body='test body') - - index = self.get_post_index() - index.update_object(post, using=DEFAULT_ALIAS) - index = self.get_post_index() - indexed = index.prepared_data - - self.assertEqual(post.get_title(), indexed['title']) - self.assertEqual(post.get_description(), indexed['description']) - self.assertEqual(post.get_tags(), indexed['tags']) - self.assertEqual(self.sample_text, indexed['text']) - self.assertEqual(post.get_absolute_url(), indexed['url']) - self.assertEqual(post.date_published, indexed['pub_date']) - def test_searchqueryset(self): posts = self.get_posts() all_results = SearchQuerySet().models(Post) From f28a2cd6d106822ba7c761cd7a985d17eff6bda3 Mon Sep 17 00:00:00 2001 From: Tadas Dailyda Date: Wed, 17 Feb 2016 10:22:26 +0200 Subject: [PATCH 3/4] don't repeat blog title in search index twice --- djangocms_blog/search_indexes.py | 2 +- tests/test_search.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/djangocms_blog/search_indexes.py b/djangocms_blog/search_indexes.py index 9f56cae..49f9d9b 100644 --- a/djangocms_blog/search_indexes.py +++ b/djangocms_blog/search_indexes.py @@ -48,7 +48,7 @@ class PostIndex(get_index_base()): abstract = strip_tags(post.safe_translation_getter('abstract', default='')) keywords = post.get_keywords() - text_bits = [post.get_title()] + text_bits = [] if abstract: text_bits.append(abstract) if description: diff --git a/tests/test_search.py b/tests/test_search.py index 8d518ae..26f69d4 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -11,7 +11,7 @@ from .base import BaseTest class BlogIndexingTests(BaseTest): - sample_text = ('First post First post first line This is the description keyword1 ' + sample_text = ('First post first line This is the description keyword1 ' 'keyword2 category 1 a tag test body') def setUp(self): From d3f64ccdba20b3693c338b5691a7d9e2be0bd4f9 Mon Sep 17 00:00:00 2001 From: Tadas Dailyda Date: Fri, 19 Feb 2016 13:31:11 +0200 Subject: [PATCH 4/4] use post.get_title() to get fallback title from meta --- djangocms_blog/search_indexes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/djangocms_blog/search_indexes.py b/djangocms_blog/search_indexes.py index 49f9d9b..2e9c8d0 100644 --- a/djangocms_blog/search_indexes.py +++ b/djangocms_blog/search_indexes.py @@ -19,7 +19,7 @@ class PostIndex(get_index_base()): post_text = indexes.CharField(null=True) def get_title(self, post): - return post.safe_translation_getter('title') + return post.get_title() def get_description(self, post): return post.get_description()