Commit 6826cbb56f1f302067610356078e20c6b54733fa
1 parent
3d354922
Exists in
master
and in
39 other branches
Adding search app and type field to search_indexes
Showing
10 changed files
with
72 additions
and
2 deletions
Show diff stats
src/accounts/search_indexes.py
... | ... | @@ -17,8 +17,13 @@ class UserIndex(indexes.SearchIndex, indexes.Indexable): |
17 | 17 | google_talk = indexes.CharField(model_attr='google_talk', null=True) |
18 | 18 | webpage = indexes.CharField(model_attr='webpage', null=True) |
19 | 19 | |
20 | + type = indexes.CharField() | |
21 | + | |
20 | 22 | def get_model(self): |
21 | 23 | return User |
22 | 24 | |
25 | + def prepare_type(self, obj): | |
26 | + return u'user' | |
27 | + | |
23 | 28 | def index_queryset(self, using=None): |
24 | 29 | return self.get_model().objects.filter(is_active=True) | ... | ... |
src/colab/custom_settings.py
src/colab/urls.py
... | ... | @@ -8,6 +8,7 @@ from haystack.query import SearchQuerySet |
8 | 8 | from haystack.views import SearchView |
9 | 9 | |
10 | 10 | from accounts.models import User |
11 | +from search.forms import ColabSearchForm | |
11 | 12 | from super_archives.models import Message |
12 | 13 | |
13 | 14 | |
... | ... | @@ -16,11 +17,12 @@ admin.autodiscover() |
16 | 17 | urlpatterns = patterns('', |
17 | 18 | url(r'^$', 'colab.deprecated.views.other.home', name='home'), |
18 | 19 | |
20 | + # TODO change search to full_search with haystack | |
19 | 21 | url(r'^search/$', 'colab.deprecated.views.other.search', name='search'), |
20 | 22 | url(r'^full_search/', SearchView( |
21 | 23 | template='search/search.html', |
22 | - searchqueryset=SearchQuerySet().models(User, Message), | |
23 | - form_class=ModelSearchForm, | |
24 | + searchqueryset=SearchQuerySet(), | |
25 | + form_class=ColabSearchForm, | |
24 | 26 | ), name='haystack_search'), |
25 | 27 | |
26 | 28 | url(r'open-data/$', TemplateView.as_view(template_name='open-data.html'), | ... | ... |
... | ... | @@ -0,0 +1,45 @@ |
1 | +# -*- coding: utf-8 -*- | |
2 | + | |
3 | +from django import forms | |
4 | +from django.utils.translation import ugettext_lazy as _ | |
5 | +from haystack.forms import SearchForm | |
6 | + | |
7 | +from accounts.models import User | |
8 | +from super_archives.models import Message | |
9 | + | |
10 | + | |
11 | +class ColabSearchForm(SearchForm): | |
12 | + q = forms.CharField(label=_('Search')) | |
13 | + type = forms.CharField(required=False, label=_(u'Type')) | |
14 | + | |
15 | + def search(self): | |
16 | + if not self.is_valid(): | |
17 | + return self.no_query_found() | |
18 | + | |
19 | + if self.cleaned_data.get('q'): | |
20 | + sqs = self.searchqueryset.auto_query(self.cleaned_data['q']) | |
21 | + else: | |
22 | + sqs = self.searchqueryset.all() | |
23 | + | |
24 | + if self.cleaned_data['type']: | |
25 | + sqs = sqs.filter(type=self.cleaned_data['type']) | |
26 | + # if self.cleaned_data['type'] == 'user': | |
27 | + # sqs = self.searchqueryset.models(User) | |
28 | + # elif self.cleaned_data['type'] in ['message', 'thread']: | |
29 | + # sqs = self.searchqueryset.models(Message) | |
30 | + # elif self.cleaned_data['type'] == 'wiki': | |
31 | + # sqs = self.searchqueryset.models(Wiki) | |
32 | + # elif self.cleaned_data['type'] == 'changeset': | |
33 | + # sqs = self.searchqueryset.models(Changeset) | |
34 | + # elif self.cleaned_data['type'] == 'ticket': | |
35 | + # sqs = self.searchqueryset.models(Ticket) | |
36 | + # else: | |
37 | + # sqs = self.searchqueryset.all() | |
38 | + else: | |
39 | + sqs = self.searchqueryset.models(User, Message) | |
40 | + | |
41 | + | |
42 | + if self.load_all: | |
43 | + sqs = sqs.load_all() | |
44 | + | |
45 | + return sqs | ... | ... |
src/super_archives/search_indexes.py
... | ... | @@ -25,12 +25,17 @@ class MessageIndex(indexes.SearchIndex, indexes.Indexable): |
25 | 25 | ) |
26 | 26 | url = indexes.CharField(model_attr='url', null=True) |
27 | 27 | |
28 | + type = indexes.CharField() | |
29 | + | |
28 | 30 | def get_model(self): |
29 | 31 | return Message |
30 | 32 | |
31 | 33 | def get_updated_field(self): |
32 | 34 | return 'received_time' |
33 | 35 | |
36 | + def prepare_type(self, obj): | |
37 | + return u'thread' | |
38 | + | |
34 | 39 | def index_queryset(self, using=None): |
35 | 40 | return self.get_model().objects.filter( |
36 | 41 | thread__spam=False, spam=False | ... | ... |