-
Notifications
You must be signed in to change notification settings - Fork 36
/
search_indexes.py
108 lines (91 loc) · 4.55 KB
/
search_indexes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import re
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db.models import Q
from django.template import RequestContext
from django.test.client import RequestFactory
from django.utils.encoding import force_unicode
from django.utils.translation import get_language, activate
def _strip_tags(value):
"""
Returns the given HTML with all tags stripped.
This is a copy of django.utils.html.strip_tags, except that it adds some
whitespace in between replaced tags to make sure words are not erroneously
concatenated.
"""
return re.sub(r'<[^>]*?>', ' ', force_unicode(value))
try:
import importlib
except ImportError:
from django.utils import importlib
from haystack import indexes, site
from cms.models.pluginmodel import CMSPlugin
from cms_search import models as proxy_models
from cms_search import settings as search_settings
def _get_index_base():
index_string = search_settings.INDEX_BASE_CLASS
module, class_name = index_string.rsplit('.', 1)
mod = importlib.import_module(module)
base_class = getattr(mod, class_name, None)
if not base_class:
raise ImproperlyConfigured('CMS_SEARCH_INDEX_BASE_CLASS: module %s has no class %s' % (module, class_name))
if not issubclass(base_class, indexes.SearchIndex):
raise ImproperlyConfigured('CMS_SEARCH_INDEX_BASE_CLASS: %s is not a subclass of haystack.indexes.SearchIndex' % search_settings.INDEX_BASE_CLASS)
return base_class
rf = RequestFactory()
def page_index_factory(language_code):
class _PageIndex(_get_index_base()):
_language = language_code
language = indexes.CharField()
text = indexes.CharField(document=True, use_template=False)
pub_date = indexes.DateTimeField(model_attr='publication_date', null=True)
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')
site_id = indexes.IntegerField(stored=True, indexed=True, model_attr='site_id')
def prepare(self, obj):
current_languge = get_language()
try:
if current_languge != self._language:
activate(self._language)
request = rf.get("/")
request.session = {}
request.LANGUAGE_CODE = self._language
self.prepared_data = super(_PageIndex, self).prepare(obj)
plugins = CMSPlugin.objects.filter(language=language_code, placeholder__in=obj.placeholders.all())
text = u''
for base_plugin in plugins:
instance, plugin_type = base_plugin.get_plugin_instance()
if instance is None:
# this is an empty plugin
continue
if hasattr(instance, 'search_fields'):
text += u' '.join(force_unicode(_strip_tags(getattr(instance, field, ''))) for field in instance.search_fields)
if getattr(instance, 'search_fulltext', False) or getattr(plugin_type, 'search_fulltext', False):
text += _strip_tags(instance.render_plugin(context=RequestContext(request))) + u' '
text += obj.get_meta_description() or u''
text += u' '
text += obj.get_meta_keywords() or u''
self.prepared_data['text'] = text
self.prepared_data['language'] = self._language
return self.prepared_data
finally:
if get_language() != current_languge:
activate(current_languge)
def index_queryset(self):
# get the correct language and exclude pages that have a redirect
qs = super(_PageIndex, self).index_queryset()
qs = qs.published().filter(
Q(title_set__language=language_code) & (Q(title_set__redirect__exact='') | Q(title_set__redirect__isnull=True)))
if 'publisher' in settings.INSTALLED_APPS:
qs = qs.filter(publisher_is_draft=True)
qs = qs.distinct()
return qs
return _PageIndex
for language_code, language_name in settings.LANGUAGES:
proxy_model = getattr(proxy_models, proxy_models.proxy_name(language_code))
index = page_index_factory(language_code)
if proxy_model:
site.register(proxy_model, index)
else:
print "no page proxy model found for language %s" % language_code