Commit 1a5d4e77 authored by Michal Čihař's avatar Michal Čihař

Implement searchin in context/source/translation

parent 233f1179
...@@ -7,7 +7,7 @@ from trans.models import Unit ...@@ -7,7 +7,7 @@ from trans.models import Unit
class Word(models.Model): class Word(models.Model):
word = models.CharField(max_length=255, db_index=True) word = models.CharField(max_length=255, db_index=True)
origin = models.ForeignKey(Language, null = True, blank = True) language = models.ForeignKey(Language, null = True, blank = True)
def __unicode__(self): def __unicode__(self):
return "%s: %s" % (self.language.name, self.word) return "%s: %s" % (self.language.name, self.word)
......
...@@ -163,14 +163,14 @@ class UnitManager(models.Manager): ...@@ -163,14 +163,14 @@ class UnitManager(models.Manager):
self.__index_item('\n'.join(unit.get_source_plurals()), Language.objects.get(code = 'en'), unit) self.__index_item('\n'.join(unit.get_source_plurals()), Language.objects.get(code = 'en'), unit)
# Index translation # Index translation
self.__index_item('\n'.join(unit.get_target_plurals()), unit.translation.language, unit) self.__index_item('\n'.join(unit.get_target_plurals()), unit.translation.language, unit)
# Index context
if unit.context != '':
self.__index_item(unit.context, None, unit)
def __get_match_rows(self, query, language = None): def __get_match_rows(self, query, language):
from ftsearch.models import Word from ftsearch.models import Word
# Grab relevant words # Grab relevant words
if language is None: word_objects = Word.objects.filter(word__in = query, language = language)
word_objects = Word.objects.filter(word__in = query)
else:
word_objects = Word.objects.filter(word__in = query, language = language)
field_list = 'w0.unit_id' field_list = 'w0.unit_id'
table_list = '' table_list = ''
...@@ -202,7 +202,7 @@ class UnitManager(models.Manager): ...@@ -202,7 +202,7 @@ class UnitManager(models.Manager):
return [row for row in rows] return [row for row in rows]
def search(self, query): def search(self, query, language):
from trans.models import Unit from trans.models import Unit
if isinstance(query, str) or isinstance(query, unicode): if isinstance(query, str) or isinstance(query, unicode):
# split the string into a list of search terms # split the string into a list of search terms
...@@ -215,7 +215,7 @@ class UnitManager(models.Manager): ...@@ -215,7 +215,7 @@ class UnitManager(models.Manager):
stemmed_query = [p.stem(s.lower()) for s in query if s != ''] stemmed_query = [p.stem(s.lower()) for s in query if s != '']
# get a row from the db for each matching word # get a row from the db for each matching word
rows = self.__get_match_rows(stemmed_query) rows = self.__get_match_rows(stemmed_query, language)
# apply the weights to each row # apply the weights to each row
weights = [(w, weight_fn(rows)) for w, weight_fn in settings.SEARCH_WEIGHTS] weights = [(w, weight_fn(rows)) for w, weight_fn in settings.SEARCH_WEIGHTS]
......
...@@ -301,13 +301,13 @@ def translate(request, project, subproject, lang): ...@@ -301,13 +301,13 @@ def translate(request, project, subproject, lang):
query |= Q(context = search_query) query |= Q(context = search_query)
units = units.filter(query) units = units.filter(query)
else: else:
units = obj.unit_set.search(search_query) units = obj.unit_set.none()
# if search_source: if search_source:
# query |= Q(source__icontains = search_query) units |= obj.unit_set.search(search_query, Language.objects.get(code = 'en'))
# if search_target: if search_target:
# query |= Q(target__icontains = search_query) units |= obj.unit_set.search(search_query, unit.translation.language)
# if search_context: if search_context:
# query |= Q(context__icontains = search_query) units |= obj.unit_set.search(search_query, None)
if direction == 'stay': if direction == 'stay':
units = units.filter(position = pos) units = units.filter(position = pos)
elif direction == 'back': elif direction == 'back':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment