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