mise-en-production
[auf_framonde.git] / project / framonde / search_indexes.py
CommitLineData
5f23ec55
PH
1# encoding: utf-8
2import datetime
3
4from django.core.exceptions import ObjectDoesNotExist
5from haystack import indexes
6
7from .models import Communication, Contribution, Offre
8
9
10class AufIndex(indexes.SearchIndex):
11 text = indexes.NgramField(model_attr='texte', document=True, use_template=True)
12 title = indexes.NgramField(model_attr='titre')
13 annee = indexes.FacetField(stored=True, null=True)
14 regions = indexes.FacetMultiValueField(null=True, stored=True)
15 section = indexes.FacetField(stored=True, null=True)
16 date_pub = indexes.DateField(model_attr='date_pub', null=True)
17
18# def prepare_regions(self, obj):
19# try:
20# return [b.nom for b in obj.bureau.all()]
21# except ObjectDoesNotExist as e:
22# print(e)
23# return [u'Non précisé']
24
25 def prepare_annee(self, obj):
26 if obj.date_pub is not None:
27 return str(obj.date_pub.year)
28
29
30class CommunicationIndex(AufIndex, indexes.Indexable):
31
32 def get_model(self):
33 return Communication
34
35 def prepare_section(self, obj):
36 return u"Communication"
37
38 def prepare_date_pub(self, obj):
39 return obj.date_pub.date()
40
41 def index_queryset(self, using=None):
42 """Used when the entire index for model is updated."""
43 return Communication.objects.filter(status=3)
44
45
46class ContributionIndex(AufIndex, indexes.Indexable):
47
48 def get_model(self):
49 return Contribution
50
51 def prepare_section(self, obj):
52 return u"Contribution"
53
54 def prepare_date_pub(self, obj):
55 return obj.date_pub.date()
56
57 def index_queryset(self, using=None):
58 return Contribution.objects.filter(status=3)
59
60
61class OffreIndex(AufIndex, indexes.Indexable):
62
63 def get_model(self):
64 return Offre
65
66 def prepare_section(self, obj):
67 return u"Offre"
68
69 def prepare_date_pub(self, obj):
70 return obj.date_pub.date()
71
72 def index_queryset(self, using=None):
73 return Offre.objects.filter(status=3)