only return matching annotations in findClips if query contains annotation conditions

This commit is contained in:
j 2012-01-05 18:01:56 +05:30
parent 2e4d6fd9d9
commit 09389f84dc
5 changed files with 52 additions and 7 deletions

View file

@ -28,6 +28,7 @@ def parseCondition(condition, user):
'out': 'end', 'out': 'end',
'place': 'annotations__places__id', 'place': 'annotations__places__id',
'text': 'annotations__findvalue', 'text': 'annotations__findvalue',
'annotations': 'annotations__findvalue',
'user': 'annotations__user__username', 'user': 'annotations__user__username',
}.get(k, k) }.get(k, k)
if not k: if not k:
@ -139,6 +140,40 @@ class ClipManager(Manager):
def get_query_set(self): def get_query_set(self):
return QuerySet(self.model) return QuerySet(self.model)
def filter_annotations(self, data, user):
public_layers = [l['id']
for l in filter(lambda l: not l.get('private', False),
settings.CONFIG['layers'])]
keys = public_layers + ['annotations', 'text', '*']
conditions = data.get('query', {}).get('conditions', [])
conditions = filter(lambda c: c['key'] in keys, conditions)
operator = data.get('query', {}).get('operator', '&')
def parse(condition):
key = "%s%s" % ('findvalue', {
'>': '__gt',
'>=': '__gte',
'<': '__lt',
'<=': '__lte',
'==': '__iexact',
'=': '__icontains',
'^': '__istartswith',
'$': '__iendswith',
}.get(condition.get('opterator', ''), '__icontains'))
q = Q(**{key: condition['value']})
if condition['key'] in public_layers:
q = q & Q(layer=condition['key'])
return q
conditions = map(parse, conditions)
if conditions:
q = conditions[0]
for c in conditions[1:]:
if operator == '|':
q = q | c
else:
q = q & c
return q
return None
def find(self, data, user): def find(self, data, user):
''' '''
query: { query: {

View file

@ -42,7 +42,7 @@ class MetaClip:
clip_keys = ('id', 'in', 'out', 'position', 'created', 'modified', clip_keys = ('id', 'in', 'out', 'position', 'created', 'modified',
'hue', 'saturation', 'lightness', 'volume', 'videoRatio') 'hue', 'saturation', 'lightness', 'volume', 'videoRatio')
def json(self, keys=None): def json(self, keys=None, qs=None):
j = {} j = {}
for key in self.clip_keys: for key in self.clip_keys:
j[key] = getattr(self, { j[key] = getattr(self, {
@ -60,8 +60,11 @@ class MetaClip:
del j[key] del j[key]
#needed here to make item find with clips work #needed here to make item find with clips work
if 'annotations' in keys: if 'annotations' in keys:
annotations = self.annotations.filter(layer__in=self.layers)
if qs:
annotations = annotations.filter(qs)
j['annotations'] = [a.json(keys=['value', 'id', 'layer']) j['annotations'] = [a.json(keys=['value', 'id', 'layer'])
for a in self.annotations.filter(layer__in=self.layers)] for a in annotations]
for key in keys: for key in keys:
if key not in self.clip_keys and key not in j: if key not in self.clip_keys and key not in j:
value = self.item.get(key) value = self.item.get(key)

View file

@ -23,6 +23,7 @@ def parse_query(data, user):
if key in data: if key in data:
query[key] = data[key] query[key] = data[key]
query['qs'] = models.Clip.objects.find(query, user) query['qs'] = models.Clip.objects.find(query, user)
query['filter'] = models.Clip.objects.filter_annotations(query, user)
if 'itemsQuery' in data and data['itemsQuery'].get('conditions'): if 'itemsQuery' in data and data['itemsQuery'].get('conditions'):
item_query = Item.objects.find({'query': data['itemsQuery']}, user) item_query = Item.objects.find({'query': data['itemsQuery']}, user)
query['qs'] = query['qs'].filter(item__in=item_query) query['qs'] = query['qs'].filter(item__in=item_query)
@ -94,11 +95,12 @@ def findClips(request):
response['data']['items'] = [add(p) for p in qs] response['data']['items'] = [add(p) for p in qs]
keys = data['keys'] keys = data['keys']
def add_annotations(key, qs, add_layer=False): def add_annotations(key, qs, add_layer=False):
values = ['public_id', 'value', 'clip__public_id'] values = ['public_id', 'value', 'clip__public_id']
if add_layer: if add_layer:
values.append('layer') values.append('layer')
if query['filter']:
qs = qs.filter(query['filter'])
for a in qs.values(*values): for a in qs.values(*values):
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']:

View file

@ -92,6 +92,8 @@ def parse_query(data, user):
if 'clips' in data: if 'clips' in data:
query['clip_qs'] = Clip.objects.find({'query': data['clips']['query']}, query['clip_qs'] = Clip.objects.find({'query': data['clips']['query']},
user).order_by('start') user).order_by('start')
query['clip_filter'] = Clip.objects.find({'query': data['clips']['query']},
user)
query['clip_items'] = data['clips'].get('items', 5) query['clip_items'] = data['clips'].get('items', 5)
query['clip_keys'] = data['clips'].get('keys') query['clip_keys'] = data['clips'].get('keys')
if not query['clip_keys']: if not query['clip_keys']:
@ -231,7 +233,7 @@ Positions
i += step i += step
else: else:
clips = qs clips = qs
return [c.json(query['clip_keys']) for c in clips] return [c.json(query['clip_keys'], query['clip_filter']) for c in clips]
def only_p_sums(m): def only_p_sums(m):
r = {} r = {}

View file

@ -4,7 +4,10 @@
pandora.ui.clipsView = function(videoRatio) { pandora.ui.clipsView = function(videoRatio) {
var that = Ox.SplitPanel({ var textKey = Ox.getObjectById(pandora.site.layers, 'subtitles')
? 'subtitles'
: 'annotations',
that = Ox.SplitPanel({
elements: [ elements: [
{ {
element: Ox.Bar({size: 24}) element: Ox.Bar({size: 24})
@ -26,7 +29,7 @@ pandora.ui.clipsView = function(videoRatio) {
.bindEvent({ .bindEvent({
submit: function(data) { submit: function(data) {
pandora.UI.set('itemFind', data.value ? { pandora.UI.set('itemFind', data.value ? {
conditions: [{key: 'subtitles', value: data.value, operator: '='}], conditions: [{key: textKey, value: data.value, operator: '='}],
operator: '&' operator: '&'
} : pandora.site.user.ui.itemFind); } : pandora.site.user.ui.itemFind);
// since this is the only way itemFind can change, // since this is the only way itemFind can change,