-
-
Save micrypt/642206 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
from django.utils.translation import string_concat, ugettext_lazy | |
from django.utils.html import strip_tags | |
from haystack import indexes, site | |
from cms.models.managers import PageManager | |
from cms.models.pagemodel import Page | |
from cms.models.pluginmodel import CMSPlugin | |
def page_index_factory(lang, lang_name): | |
if isinstance(lang_name, basestring): | |
lang_name = ugettext_lazy(lang_name) | |
def get_absolute_url(self): | |
if 'cms.middleware.multilingual.MultilingualURLMiddleware' in settings.MIDDLEWARE_CLASSES: | |
return '/%s%s' % (lang, Page.get_absolute_url(self)) | |
else: | |
return Page.get_absolute_url(self) | |
class Meta: | |
proxy = True | |
app_label = 'cms' | |
if len(settings.LANGUAGES) > 1: | |
verbose_name = string_concat(Page._meta.verbose_name, ' (', lang_name, ')') | |
verbose_name_plural = string_concat(Page._meta.verbose_name_plural, ' (', lang_name, ')') | |
else: | |
verbose_name = Page._meta.verbose_name | |
verbose_name_plural = Page._meta.verbose_name_plural | |
attrs = {'__module__': Page.__module__, | |
'Meta': Meta, | |
'objects': PageManager(), | |
'get_absolute_url': get_absolute_url} | |
_PageProxy = type("Page_%s" % lang.title() , (Page,), attrs) | |
_PageProxy._meta.parent_attr = 'parent' | |
_PageProxy._meta.left_attr = 'lft' | |
_PageProxy._meta.right_attr = 'rght' | |
_PageProxy._meta.tree_id_attr = 'tree_id' | |
class _PageIndex(indexes.SearchIndex): | |
language = lang | |
text = indexes.CharField(document=True, use_template=False) | |
pub_date = indexes.DateTimeField(model_attr='publication_date') | |
login_required = indexes.BooleanField(model_attr='login_required') | |
url = indexes.CharField(stored=True, indexed=False, model_attr='get_absolute_url') | |
title = indexes.CharField(stored=True, indexed=False, model_attr='get_title') | |
def prepare(self, obj): | |
self.prepared_data = super(_PageIndex, self).prepare(obj) | |
plugins = CMSPlugin.objects.filter(language=lang, placeholder__in=obj.placeholders.all()) | |
text = '' | |
for plugin in plugins: | |
instance, _ = plugin.get_plugin_instance() | |
if hasattr(instance, 'search_fields'): | |
text += ''.join(strip_tags(getattr(instance, field, '')) for field in instance.search_fields) | |
if getattr(instance, 'search_fulltext', False): | |
text += strip_tags(instance.render_plugin()) | |
self.prepared_data['text'] = text | |
return self.prepared_data | |
def get_queryset(self): | |
qs = _PageProxy.objects.published().filter(title_set__language=lang).distinct() | |
if 'publisher' in settings.INSTALLED_APPS: | |
qs = qs.filter(publisher_is_draft=True) | |
return qs | |
return _PageProxy, _PageIndex | |
for lang_tuple in settings.LANGUAGES: | |
lang, lang_name = lang_tuple | |
site.register(*page_index_factory(lang, lang_name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment