1 from django
.db
.models
import Q
2 from django
.utils
import timezone
4 from cms
.models
import CMSPlugin
, Title
6 from .conf
import settings
7 from .helpers
import get_plugin_index_data
8 from .utils
import clean_join
, get_index_base
, strip_tags
11 # Backwards compatibility
12 _strip_tags
= strip_tags
15 class TitleIndex(get_index_base()):
18 haystack_use_for_indexing
= settings
.ALDRYN_SEARCH_CMS_PAGE
20 def prepare_pub_date(self
, obj
):
21 return obj
.page
.publication_date
23 def prepare_login_required(self
, obj
):
24 return obj
.page
.login_required
26 def prepare_site_id(self
, obj
):
27 return obj
.page
.site_id
29 def get_language(self
, obj
):
32 def get_url(self
, obj
):
33 return obj
.page
.get_absolute_url()
35 def get_title(self
, obj
):
38 def get_description(self
, obj
):
39 return obj
.meta_description
or None
41 def get_plugin_queryset(self
, language
):
42 queryset
= CMSPlugin
.objects
.filter(language
=language
)
45 def get_search_data(self
, obj
, language
, request
):
46 current_page
= obj
.page
47 placeholders
= current_page
.placeholders
.all()
48 plugins
= self
.get_plugin_queryset(
49 language
).filter(placeholder__in
=placeholders
)
52 for base_plugin
in plugins
:
53 plugin_text_content
= self
.get_plugin_search_text(
55 text_bits
.append(plugin_text_content
)
57 page_meta_description
= current_page
.get_meta_description(
58 fallback
=False, language
=language
)
60 if page_meta_description
:
61 text_bits
.append(page_meta_description
)
63 page_meta_keywords
= getattr(current_page
, 'get_meta_keywords', None)
65 if callable(page_meta_keywords
):
66 text_bits
.append(page_meta_keywords())
68 return clean_join(' ', text_bits
)
70 def get_plugin_search_text(self
, base_plugin
, request
):
72 plugin_content_bits
= get_plugin_index_data(base_plugin
, request
)
75 return clean_join(' ', plugin_content_bits
)
80 def get_index_queryset(self
, language
):
81 queryset
= Title
.objects
.public().filter(
82 Q(page__publication_date__lt
=timezone
.now()) |
Q(
83 page__publication_date__isnull
=True),
84 Q(page__publication_end_date__gte
=timezone
.now()) |
Q(
85 page__publication_end_date__isnull
=True),
86 Q(redirect__exact
='') |
Q(redirect__isnull
=True),
88 ).select_related('page').distinct()