Skip to content

Commit

Permalink
added comments search resource
Browse files Browse the repository at this point in the history
  • Loading branch information
loolmeh committed Jul 15, 2015
1 parent b3b8b2b commit aa18dec
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
20 changes: 19 additions & 1 deletion tatoeba2-django/tatoeba2/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .api_base import BaseSearchResource, UCharField, IDPaginator
from .models import Sentences
from .search_indexes import (
SentencesIndex, TagsIndex, SentencesListsIndex
SentencesIndex, TagsIndex, SentencesListsIndex, SentenceCommentsIndex
)
from datetime import datetime

Expand Down Expand Up @@ -92,3 +92,21 @@ class Meta:
filtering.update({f: SEARCH_FILTERS})

SentencesListsSearchResource._meta.filtering = filtering


class SentenceCommentsSearchResource(BaseSearchResource):
class Meta:
resource_name = 'sentence_comments_search'
index = SentenceCommentsIndex()
autoquery_fields = [
'comment_text', 'user'
]
allowed_methods = ['get']

filtering = {'django_id': SEARCH_FILTERS}

for f in SentenceCommentsSearchResource._meta.index.fields.keys():
if f == 'text': continue
filtering.update({f: SEARCH_FILTERS})

SentenceCommentsSearchResource._meta.filtering = filtering
34 changes: 33 additions & 1 deletion tatoeba2-django/tatoeba2/search_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .utils import now, stemmer, uclean
from .models import (
Sentences, Users, SentencesTranslations, UsersLanguages, Tags,
TagsSentences, SentencesLists
TagsSentences, SentencesLists, SentenceComments
)
from collections import defaultdict

Expand Down Expand Up @@ -174,3 +174,35 @@ def prepare(self, object):
self.prepared_data['is_public'] = is_public

return self.prepared_data


class SentenceCommentsIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True)
id = indexes.IntegerField(model_attr='id')
sentence_id = indexes.IntegerField(model_attr='sentence_id')
comment_text = indexes.CharField(default='')
user = indexes.CharField(default='')
created = indexes.DateTimeField(model_attr='created', default=datetime(1,1,1))
modified = indexes.DateTimeField(model_attr='modified', default=datetime(1,1,1))
hidden = indexes.IntegerField(model_attr='hidden')

def get_model(self):
return SentenceComments

def get_updated_field(self):
return 'modified'

def index_queryset(self, using=None):
return self.get_model().objects.all()

def prepare(self, object):
self.prepared_data = super(SentenceCommentsIndex, self).prepare(object)

text = object.text
user = Users.objects.filter(id=object.user_id)
user = user[0] if user else ''

self.prepared_data['comment_text'] = text
self.prepared_data['user'] = user

return self.prepared_data
3 changes: 2 additions & 1 deletion tatoeba2-django/tatoeba2/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from tastypie.api import Api
from .api import (
SentencesResource, SentencesSearchResource, TagsSearchResource,
SentencesListsSearchResource
SentencesListsSearchResource, SentenceCommentsSearchResource
)


Expand All @@ -11,6 +11,7 @@
api.register(SentencesSearchResource())
api.register(TagsSearchResource())
api.register(SentencesListsSearchResource())
api.register(SentenceCommentsSearchResource())

urlpatterns = patterns('',
url(r'^', include(api.urls)),
Expand Down

0 comments on commit aa18dec

Please sign in to comment.