recherche: première ébauche
[auf_framonde.git] / project / aldryn_search / search_indexes.py
CommitLineData
5f23ec55
PH
1from django.db.models import Q
2from django.utils import timezone
3
4from cms.models import CMSPlugin, Title
5
6from .conf import settings
7from .helpers import get_plugin_index_data
8from .utils import clean_join, get_index_base, strip_tags
9
10
11# Backwards compatibility
12_strip_tags = strip_tags
13
14
15class TitleIndex(get_index_base()):
16 index_title = True
17
18 haystack_use_for_indexing = settings.ALDRYN_SEARCH_CMS_PAGE
19
20 def prepare_pub_date(self, obj):
21 return obj.page.publication_date
22
23 def prepare_login_required(self, obj):
24 return obj.page.login_required
25
26 def prepare_site_id(self, obj):
27 return obj.page.site_id
28
29 def get_language(self, obj):
30 return obj.language
31
32 def get_url(self, obj):
33 return obj.page.get_absolute_url()
34
35 def get_title(self, obj):
36 return obj.title
37
38 def get_description(self, obj):
39 return obj.meta_description or None
40
41 def get_plugin_queryset(self, language):
42 queryset = CMSPlugin.objects.filter(language=language)
43 return queryset
44
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)
50 text_bits = []
51
52 for base_plugin in plugins:
53 plugin_text_content = self.get_plugin_search_text(
54 base_plugin, request)
55 text_bits.append(plugin_text_content)
56
57 page_meta_description = current_page.get_meta_description(
58 fallback=False, language=language)
59
60 if page_meta_description:
61 text_bits.append(page_meta_description)
62
63 page_meta_keywords = getattr(current_page, 'get_meta_keywords', None)
64
65 if callable(page_meta_keywords):
66 text_bits.append(page_meta_keywords())
67
68 return clean_join(' ', text_bits)
69
70 def get_plugin_search_text(self, base_plugin, request):
71 try:
72 plugin_content_bits = get_plugin_index_data(base_plugin, request)
73 except:
74 return ''
75 return clean_join(' ', plugin_content_bits)
76
77 def get_model(self):
78 return Title
79
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),
87 language=language
88 ).select_related('page').distinct()
89 return queryset