only find places/events where words match fixes: #58

This commit is contained in:
j 2011-10-30 12:55:46 +01:00
parent d4e1bc7e56
commit e71421f597
3 changed files with 31 additions and 12 deletions

View file

@ -2,10 +2,13 @@
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
from __future__ import division from __future__ import division
from django.conf import settings
from ox.utils import json from ox.utils import json
from ox.django.shortcuts import render_to_json_response, json_response from ox.django.shortcuts import render_to_json_response, json_response
from api.actions import actions from api.actions import actions
from annotation.models import Annotation
from item.models import Item from item.models import Item
from item import utils from item import utils
@ -81,15 +84,13 @@ def findClips(request):
qs = qs[query['range'][0]:query['range'][1]] qs = qs[query['range'][0]:query['range'][1]]
qs = qs.select_related('item__sort') qs = qs.select_related('item__sort')
response['data']['items'] = [p.json(keys=data['keys']) for p in qs] response['data']['items'] = [p.json(keys=data['keys']) for p in qs]
from django.conf import settings
from annotation.models import Annotation
keys = data['keys'] keys = data['keys']
public_layers = [l['id'] public_layers = [l['id']
for l in filter(lambda l: not l.get('private', False), for l in filter(lambda l: not l.get('private', False),
settings.CONFIG['layers'])] settings.CONFIG['layers'])]
def merge_annotations(layer, qs): def add_annotations(layer, qs):
for a in qs.values('public_id', 'value', 'clip__public_id'): for a in qs.values('public_id', 'value', 'clip__public_id'):
for i in response['data']['items']: for i in response['data']['items']:
if i['id'] == a['clip__public_id']: if i['id'] == a['clip__public_id']:
@ -99,13 +100,13 @@ def findClips(request):
'id': a['public_id'], 'id': a['public_id'],
'value': a['value'], 'value': a['value'],
}) })
if response['data']['items']:
if 'annotations' in keys: if 'annotations' in keys:
merge_annotations('annotations', add_annotations('annotations',
Annotation.objects.filter(layer__name__in=public_layers, clip__in=qs)) Annotation.objects.filter(layer__name__in=public_layers, clip__in=qs))
for layer in filter(lambda l: l in keys, public_layers): for layer in filter(lambda l: l in keys, public_layers):
merge_annotations(layer, add_annotations(layer,
Annotation.objects.filter(layer__name=layer, clip__in=qs)) Annotation.objects.filter(layer__name=layer, clip__in=qs))
elif 'position' in query: elif 'position' in query:
qs = order_query(qs, query['sort']) qs = order_query(qs, query['sort'])
ids = [i.public_id for i in qs] ids = [i.public_id for i in qs]

View file

@ -70,7 +70,16 @@ class Event(models.Model):
q = Q(value__icontains=" " + self.name)|Q(value__startswith=self.name) q = Q(value__icontains=" " + self.name)|Q(value__startswith=self.name)
for name in self.alternativeNames: for name in self.alternativeNames:
q = q|Q(value__icontains=" " + name)|Q(value__startswith=name) q = q|Q(value__icontains=" " + name)|Q(value__startswith=name)
return Annotation.objects.filter(q) matches = []
for a in Annotation.objects.filter(q):
words = ox.words(a.value)
for name in [self.name] + list(self.alternativeNames):
if name in words:
matches.append(a.id)
break
if not matches:
matches = [-1]
return Annotation.objects.filter(id__in=matches)
def update_matches(self): def update_matches(self):
matches = self.get_matches() matches = self.get_matches()

View file

@ -79,7 +79,16 @@ class Place(models.Model):
q = Q(value__contains=" " + self.name)|Q(value__startswith=self.name) q = Q(value__contains=" " + self.name)|Q(value__startswith=self.name)
for name in self.alternativeNames: for name in self.alternativeNames:
q = q|Q(value__contains=" " + name)|Q(value__startswith=name) q = q|Q(value__contains=" " + name)|Q(value__startswith=name)
return Annotation.objects.filter(q) matches = []
for a in Annotation.objects.filter(q):
words = ox.words(a.value)
for name in [self.name] + list(self.alternativeNames):
if name in words:
matches.append(a.id)
break
if not matches:
matches = [-1]
return Annotation.objects.filter(id__in=matches)
def update_matches(self): def update_matches(self):
matches = self.get_matches() matches = self.get_matches()