From d6c10eb377a0fcd58bbb6b3bca9bab9cbaf80f9a Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Mon, 17 Nov 2014 19:56:27 +0000 Subject: [PATCH] add getAnnotation, fixes #2572 --- pandora/annotation/models.py | 4 ++++ pandora/annotation/views.py | 26 +++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/pandora/annotation/models.py b/pandora/annotation/models.py index f8e16fd7..555008a7 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 a1949916..a9b20974 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):