mise-en-production
[auf_framonde.git] / project / framonde / search_indexes.py
1 # encoding: utf-8
2 import datetime
3
4 from django.core.exceptions import ObjectDoesNotExist
5 from haystack import indexes
6
7 from .models import Communication, Contribution, Offre
8
9 REGIONS = {
10 '0': 'Internationale',
11 '1': 'Afrique Centrale et des Grands-Lacs',
12 '2': 'Afrique de l\'ouest',
13 '3': 'Amériques',
14 '4': 'Asie-Pacifique',
15 '5': 'Caraïbe',
16 '6': 'Europe centrale et orientale',
17 '7': 'Moyen-Orient',
18 '8': 'Océan Indien',
19 '9': 'Maghreb',
20 '10': 'Europe de l\'Ouest'
21 }
22
23
24 class AufIndex(indexes.SearchIndex):
25 text = indexes.NgramField(document=True, use_template=True)
26 title = indexes.NgramField(model_attr='titre', stored=True)
27 region = indexes.FacetField(stored=True)
28 section = indexes.FacetField(stored=True)
29 # annee_evenement = indexes.FacetField(stored=True)
30 annee_limite = indexes.FacetField(stored=True)
31 date_pub = indexes.DateField(model_attr='date_pub')
32
33 def prepare_region(self, obj):
34 return REGIONS[obj.region]
35
36 # def prepare_annee_evenement(self, obj):
37 # if obj.date_event is not None:
38 # return str(obj.date_event.year)
39
40 def prepare_annee_limite(self, obj):
41 if obj.date_limite is not None:
42 return str(obj.date_limite.year)
43
44
45 class CommunicationIndex(AufIndex, indexes.Indexable):
46
47 def prepare_section(self, obj):
48 return u"Communication"
49
50 def get_model(self):
51 return Communication
52
53 def index_queryset(self, using=None):
54 return Communication.objects.filter(status=3).order_by('-date_pub')
55
56
57 class ContributionIndex(AufIndex, indexes.Indexable):
58
59 def prepare_section(self, obj):
60 return u"Contribution"
61
62 def get_model(self):
63 return Contribution
64
65 def index_queryset(self, using=None):
66 return Contribution.objects.filter(status=3).order_by('-date_pub')
67
68
69 class OffreIndex(AufIndex, indexes.Indexable):
70
71 def prepare_section(self, obj):
72 return u"Offre"
73
74 def get_model(self):
75 return Offre
76
77 def index_queryset(self, using=None):
78 return Offre.objects.filter(status=3).order_by('-date_pub')