From e71421f59794ec764ddd13347f9f1c51656af207 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sun, 30 Oct 2011 12:55:46 +0100 Subject: [PATCH] only find places/events where words match fixes: #58 --- pandora/clip/views.py | 21 +++++++++++---------- pandora/event/models.py | 11 ++++++++++- pandora/place/models.py | 11 ++++++++++- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/pandora/clip/views.py b/pandora/clip/views.py index a4660c4c..94505584 100644 --- a/pandora/clip/views.py +++ b/pandora/clip/views.py @@ -2,10 +2,13 @@ # vi:si:et:sw=4:sts=4:ts=4 from __future__ import division +from django.conf import settings from ox.utils import json from ox.django.shortcuts import render_to_json_response, json_response from api.actions import actions + +from annotation.models import Annotation from item.models import Item from item import utils @@ -81,15 +84,13 @@ def findClips(request): qs = qs[query['range'][0]:query['range'][1]] qs = qs.select_related('item__sort') 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'] public_layers = [l['id'] for l in filter(lambda l: not l.get('private', False), 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 i in response['data']['items']: if i['id'] == a['clip__public_id']: @@ -99,13 +100,13 @@ def findClips(request): 'id': a['public_id'], 'value': a['value'], }) - - if 'annotations' in keys: - merge_annotations('annotations', - Annotation.objects.filter(layer__name__in=public_layers, clip__in=qs)) - for layer in filter(lambda l: l in keys, public_layers): - merge_annotations(layer, - Annotation.objects.filter(layer__name=layer, clip__in=qs)) + if response['data']['items']: + if 'annotations' in keys: + add_annotations('annotations', + Annotation.objects.filter(layer__name__in=public_layers, clip__in=qs)) + for layer in filter(lambda l: l in keys, public_layers): + add_annotations(layer, + Annotation.objects.filter(layer__name=layer, clip__in=qs)) elif 'position' in query: qs = order_query(qs, query['sort']) ids = [i.public_id for i in qs] diff --git a/pandora/event/models.py b/pandora/event/models.py index ca4ebc0c..cae26602 100644 --- a/pandora/event/models.py +++ b/pandora/event/models.py @@ -70,7 +70,16 @@ class Event(models.Model): q = Q(value__icontains=" " + self.name)|Q(value__startswith=self.name) for name in self.alternativeNames: 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): matches = self.get_matches() diff --git a/pandora/place/models.py b/pandora/place/models.py index 49ecad2d..738bb72e 100644 --- a/pandora/place/models.py +++ b/pandora/place/models.py @@ -79,7 +79,16 @@ class Place(models.Model): q = Q(value__contains=" " + self.name)|Q(value__startswith=self.name) for name in self.alternativeNames: 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): matches = self.get_matches()