add getAnnotation, fixes #2572

This commit is contained in:
j 2014-11-17 19:56:27 +00:00
parent 1ac959623e
commit d6c10eb377
2 changed files with 29 additions and 1 deletions

View file

@ -110,6 +110,10 @@ class Annotation(models.Model):
return True return True
return False return False
@classmethod
def get(cls, id):
return cls.objects.get(public_id=id)
def set_public_id(self): def set_public_id(self):
if self.id: if self.id:
public_id = Annotation.objects.filter(item=self.item, id__lt=self.id).count() + 1 public_id = Annotation.objects.filter(item=self.item, id__lt=self.id).count() + 1

View file

@ -6,7 +6,7 @@ from django.conf import settings
from ox.utils import json from ox.utils import json
from ox.django.decorators import login_required_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 from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response, HttpErrorJson
from ox.django.api import actions from ox.django.api import actions
@ -17,6 +17,14 @@ from item.models import Item
import models import models
from tasks import update_item, add_annotations from tasks import update_item, add_annotations
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)
def parse_query(data, user): def parse_query(data, user):
query = {} query = {}
query['range'] = [0, 100] query['range'] = [0, 100]
@ -108,6 +116,22 @@ def findAnnotations(request, data):
return render_to_json_response(response) return render_to_json_response(response)
actions.register(findAnnotations) actions.register(findAnnotations)
def getAnnotation(request, data):
'''
takes {
id: string,
keys: [string]
}
returns {
key: value
}
'''
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)
@login_required_json @login_required_json
def addAnnotation(request, data): def addAnnotation(request, data):