2 from haystack import indexes
3 from migdal.models import Entry
4 from django.conf import settings
8 class EntryIndex(indexes.SearchIndex, indexes.Indexable):
9 date = indexes.DateTimeField(indexed=True)
10 author = indexes.CharField()
15 def index_queryset(self):
16 """Used when the entire index for model is updated."""
17 return self.get_model().objects.filter(date__lte=datetime.datetime.now())
20 def add_translatable(index_class, fields, languages=None):
21 """Adds some translatable fields to a search index, and a getter."""
23 languages = settings.LANGUAGES
24 for name, field in fields.items():
25 for lang_code, lang_name in languages:
26 new_field = copy(field)
27 fname = "%s_%s" % (name, lang_code)
28 new_field.index_fieldname = fname
29 setattr(index_class, fname, new_field)
30 index_class.fields[fname] = new_field
33 add_translatable(EntryIndex, {
34 'title': indexes.CharField(indexed=True, document=False),
35 'lead': indexes.CharField(indexed=True, document=False),
36 'body': indexes.CharField(indexed=True, document=False)
40 getattr(EntryIndex, "body_%s" % settings.LANGUAGE_CODE).document = True