__author__ alehrudzko from haystack import indexes class CreatedAtInde

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
__author__ = 'alehrudzko'
from haystack import indexes
class CreatedAtIndexMixin(indexes.SearchIndex):
created_at = indexes.DateTimeField(model_attr='created_at')
class LocationIndexMixin(indexes.SearchIndex):
city = indexes.CharField(null=True)
country = indexes.CharField(null=True)
continent = indexes.CharField(null=True)
def prepare_city(self, obj):
if obj.location is None:
return None
return obj.location.title
def prepare_continent(self, obj):
if obj.location is None:
return None
return obj.location.country.continent
def prepare_country(self, obj):
if obj.location is None:
return None
return obj.location.country
class TagsIndexMixin(indexes.SearchIndex):
genres = indexes.MultiValueField(faceted=True)
categories = indexes.MultiValueField(faceted=True)
subcategories = indexes.MultiValueField(faceted=True)
def prepare_categories(self, obj):
return [str(category) for category in obj.categories.filter(parent__isnull=True)]
def prepare_subcategories(self, obj):
return [str(subcategory) for subcategory in obj.categories.filter(parent__isnull=False)]
def prepare_genres(self, obj):
return [str(genre) for genre in obj.genres.all()]
class TweetTargetIndexMixin(indexes.SearchIndex):
target_tweets = indexes.MultiValueField(faceted=True)
def prepare_target_tweets(self, obj):
return [str(tweet) for tweet in obj.target_tweets.all()]
class EditorsChoiceIndexMixin(indexes.SearchIndex):
editors_choice = indexes.BooleanField(model_attr='editors_choice')
class NewsIndex(CreatedAtIndexMixin, LocationIndexMixin, TagsIndexMixin, TweetTargetIndexMixin,
EditorsChoiceIndexMixin, indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
news_type = indexes.CharField(model_attr='category', faceted=True, null=True)
def get_model(self):
return News
def index_queryset(self, using=None):
return self.get_model().objects.all_active()