pandora/pandora/annotation/views.py

181 lines
5.3 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division
2011-02-11 14:46:21 +00:00
import ox
from ox.utils import json
from ox.django.decorators import login_required_json
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response
2011-02-09 14:28:52 +00:00
2011-02-11 10:21:25 +00:00
from app.models import site_config
from item.models import Item
from api.actions import actions
2011-06-16 20:00:10 +00:00
from item import utils
from item.models import Item
2011-02-11 10:21:25 +00:00
import models
2010-12-28 14:04:28 +00:00
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-06-16 20:00:10 +00:00
if 'itemQuery' in data:
item_query = Item.objects.find({'query': data['itemQuery']}, user)
query['qs'] = query['qs'].filter(item__in=item_query)
return query
def order_query(qs, sort):
qs = qs.order_by('start')
return qs
2011-01-18 09:54:14 +00:00
def findAnnotations(request):
2010-12-28 14:04:28 +00:00
'''
param data {
2011-06-16 20:00:10 +00:00
query: ...
itemQuery: ...
2010-12-28 14:04:28 +00:00
}
return {
'status': {'code': int, 'text': string}
'data': {
annotations = [{..}, {...}, ...]
}
}
'''
data = json.loads(request.POST['data'])
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:
qs = qs[query['range'][0]:query['range'][1]]
response['data']['items'] = [p.json(keys=data['keys']) for p in qs]
elif 'position' in query:
ids = [i.get_id() for i in qs]
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:
ids = [i.get_id() for i in qs]
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
2011-01-01 11:44:42 +00:00
@login_required_json
2010-12-28 14:04:28 +00:00
def addAnnotation(request):
'''
2010-12-28 14:04:28 +00:00
param data {
item: itemId,
layer: layerId,
2011-02-09 14:28:52 +00:00
in: float,
out: float,
2010-12-28 14:04:28 +00:00
value: string
}
return {'status': {'code': int, 'text': string},
2010-12-28 14:04:28 +00:00
'data': {
2011-02-25 15:03:41 +00:00
id: 123,
...
2010-12-28 14:04:28 +00:00
}
}
'''
2010-12-28 14:04:28 +00:00
data = json.loads(request.POST['data'])
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
2011-02-11 10:21:25 +00:00
#FIXME: this should be only called starting up server
models.load_layers(site_config()['layers'])
2011-02-09 14:28:52 +00:00
item = get_object_or_404_json(Item, itemId=data['item'])
layer = get_object_or_404_json(models.Layer, name=data['layer'])
2011-01-01 11:44:42 +00:00
2010-12-28 14:04:28 +00:00
annotation = models.Annotation(
item=item,
layer=layer,
user=request.user,
2011-02-09 14:28:52 +00:00
start=float(data['in']), end=float(data['out']),
2011-01-01 11:44:42 +00:00
value=data['value'])
2010-12-28 14:04:28 +00:00
annotation.save()
2011-02-25 15:03:41 +00:00
response = json_response(annotation.json())
2010-12-28 14:04:28 +00:00
return render_to_json_response(response)
response = {'status': {'code': 501, 'text': 'not implemented'}}
return render_to_json_response(response)
2011-01-13 08:33:14 +00:00
actions.register(addAnnotation, cache=False)
2011-01-01 11:44:42 +00:00
@login_required_json
2011-02-11 14:46:21 +00:00
def removeAnnotations(request):
'''
2010-12-28 14:04:28 +00:00
param data {
2011-02-11 14:46:21 +00:00
ids: []
2010-12-28 14:04:28 +00:00
}
return {'status': {'code': int, 'text': string},
2010-12-28 14:04:28 +00:00
'data': {
}
}
'''
2011-02-11 14:46:21 +00:00
response = json_response({})
data = json.loads(request.POST['data'])
ids = map(ox.from32, data['ids'])
failed = []
for a in models.Annotation.objects.filter(id__in=ids):
if a.editable(request.user):
a.delete()
else:
failed.append(a.get_id)
if failed:
response = json_response(status=403, text='permission denied')
response['data']['ids'] = failed
return render_to_json_response(response)
2011-02-11 14:46:21 +00:00
actions.register(removeAnnotations, cache=False)
2011-01-01 11:44:42 +00:00
@login_required_json
2010-12-28 14:04:28 +00:00
def editAnnotation(request):
'''
2010-12-28 14:04:28 +00:00
param data {
id:,
2011-02-09 14:28:52 +00:00
in: float,
out: float,
2010-12-28 14:04:28 +00:00
value: string,
}
return {'status': {'code': int, 'text': string},
2010-12-28 14:04:28 +00:00
'data': {
2011-02-25 15:03:41 +00:00
id:
...
2010-12-28 14:04:28 +00:00
}
}
'''
response = json_response({})
data = json.loads(request.POST['data'])
2011-08-18 11:53:38 +00:00
itemId, annotationId = data['id'].split('/')
a = get_object_or_404_json(models.Annotation, pk=ox.from32(annotationId))
2011-02-09 14:28:52 +00:00
if a.editable(request.user):
a.value = data['value']
a.start = data['in']
a.end = data['out']
a.save()
2011-02-25 15:03:41 +00:00
response['data'] = a.json()
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)