Commit 6826cbb56f1f302067610356078e20c6b54733fa

Authored by Luan
1 parent 3d354922

Adding search app and type field to search_indexes

src/accounts/search_indexes.py
@@ -17,8 +17,13 @@ class UserIndex(indexes.SearchIndex, indexes.Indexable): @@ -17,8 +17,13 @@ class UserIndex(indexes.SearchIndex, indexes.Indexable):
17 google_talk = indexes.CharField(model_attr='google_talk', null=True) 17 google_talk = indexes.CharField(model_attr='google_talk', null=True)
18 webpage = indexes.CharField(model_attr='webpage', null=True) 18 webpage = indexes.CharField(model_attr='webpage', null=True)
19 19
  20 + type = indexes.CharField()
  21 +
20 def get_model(self): 22 def get_model(self):
21 return User 23 return User
22 24
  25 + def prepare_type(self, obj):
  26 + return u'user'
  27 +
23 def index_queryset(self, using=None): 28 def index_queryset(self, using=None):
24 return self.get_model().objects.filter(is_active=True) 29 return self.get_model().objects.filter(is_active=True)
src/colab/custom_settings.py
@@ -41,6 +41,7 @@ INSTALLED_APPS = INSTALLED_APPS + ( @@ -41,6 +41,7 @@ INSTALLED_APPS = INSTALLED_APPS + (
41 'planet', 41 'planet',
42 'accounts', 42 'accounts',
43 'proxy', 43 'proxy',
  44 + 'search',
44 45
45 # Feedzilla and deps 46 # Feedzilla and deps
46 'feedzilla', 47 'feedzilla',
src/colab/urls.py
@@ -8,6 +8,7 @@ from haystack.query import SearchQuerySet @@ -8,6 +8,7 @@ from haystack.query import SearchQuerySet
8 from haystack.views import SearchView 8 from haystack.views import SearchView
9 9
10 from accounts.models import User 10 from accounts.models import User
  11 +from search.forms import ColabSearchForm
11 from super_archives.models import Message 12 from super_archives.models import Message
12 13
13 14
@@ -16,11 +17,12 @@ admin.autodiscover() @@ -16,11 +17,12 @@ admin.autodiscover()
16 urlpatterns = patterns('', 17 urlpatterns = patterns('',
17 url(r'^$', 'colab.deprecated.views.other.home', name='home'), 18 url(r'^$', 'colab.deprecated.views.other.home', name='home'),
18 19
  20 + # TODO change search to full_search with haystack
19 url(r'^search/$', 'colab.deprecated.views.other.search', name='search'), 21 url(r'^search/$', 'colab.deprecated.views.other.search', name='search'),
20 url(r'^full_search/', SearchView( 22 url(r'^full_search/', SearchView(
21 template='search/search.html', 23 template='search/search.html',
22 - searchqueryset=SearchQuerySet().models(User, Message),  
23 - form_class=ModelSearchForm, 24 + searchqueryset=SearchQuerySet(),
  25 + form_class=ColabSearchForm,
24 ), name='haystack_search'), 26 ), name='haystack_search'),
25 27
26 url(r'open-data/$', TemplateView.as_view(template_name='open-data.html'), 28 url(r'open-data/$', TemplateView.as_view(template_name='open-data.html'),
src/search/__init__.py 0 → 100644
src/search/admin.py 0 → 100644
@@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
  1 +from django.contrib import admin
  2 +
  3 +# Register your models here.
src/search/forms.py 0 → 100644
@@ -0,0 +1,45 @@ @@ -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/search/models.py 0 → 100644
@@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
  1 +from django.db import models
  2 +
  3 +# Create your models here.
src/search/tests.py 0 → 100644
@@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
  1 +from django.test import TestCase
  2 +
  3 +# Create your tests here.
src/search/views.py 0 → 100644
@@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
  1 +from django.shortcuts import render
  2 +
  3 +# Create your views here.
src/super_archives/search_indexes.py
@@ -25,12 +25,17 @@ class MessageIndex(indexes.SearchIndex, indexes.Indexable): @@ -25,12 +25,17 @@ class MessageIndex(indexes.SearchIndex, indexes.Indexable):
25 ) 25 )
26 url = indexes.CharField(model_attr='url', null=True) 26 url = indexes.CharField(model_attr='url', null=True)
27 27
  28 + type = indexes.CharField()
  29 +
28 def get_model(self): 30 def get_model(self):
29 return Message 31 return Message
30 32
31 def get_updated_field(self): 33 def get_updated_field(self):
32 return 'received_time' 34 return 'received_time'
33 35
  36 + def prepare_type(self, obj):
  37 + return u'thread'
  38 +
34 def index_queryset(self, using=None): 39 def index_queryset(self, using=None):
35 return self.get_model().objects.filter( 40 return self.get_model().objects.filter(
36 thread__spam=False, spam=False 41 thread__spam=False, spam=False