Add basic index for blog posts

This commit is contained in:
Marco Federighi 2015-10-15 17:20:52 +02:00 committed by Iacopo Spalletti
parent fa45841d0d
commit 1869cb3e57
3 changed files with 53 additions and 0 deletions

View file

@ -0,0 +1,51 @@
# -*- coding: utf-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 .models import Post
from .settings import get_setting
class PostIndex(get_index_base()):
haystack_use_for_indexing = get_setting('ENABLE_SEARCH')
index_title = True
def get_title(self, post):
return post.safe_translation_getter('title')
def get_description(self, post):
return post.safe_translation_getter('abstract')
def index_queryset(self, using=None):
self._get_backend(using)
language = self.get_current_language(using)
filter_kwargs = self.get_index_kwargs(language)
qs = self.get_index_queryset(language)
if filter_kwargs:
return qs.translated(language, **filter_kwargs)
return qs
def get_index_queryset(self, language):
return self.get_model().objects.published().active_translations(
language_code=language)
def get_model(self):
return Post
def get_search_data(self, post, language, request):
description = post.safe_translation_getter('abstract')
text_bits = [strip_tags(description)]
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:
plugins = post.content.cmsplugin_set.filter(language=language)
for base_plugin in plugins:
content = get_plugin_index_data(base_plugin, request)
text_bits.append(content)
return ' '.join(text_bits)

View file

@ -79,5 +79,6 @@ def get_setting(name):
'BLOG_AUTO_HOME_TITLE': getattr(settings, 'BLOG_AUTO_HOME_TITLE', 'Home'),
'BLOG_AUTO_BLOG_TITLE': getattr(settings, 'BLOG_AUTO_BLOG_TITLE', 'Blog'),
'BLOG_AUTO_APP_TITLE': getattr(settings, 'BLOG_AUTO_APP_TITLE', 'Blog'),
'BLOG_ENABLE_SEARCH': getattr(settings, 'BLOG_ENABLE_SEARCH', True),
}
return default['BLOG_%s' % name]

View file

@ -49,6 +49,7 @@ setup(
'django-meta>=0.2',
'django-meta-mixin>=0.2.1',
'aldryn-apphooks-config>=0.2.6',
'aldryn-search'
],
license='BSD',
zip_safe=False,