some annoation api

This commit is contained in:
j 2010-12-28 19:34:28 +05:30
parent 6f10b1a78a
commit da8aa69bf1
2 changed files with 102 additions and 24 deletions

View file

@ -23,6 +23,8 @@ from ox.utils import json
from ox import stripTags
from ox.normalize import canonicalTitle, canonicalName
import utils
class Layer(models.Model):
class Meta:
@ -70,7 +72,7 @@ class Annotation(models.Model):
start = models.FloatField(default=-1)
stop = models.FloatField(default=-1)
type = models.CharField(blank=True, max_length=255)
layer = models.ForeignKey(Layer)
value = models.TextField()
def editable(self, user):
@ -81,6 +83,23 @@ class Annotation(models.Model):
return True
return False
def html(self):
if self.layer.type == 'string':
return utils.html_parser(self.value)
else:
return self.value
def json(self):
return {
'id': self.id,
'user': self.user.username,
'start': self.start,
'stop': self.start,
'value': self.value,
'value_html': self.html(),
'layer': self.layer.name
}
def __unicode__(self):
return "%s/%s-%s" %(self.item, self.start, self.stop)

View file

@ -26,37 +26,96 @@ import ox
import models
from api.actions import actions
@login_required_json
def addLayer(request):
def findAnnotation(request):
'''
param data
{key: value}
param data {
fixme
}
return {
'status': {'code': int, 'text': string}
'data': {
annotations = [{..}, {...}, ...]
}
}
'''
#FIXME: implement findItem like queries
data = json.loads(request.POST['data'])
response = json_response(status=200, text='ok')
qs = models.Annotations.objects.filter(item__itemId=data['item'])
response['data']['annotations'] = [a.json() for a in qs]
return render_to_json_response(response)
actions.register(findAnnotation)
@login_required_json
def addAnnotation(request):
'''
param data {
item: itemId,
layer: layerId,
start: float,
end: float,
value: string
}
return {'status': {'code': int, 'text': string},
'data': {}}
'data': {
'annotation': {}s
}
}
'''
data = json.loads(request.POST['data'])
for key in ('item', 'layer', 'start', 'end', 'value'):
if key not in data:
return render_to_json_response(json_response(status=400, text='invalid data'))
item = get_object_or_404_json(models.Item, itemId=data['item'])
layer = get_object_or_404_json(models.Layer, layerId=data['layer'])
annotation = models.Annotation(
item=item,
layer=layer,
user=request.user,
start=float(data['start']), start=float(data['end']),
value=data['value']
)
annotation.save()
response = json_response()
response['data']['annotation'] = annotation.json()
return render_to_json_response(response)
response = {'status': {'code': 501, 'text': 'not implemented'}}
return render_to_json_response(response)
actions.register(addAnnotation)
@login_required_json
def removeAnnotation(request):
'''
param data {
id:
}
return {'status': {'code': int, 'text': string},
'data': {
}
}
'''
response = {'status': {'code': 501, 'text': 'not implemented'}}
return render_to_json_response(response)
actions.register(addLayer)
actions.register(removeAnnotation)
@login_required_json
def removeLayer(request):
def editAnnotation(request):
'''
param data
{key: value}
param data {
id:,
start: float,
end: float,
value: string,
}
return {'status': {'code': int, 'text': string},
'data': {}}
'''
response = {'status': {'code': 501, 'text': 'not implemented'}}
return render_to_json_response(response)
actions.register(removeLayer)
@login_required_json
def editLayer(request):
'''
param data
{key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
'data': {
}
}
'''
response = json_response({})
data = json.loads(request.POST['data'])
@ -69,5 +128,5 @@ def editLayer(request):
response = json_response(status=501, text='not implemented')
return render_to_json_response(response)
actions.register(editLayer)
actions.register(editAnnotation)