diff --git a/pandora/annotation/models.py b/pandora/annotation/models.py index f8e16fd7c..555008a77 100644 --- a/pandora/annotation/models.py +++ b/pandora/annotation/models.py @@ -110,6 +110,10 @@ class Annotation(models.Model): return True return False + @classmethod + def get(cls, id): + return cls.objects.get(public_id=id) + def set_public_id(self): if self.id: public_id = Annotation.objects.filter(item=self.item, id__lt=self.id).count() + 1 diff --git a/pandora/annotation/views.py b/pandora/annotation/views.py index a19499161..a9b20974d 100644 --- a/pandora/annotation/views.py +++ b/pandora/annotation/views.py @@ -6,7 +6,7 @@ from django.conf import settings 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 +from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response, HttpErrorJson from ox.django.api import actions @@ -17,6 +17,14 @@ from item.models import Item import models 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): query = {} query['range'] = [0, 100] @@ -108,6 +116,22 @@ def findAnnotations(request, data): return render_to_json_response(response) 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 def addAnnotation(request, data):