From 79f8a1d1546691b202742fe590c5e53f3f49b42b Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sat, 29 Oct 2011 13:04:25 +0200 Subject: [PATCH] get all annotations at once and merge into clips --- pandora/clip/models.py | 10 ---------- pandora/clip/views.py | 26 +++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/pandora/clip/models.py b/pandora/clip/models.py index 920f67040..1d3e54d0c 100644 --- a/pandora/clip/models.py +++ b/pandora/clip/models.py @@ -79,16 +79,6 @@ class Clip(models.Model): for key in j.keys(): if key not in keys: del j[key] - public_layers = [l['id'] - for l in filter(lambda l: not l.get('private', False), - settings.CONFIG['layers'])] - if 'annotations' in keys: - j['annotations'] = [a.json(keys=['value', 'id', 'layer']) - for a in self.annotations.filter(layer__name__in=public_layers).select_related()] - - for layer in filter(lambda l: l in keys, public_layers): - j[layer] = [a.json(keys=['id', 'value']) - for a in self.annotations.filter(layer__name=layer)] for key in keys: if key not in clip_keys and key not in j: value = self.item.get(key) diff --git a/pandora/clip/views.py b/pandora/clip/views.py index 5bfbae967..a4660c4ce 100644 --- a/pandora/clip/views.py +++ b/pandora/clip/views.py @@ -80,8 +80,32 @@ def findClips(request): qs = order_query(qs, query['sort']) qs = qs[query['range'][0]:query['range'][1]] qs = qs.select_related('item__sort') - #qs = qs.prefetch_related('annotations') 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): + for a in qs.values('public_id', 'value', 'clip__public_id'): + for i in response['data']['items']: + if i['id'] == a['clip__public_id']: + if not i[layer]: + i[layer] = [] + i[layer].append({ + '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)) elif 'position' in query: qs = order_query(qs, query['sort']) ids = [i.public_id for i in qs]