2010-11-26 15:07:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
from __future__ import division
|
|
|
|
|
2011-09-06 12:06:59 +00:00
|
|
|
from django.conf import settings
|
|
|
|
|
2010-11-26 15:07:24 +00:00
|
|
|
from ox.utils import json
|
|
|
|
from ox.django.decorators import login_required_json
|
2014-11-17 19:56:27 +00:00
|
|
|
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response, HttpErrorJson
|
2010-11-26 15:07:24 +00:00
|
|
|
|
2011-02-09 14:28:52 +00:00
|
|
|
|
2012-01-03 20:18:47 +00:00
|
|
|
from ox.django.api import actions
|
2010-11-26 15:07:24 +00:00
|
|
|
|
2011-06-16 20:00:10 +00:00
|
|
|
from item import utils
|
|
|
|
from item.models import Item
|
2014-12-16 16:39:45 +00:00
|
|
|
from item.utils import get_by_id
|
|
|
|
from entity.models import Entity
|
2014-12-17 13:45:46 +00:00
|
|
|
from changelog.models import add_changelog
|
2011-06-16 20:00:10 +00:00
|
|
|
|
2011-02-11 10:21:25 +00:00
|
|
|
import models
|
2013-03-04 15:41:25 +00:00
|
|
|
from tasks import update_item, add_annotations
|
2010-12-28 14:04:28 +00:00
|
|
|
|
2014-11-17 19:56:27 +00:00
|
|
|
def get_annotation_or_404_json(id):
|
|
|
|
try:
|
|
|
|
return models.Annotation.get(id)
|
|
|
|
except models.Annotation.DoesNotExist:
|
|
|
|
response = {'status': {'code': 404,
|
|
|
|
'text': 'Annotation not found'}}
|
|
|
|
raise HttpErrorJson(response)
|
|
|
|
|
2011-06-16 20:00:10 +00:00
|
|
|
def parse_query(data, user):
|
|
|
|
query = {}
|
|
|
|
query['range'] = [0, 100]
|
|
|
|
query['sort'] = [{'key':'in', 'operator':'+'}]
|
2011-06-17 07:44:45 +00:00
|
|
|
for key in ('keys', 'group', 'range', 'sort', 'query'):
|
2011-06-16 20:00:10 +00:00
|
|
|
if key in data:
|
|
|
|
query[key] = data[key]
|
2011-06-17 07:44:45 +00:00
|
|
|
query['qs'] = models.Annotation.objects.find(query, user)
|
2011-10-19 16:20:12 +00:00
|
|
|
if 'itemsQuery' in data:
|
|
|
|
item_query = Item.objects.find({'query': data['itemsQuery']}, user)
|
2011-06-16 20:00:10 +00:00
|
|
|
query['qs'] = query['qs'].filter(item__in=item_query)
|
|
|
|
return query
|
|
|
|
|
2011-09-28 00:29:40 +00:00
|
|
|
def annotation_sort_key(key):
|
|
|
|
return {
|
|
|
|
'text': 'value',
|
|
|
|
'position': 'start',
|
|
|
|
}.get(key, key)
|
|
|
|
|
2011-06-16 20:00:10 +00:00
|
|
|
def order_query(qs, sort):
|
2011-09-28 00:29:40 +00:00
|
|
|
order_by = []
|
|
|
|
for e in sort:
|
|
|
|
operator = e['operator']
|
|
|
|
if operator != '-':
|
|
|
|
operator = ''
|
2011-09-30 22:28:35 +00:00
|
|
|
key = {
|
2011-10-04 09:39:00 +00:00
|
|
|
'duration': 'clip__duration',
|
2011-09-30 22:28:35 +00:00
|
|
|
'in': 'start',
|
2011-10-04 09:39:00 +00:00
|
|
|
'lightness': 'clip__lightness',
|
2011-09-30 22:28:35 +00:00
|
|
|
'out': 'end',
|
2011-10-04 09:39:00 +00:00
|
|
|
'saturation': 'clip__saturation',
|
|
|
|
'volume': 'clip__volume',
|
2011-09-30 22:28:35 +00:00
|
|
|
}.get(e['key'], e['key'])
|
|
|
|
if key.startswith('clip:'):
|
2011-09-28 00:29:40 +00:00
|
|
|
key = annotation_sort_key(e['key'][len('clip:'):])
|
2011-10-04 09:39:00 +00:00
|
|
|
elif key not in ('start', 'end', 'value') and not key.startswith('clip__'):
|
2011-09-28 00:29:40 +00:00
|
|
|
#key mgith need to be changed, see order_sort in item/views.py
|
2011-09-30 22:28:35 +00:00
|
|
|
key = "item__sort__%s" % key
|
2011-09-28 00:29:40 +00:00
|
|
|
order = '%s%s' % (operator, key)
|
|
|
|
order_by.append(order)
|
|
|
|
if order_by:
|
2014-10-26 17:44:36 +00:00
|
|
|
qs = qs.order_by(*order_by, nulls_last=True).exclude(value='')
|
2011-06-16 20:00:10 +00:00
|
|
|
return qs
|
|
|
|
|
2014-10-06 08:26:43 +00:00
|
|
|
def findAnnotations(request, data):
|
2010-12-28 14:04:28 +00:00
|
|
|
'''
|
2014-12-18 16:39:47 +00:00
|
|
|
Finds annotations for a given query
|
2014-11-18 15:57:16 +00:00
|
|
|
takes {
|
2014-12-18 16:39:47 +00:00
|
|
|
query: object, // annotation query object, see `find`
|
|
|
|
itemsQuery: object, // item query object, see `find`
|
|
|
|
keys: [string, string, ...], // list of keys to return, see `find`
|
2014-11-18 15:57:16 +00:00
|
|
|
position: int,
|
2014-12-18 16:39:47 +00:00
|
|
|
positions: [string, string], // list of item ids, see `find`
|
|
|
|
range: [int, int], // items to return, per current sort order, see `find`
|
|
|
|
sort: [] // list of sort object, see `find`
|
2014-11-18 15:57:16 +00:00
|
|
|
}
|
|
|
|
returns {
|
|
|
|
annotations: [{
|
2014-12-18 16:39:47 +00:00
|
|
|
id: string, // annotation id
|
|
|
|
... // more annotation properties
|
2014-11-18 15:57:16 +00:00
|
|
|
}]
|
|
|
|
}
|
2014-12-19 15:05:08 +00:00
|
|
|
see: addAnnotation, addAnnotations, editAnnotation, find, getAnnotation,
|
2014-12-18 16:39:47 +00:00
|
|
|
removeAnnotation
|
2010-12-28 14:04:28 +00:00
|
|
|
'''
|
2011-06-16 20:00:10 +00:00
|
|
|
response = json_response()
|
|
|
|
|
|
|
|
query = parse_query(data, request.user)
|
|
|
|
qs = order_query(query['qs'], query['sort'])
|
|
|
|
if 'keys' in data:
|
2011-10-09 13:39:45 +00:00
|
|
|
qs = qs.select_related()[query['range'][0]:query['range'][1]]
|
2011-06-16 20:00:10 +00:00
|
|
|
response['data']['items'] = [p.json(keys=data['keys']) for p in qs]
|
|
|
|
elif 'position' in query:
|
2011-08-23 10:47:59 +00:00
|
|
|
ids = [i.public_id for i in qs]
|
2011-06-16 20:00:10 +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:
|
2014-09-19 12:26:46 +00:00
|
|
|
response['data']['position'] = utils.get_positions(ids, [qs[0].public_id])[0]
|
2011-06-16 20:00:10 +00:00
|
|
|
elif 'positions' in data:
|
2011-09-28 00:29:40 +00:00
|
|
|
ids = [i.public_id for i in qs]
|
2011-06-16 20:00:10 +00:00
|
|
|
response['data']['positions'] = utils.get_positions(ids, data['positions'])
|
|
|
|
else:
|
|
|
|
response['data']['items'] = qs.count()
|
2010-12-28 14:04:28 +00:00
|
|
|
return render_to_json_response(response)
|
2011-01-18 09:54:14 +00:00
|
|
|
actions.register(findAnnotations)
|
2010-12-28 14:04:28 +00:00
|
|
|
|
2014-11-17 19:56:27 +00:00
|
|
|
def getAnnotation(request, data):
|
|
|
|
'''
|
2014-11-18 15:57:16 +00:00
|
|
|
Gets data for an annotation
|
|
|
|
takes {
|
|
|
|
id: string, // annotation id
|
|
|
|
keys: [string] // list of keys to return
|
|
|
|
}
|
|
|
|
returns {
|
2014-12-18 16:39:47 +00:00
|
|
|
key: value, // property id and value
|
|
|
|
... // more key/value pairs
|
2014-11-18 15:57:16 +00:00
|
|
|
}
|
2014-11-17 19:56:27 +00:00
|
|
|
'''
|
|
|
|
response = json_response({})
|
|
|
|
data['keys'] = data.get('keys', [])
|
|
|
|
annotation = get_annotation_or_404_json(data['id'])
|
|
|
|
response['data'] = annotation.json(keys=data['keys'], user=request.user)
|
|
|
|
return render_to_json_response(response)
|
|
|
|
actions.register(getAnnotation)
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-11-26 15:07:24 +00:00
|
|
|
@login_required_json
|
2014-10-06 08:26:43 +00:00
|
|
|
def addAnnotation(request, data):
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
2014-11-15 18:39:16 +00:00
|
|
|
Adds a single annotation
|
|
|
|
takes {
|
2014-11-18 15:45:41 +00:00
|
|
|
item: string, // item id
|
|
|
|
layer: string, // annotation layer id
|
|
|
|
in: float, // in point in seconds
|
|
|
|
out: float, // out point in seconds
|
|
|
|
value: string // annotation value
|
2014-11-15 18:39:16 +00:00
|
|
|
}
|
|
|
|
returns {
|
2014-11-18 15:45:41 +00:00
|
|
|
id: string, // annotation id
|
2014-12-18 13:38:20 +00:00
|
|
|
... // more annotation properties
|
2014-11-15 18:39:16 +00:00
|
|
|
}
|
2014-12-18 16:39:47 +00:00
|
|
|
see: addAnnotations, editAnnotation, findAnnotations, getAnnotation,
|
2014-12-18 13:38:20 +00:00
|
|
|
getTaskStatus, removeAnnotation
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
2011-02-09 14:28:52 +00:00
|
|
|
for key in ('item', 'layer', 'in', 'out', 'value'):
|
2010-12-28 14:04:28 +00:00
|
|
|
if key not in data:
|
2011-01-01 11:44:42 +00:00
|
|
|
return render_to_json_response(json_response(status=400,
|
|
|
|
text='invalid data'))
|
2010-12-28 14:04:28 +00:00
|
|
|
|
2014-09-19 12:26:46 +00:00
|
|
|
item = get_object_or_404_json(Item, public_id=data['item'])
|
2011-11-02 14:06:34 +00:00
|
|
|
|
2012-01-02 15:08:38 +00:00
|
|
|
layer_id = data['layer']
|
2014-12-16 16:39:45 +00:00
|
|
|
layer = get_by_id(settings.CONFIG['layers'], layer_id)
|
2012-01-02 15:08:38 +00:00
|
|
|
if layer['canAddAnnotations'].get(request.user.get_profile().get_level()):
|
2014-12-16 16:39:45 +00:00
|
|
|
if layer['type'] == 'entity':
|
2014-12-17 19:20:00 +00:00
|
|
|
try:
|
2015-02-06 07:12:19 +00:00
|
|
|
value = Entity.get_by_name(data['value'], layer['entity']).get_id()
|
2014-12-17 19:20:00 +00:00
|
|
|
except Entity.DoesNotExist:
|
|
|
|
response = json_response({})
|
|
|
|
response['status']['text'] = 'unkown entity'
|
|
|
|
return render_to_json_response(response)
|
2014-12-16 16:39:45 +00:00
|
|
|
else:
|
|
|
|
value = data['value']
|
2012-01-02 15:08:38 +00:00
|
|
|
annotation = models.Annotation(
|
|
|
|
item=item,
|
|
|
|
layer=layer_id,
|
|
|
|
user=request.user,
|
|
|
|
start=float(data['in']), end=float(data['out']),
|
2014-12-16 16:39:45 +00:00
|
|
|
value=value)
|
2012-01-02 15:08:38 +00:00
|
|
|
annotation.save()
|
2012-09-10 08:23:09 +00:00
|
|
|
update_item.delay(annotation.id)
|
2014-12-17 13:45:46 +00:00
|
|
|
add_changelog(request, data, annotation.public_id)
|
2012-01-02 15:08:38 +00:00
|
|
|
response = json_response(annotation.json())
|
2012-01-15 15:05:37 +00:00
|
|
|
response['data']['editable'] = True
|
2012-01-02 15:08:38 +00:00
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='permission denied')
|
2010-12-28 14:04:28 +00:00
|
|
|
return render_to_json_response(response)
|
2011-01-13 08:33:14 +00:00
|
|
|
actions.register(addAnnotation, cache=False)
|
2010-11-26 15:07:24 +00:00
|
|
|
|
2013-03-04 15:41:25 +00:00
|
|
|
@login_required_json
|
2014-10-06 08:26:43 +00:00
|
|
|
def addAnnotations(request, data):
|
2013-03-04 15:41:25 +00:00
|
|
|
'''
|
2014-11-15 18:39:16 +00:00
|
|
|
Adds multiple annotations
|
|
|
|
takes {
|
2014-11-18 15:45:41 +00:00
|
|
|
item: string, // item id
|
|
|
|
layer: string, // annotation layer id
|
2014-11-15 18:39:16 +00:00
|
|
|
annotations: [
|
|
|
|
{
|
2014-11-18 15:45:41 +00:00
|
|
|
in: float, // in point in seconds
|
|
|
|
out: float, // out point in seconds
|
|
|
|
value: string // annotation value
|
2014-11-15 18:39:16 +00:00
|
|
|
},
|
2014-12-18 13:38:20 +00:00
|
|
|
... // more annotations
|
2014-11-15 18:39:16 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
returns {
|
2014-12-18 13:38:20 +00:00
|
|
|
taskId: string // task id, use `getTaskStatus` to poll
|
2014-11-15 18:39:16 +00:00
|
|
|
}
|
2014-12-18 13:38:20 +00:00
|
|
|
see: addAnnotation, editAnnotation, findAnnotations, getAnnotation,
|
|
|
|
getTaskStatus, removeAnnotation
|
2013-03-04 15:41:25 +00:00
|
|
|
'''
|
|
|
|
for key in ('item', 'layer', 'annotations'):
|
|
|
|
if key not in data:
|
|
|
|
return render_to_json_response(json_response(status=400,
|
|
|
|
text='invalid data'))
|
|
|
|
|
2014-09-19 12:26:46 +00:00
|
|
|
item = get_object_or_404_json(Item, public_id=data['item'])
|
2013-03-04 15:41:25 +00:00
|
|
|
|
|
|
|
layer_id = data['layer']
|
2014-12-16 16:39:45 +00:00
|
|
|
layer = get_by_id(settings.CONFIG['layers'], layer_id)
|
2013-03-04 15:41:25 +00:00
|
|
|
if item.editable(request.user) \
|
|
|
|
and layer['canAddAnnotations'].get(request.user.get_profile().get_level()):
|
|
|
|
response = json_response()
|
|
|
|
data['user'] = request.user.username
|
|
|
|
t = add_annotations.delay(data)
|
2014-12-17 13:45:46 +00:00
|
|
|
add_changelog(request, data, item.public_id)
|
2013-03-04 15:41:25 +00:00
|
|
|
response['data']['taskId'] = t.task_id
|
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='permission denied')
|
|
|
|
return render_to_json_response(response)
|
|
|
|
actions.register(addAnnotations, cache=False)
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-11-26 15:07:24 +00:00
|
|
|
@login_required_json
|
2014-10-06 08:26:43 +00:00
|
|
|
def removeAnnotation(request, data):
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
2014-11-18 15:57:16 +00:00
|
|
|
Removes an annotation
|
|
|
|
takes {
|
|
|
|
id: string // annotation id
|
|
|
|
}
|
2014-12-18 16:39:47 +00:00
|
|
|
returns {}
|
|
|
|
see: addAnnotation, addAnnotations, editAnnotation, findAnnotations,
|
|
|
|
getAnnotation
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
2011-02-11 14:46:21 +00:00
|
|
|
response = json_response({})
|
2012-01-03 10:26:50 +00:00
|
|
|
a = get_object_or_404_json(models.Annotation, public_id=data['id'])
|
|
|
|
if a.editable(request.user):
|
2014-12-17 13:45:46 +00:00
|
|
|
add_changelog(request, data, a.public_id)
|
2012-01-03 10:26:50 +00:00
|
|
|
a.delete()
|
|
|
|
else:
|
2011-02-11 14:46:21 +00:00
|
|
|
response = json_response(status=403, text='permission denied')
|
2010-11-26 15:07:24 +00:00
|
|
|
return render_to_json_response(response)
|
2012-01-03 10:26:50 +00:00
|
|
|
actions.register(removeAnnotation, cache=False)
|
2010-11-26 15:07:24 +00:00
|
|
|
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-11-26 15:07:24 +00:00
|
|
|
@login_required_json
|
2014-10-06 08:26:43 +00:00
|
|
|
def editAnnotation(request, data):
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
2014-11-18 15:57:16 +00:00
|
|
|
Edits an annotation
|
|
|
|
takes {
|
|
|
|
id: string, // annotation id
|
|
|
|
in: float, // in point in seconds, optional
|
|
|
|
out: float, // out point in seconds, optional
|
|
|
|
value: string // annotation value, optional
|
|
|
|
}
|
|
|
|
returns {
|
|
|
|
id: string, // annotation id
|
2014-12-18 16:39:47 +00:00
|
|
|
... // more annotation properties
|
2014-11-18 15:57:16 +00:00
|
|
|
}
|
2014-12-18 16:39:47 +00:00
|
|
|
see: addAnnotation, addAnnotations, findAnnotations, getAnnotation,
|
|
|
|
removeAnnotation
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
|
|
|
response = json_response({})
|
2011-09-30 22:28:35 +00:00
|
|
|
a = get_object_or_404_json(models.Annotation, public_id=data['id'])
|
2011-02-09 14:28:52 +00:00
|
|
|
if a.editable(request.user):
|
2014-12-16 16:39:45 +00:00
|
|
|
layer = get_by_id(settings.CONFIG['layers'], a.layer)
|
2012-01-09 20:26:16 +00:00
|
|
|
for key in ('value', 'in', 'out'):
|
|
|
|
if key in data:
|
2014-12-26 13:00:54 +00:00
|
|
|
if key == 'value' and layer['type'] == 'entity':
|
|
|
|
try:
|
2015-02-06 07:12:19 +00:00
|
|
|
value = Entity.get_by_name(data['value'], layer['entity']).get_id()
|
2014-12-26 13:00:54 +00:00
|
|
|
except Entity.DoesNotExist:
|
|
|
|
response['data'] = a.json()
|
|
|
|
response['data']['editable'] = True
|
|
|
|
response['status']['text'] = 'unkown entity'
|
|
|
|
return render_to_json_response(response)
|
|
|
|
else:
|
|
|
|
value = data[key]
|
2012-01-09 20:26:16 +00:00
|
|
|
setattr(a, {
|
|
|
|
'in': 'start',
|
|
|
|
'out': 'end'
|
2014-12-16 16:39:45 +00:00
|
|
|
}.get(key,key), value)
|
2014-12-17 19:20:00 +00:00
|
|
|
add_changelog(request, data)
|
2011-02-09 14:28:52 +00:00
|
|
|
a.save()
|
2012-03-09 18:27:28 +00:00
|
|
|
#update sort/find tables async
|
|
|
|
update_item.delay(a.id)
|
2011-02-25 15:03:41 +00:00
|
|
|
response['data'] = a.json()
|
2012-01-15 15:05:37 +00:00
|
|
|
response['data']['editable'] = True
|
2010-11-26 15:07:24 +00:00
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='permission denied')
|
|
|
|
return render_to_json_response(response)
|
2011-01-13 08:33:14 +00:00
|
|
|
actions.register(editAnnotation, cache=False)
|
2012-01-02 17:08:19 +00:00
|
|
|
|