2011-10-02 18:16:28 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
from __future__ import division
|
|
|
|
|
2011-10-30 11:55:46 +00:00
|
|
|
from django.conf import settings
|
2011-10-02 18:16:28 +00:00
|
|
|
from ox.utils import json
|
|
|
|
from ox.django.shortcuts import render_to_json_response, json_response
|
|
|
|
|
2012-01-03 20:18:47 +00:00
|
|
|
from ox.django.api import actions
|
2011-10-30 11:55:46 +00:00
|
|
|
|
|
|
|
from annotation.models import Annotation
|
2011-10-02 18:16:28 +00:00
|
|
|
from item.models import Item
|
|
|
|
from item import utils
|
|
|
|
|
|
|
|
import models
|
|
|
|
|
|
|
|
|
|
|
|
def parse_query(data, user):
|
|
|
|
query = {}
|
|
|
|
query['range'] = [0, 100]
|
|
|
|
query['sort'] = [{'key':'in', 'operator':'+'}]
|
|
|
|
for key in ('keys', 'group', 'range', 'sort', 'query'):
|
|
|
|
if key in data:
|
|
|
|
query[key] = data[key]
|
|
|
|
query['qs'] = models.Clip.objects.find(query, user)
|
2012-01-05 12:31:56 +00:00
|
|
|
query['filter'] = models.Clip.objects.filter_annotations(query, user)
|
2011-10-19 16:20:12 +00:00
|
|
|
if 'itemsQuery' in data and data['itemsQuery'].get('conditions'):
|
|
|
|
item_query = Item.objects.find({'query': data['itemsQuery']}, user)
|
2011-10-02 18:16:28 +00:00
|
|
|
query['qs'] = query['qs'].filter(item__in=item_query)
|
|
|
|
return query
|
|
|
|
|
|
|
|
def order_query(qs, sort):
|
|
|
|
order_by = []
|
2012-03-01 14:53:29 +00:00
|
|
|
sort += [
|
|
|
|
{'key': 'position', 'operator': '+'},
|
|
|
|
{'key': 'text', 'operator': '-'}
|
|
|
|
]
|
2011-10-02 18:16:28 +00:00
|
|
|
for e in sort:
|
|
|
|
operator = e['operator']
|
|
|
|
if operator != '-':
|
|
|
|
operator = ''
|
2011-10-09 13:01:16 +00:00
|
|
|
clip_keys = ('public_id', 'start', 'end', 'hue', 'saturation', 'lightness', 'volume',
|
2012-01-20 17:53:21 +00:00
|
|
|
'duration', 'sortvalue', 'videoRatio',
|
2012-03-22 19:40:37 +00:00
|
|
|
'random__random')
|
2011-10-02 18:16:28 +00:00
|
|
|
key = {
|
2011-10-09 12:46:35 +00:00
|
|
|
'id': 'public_id',
|
2011-10-02 18:16:28 +00:00
|
|
|
'in': 'start',
|
|
|
|
'out': 'end',
|
2011-10-09 12:02:16 +00:00
|
|
|
'position': 'start',
|
2012-01-20 17:53:21 +00:00
|
|
|
'text': 'sortvalue',
|
2011-10-29 10:01:41 +00:00
|
|
|
'videoRatio': 'aspect_ratio',
|
2012-03-22 19:40:37 +00:00
|
|
|
'random': 'random__random',
|
2011-10-02 18:16:28 +00:00
|
|
|
}.get(e['key'], e['key'])
|
|
|
|
if key.startswith('clip:'):
|
|
|
|
key = e['key'][len('clip:'):]
|
|
|
|
key = {
|
2012-01-20 17:53:21 +00:00
|
|
|
'text': 'sortvalue',
|
2011-10-02 18:16:28 +00:00
|
|
|
'position': 'start',
|
|
|
|
}.get(key, key)
|
2011-10-09 12:02:16 +00:00
|
|
|
elif key not in clip_keys:
|
2011-10-02 18:16:28 +00:00
|
|
|
#key mgith need to be changed, see order_sort in item/views.py
|
2012-03-22 21:33:42 +00:00
|
|
|
key = "sort__%s" % key
|
2012-06-18 14:36:04 +00:00
|
|
|
if key == 'public_id':
|
|
|
|
order_by.append('%s%s' % (operator, 'sort__itemId'))
|
|
|
|
order_by.append('%s%s' % (operator, 'start'))
|
|
|
|
order_by.append('end')
|
|
|
|
else:
|
|
|
|
order = '%s%s' % (operator, key)
|
|
|
|
order_by.append(order)
|
2011-10-02 18:16:28 +00:00
|
|
|
if order_by:
|
|
|
|
qs = qs.order_by(*order_by, nulls_last=True)
|
|
|
|
return qs
|
|
|
|
|
|
|
|
def findClips(request):
|
|
|
|
'''
|
|
|
|
param data {
|
2012-06-26 11:59:27 +00:00
|
|
|
query: {
|
|
|
|
conditions: [],
|
|
|
|
operator: '&'
|
|
|
|
},
|
|
|
|
itemsQuery: {
|
|
|
|
conditions: [],
|
|
|
|
operator: '&'
|
|
|
|
},
|
|
|
|
keys: [],
|
|
|
|
position: int,
|
|
|
|
positions: [],
|
|
|
|
range: [in, out],
|
|
|
|
sort: []
|
2011-10-02 18:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
'status': {'code': int, 'text': string}
|
|
|
|
'data': {
|
|
|
|
items = [{..}, {...}, ...]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
response = json_response()
|
|
|
|
|
|
|
|
query = parse_query(data, request.user)
|
2011-10-29 10:39:33 +00:00
|
|
|
qs = query['qs']
|
2011-10-02 18:16:28 +00:00
|
|
|
if 'keys' in data:
|
2011-10-29 10:39:33 +00:00
|
|
|
qs = order_query(qs, query['sort'])
|
2011-10-13 22:59:57 +00:00
|
|
|
qs = qs[query['range'][0]:query['range'][1]]
|
2011-12-29 17:04:53 +00:00
|
|
|
|
2011-11-02 11:57:10 +00:00
|
|
|
ids = []
|
2012-01-20 17:53:21 +00:00
|
|
|
keys = filter(lambda k: k not in settings.CONFIG['clipLayers'] + ['annotations'],
|
|
|
|
data['keys'])
|
2011-12-29 17:04:53 +00:00
|
|
|
if filter(lambda k: k not in models.Clip.clip_keys, keys):
|
2012-03-22 21:33:42 +00:00
|
|
|
qs = qs.select_related('sort')
|
2011-12-29 17:04:53 +00:00
|
|
|
|
2011-11-02 11:57:10 +00:00
|
|
|
def add(p):
|
|
|
|
ids.append(p.id)
|
|
|
|
return p.json(keys=keys)
|
2011-10-29 11:04:25 +00:00
|
|
|
|
2012-03-07 10:36:32 +00:00
|
|
|
response['data']['items'] = [add(p) for p in qs]
|
2011-10-29 11:04:25 +00:00
|
|
|
keys = data['keys']
|
2012-03-07 10:36:32 +00:00
|
|
|
|
2012-06-18 14:36:04 +00:00
|
|
|
def clip_public_id(c):
|
|
|
|
return u'%s/%0.03f-%0.03f' % (c['public_id'].split('/')[0], c['clip__start'], c['clip__end'])
|
|
|
|
|
2011-12-30 08:00:38 +00:00
|
|
|
def add_annotations(key, qs, add_layer=False):
|
2012-06-18 14:36:04 +00:00
|
|
|
values = ['public_id', 'value', 'clip__start', 'clip__end']
|
2011-12-30 08:00:38 +00:00
|
|
|
if add_layer:
|
|
|
|
values.append('layer')
|
2012-01-05 12:31:56 +00:00
|
|
|
if query['filter']:
|
|
|
|
qs = qs.filter(query['filter'])
|
2011-12-30 08:00:38 +00:00
|
|
|
for a in qs.values(*values):
|
2012-06-18 14:36:04 +00:00
|
|
|
public_id = clip_public_id(a)
|
2011-10-29 11:04:25 +00:00
|
|
|
for i in response['data']['items']:
|
2012-06-18 14:36:04 +00:00
|
|
|
if i['id'] == public_id:
|
2011-12-30 08:00:38 +00:00
|
|
|
if not key in i:
|
|
|
|
i[key] = []
|
|
|
|
l = {
|
2011-10-29 11:04:25 +00:00
|
|
|
'id': a['public_id'],
|
|
|
|
'value': a['value'],
|
2011-12-30 08:00:38 +00:00
|
|
|
}
|
|
|
|
if add_layer:
|
|
|
|
l['layer'] = a['layer']
|
|
|
|
i[key].append(l)
|
2011-10-30 11:55:46 +00:00
|
|
|
if response['data']['items']:
|
|
|
|
if 'annotations' in keys:
|
2012-03-07 10:36:32 +00:00
|
|
|
aqs = Annotation.objects.filter(layer__in=settings.CONFIG['clipLayers'],
|
|
|
|
clip__in=ids)
|
|
|
|
add_annotations('annotations',aqs , True)
|
2012-01-20 17:53:21 +00:00
|
|
|
for layer in filter(lambda l: l in keys, settings.CONFIG['clipLayers']):
|
2012-03-07 10:36:32 +00:00
|
|
|
aqs = Annotation.objects.filter(layer=layer, clip__in=ids)
|
|
|
|
add_annotations(layer, aqs)
|
2011-10-02 18:16:28 +00:00
|
|
|
elif 'position' in query:
|
2011-10-29 10:39:33 +00:00
|
|
|
qs = order_query(qs, query['sort'])
|
2012-06-18 14:36:04 +00:00
|
|
|
ids = [u'%s/%0.03f-%0.03f' % (c['item__itemId'], c['start'], c['end'])
|
|
|
|
for c in qs.values('item__itemId', 'start', 'end')]
|
2011-10-02 18:16:28 +00:00
|
|
|
data['conditions'] = data['conditions'] + {
|
|
|
|
'value': data['position'],
|
|
|
|
'key': query['sort'][0]['key'],
|
|
|
|
'operator': '^'
|
|
|
|
}
|
|
|
|
query = parse_query(data, request.user)
|
|
|
|
qs = order_query(query['qs'], query['sort'])
|
|
|
|
if qs.count() > 0:
|
|
|
|
response['data']['position'] = utils.get_positions(ids, [qs[0].itemId])[0]
|
|
|
|
elif 'positions' in data:
|
2011-10-29 10:39:33 +00:00
|
|
|
qs = order_query(qs, query['sort'])
|
2012-06-18 14:36:04 +00:00
|
|
|
ids = [u'%s/%0.03f-%0.03f' % (c['item__itemId'], c['start'], c['end'])
|
|
|
|
for c in qs.values('item__itemId', 'start', 'end')]
|
2011-10-02 18:16:28 +00:00
|
|
|
response['data']['positions'] = utils.get_positions(ids, data['positions'])
|
|
|
|
else:
|
|
|
|
response['data']['items'] = qs.count()
|
|
|
|
return render_to_json_response(response)
|
|
|
|
actions.register(findClips)
|