From b2c7f3156d1c47bd936fc9292740cca415fbcf60 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sun, 16 Feb 2014 10:15:45 +0000 Subject: [PATCH] add getDocument api, fixes #2313 --- pandora/document/views.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/pandora/document/views.py b/pandora/document/views.py index d4435806..56cbda41 100644 --- a/pandora/document/views.py +++ b/pandora/document/views.py @@ -6,7 +6,7 @@ from ox.utils import json from ox.django.api import actions from ox.django.decorators import login_required_json from ox.django.http import HttpFileResponse -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 django import forms from django.db.models import Sum @@ -16,7 +16,12 @@ from itemlist.models import List import models def get_document_or_404_json(id): - return models.Document.get(id) + try: + return models.Document.get(id) + except models.Document.DoesNotExist: + response = {'status': {'code': 404, + 'text': 'Document not found'}} + raise HttpErrorJson(response) @login_required_json def addDocument(request): @@ -183,6 +188,24 @@ def findDocuments(request): return render_to_json_response(response) actions.register(findDocuments) +def getDocument(request): + ''' + takes { + id: string, + keys: [string] + } + returns { + key: value + } + ''' + response = json_response({}) + data = json.loads(request.POST['data']) + data['keys'] = data.get('keys', []) + document = get_document_or_404_json(data['id']) + response['data'] = document.json(keys=data['keys'], user=request.user) + return render_to_json_response(response) +actions.register(getDocument) + @login_required_json def removeDocument(request): '''