From d5e75fc880d28e41a74d72b66eef00376c4e5fef Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Mon, 6 Oct 2014 10:31:30 +0200 Subject: [PATCH] move to new POST json + form api --- oxdata/api/views.py | 24 +++++++++++++++--------- oxdata/lookup/views.py | 3 +-- oxdata/movie/views.py | 14 +++++--------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/oxdata/api/views.py b/oxdata/api/views.py index 0ce7bdf..16deb7b 100644 --- a/oxdata/api/views.py +++ b/oxdata/api/views.py @@ -22,7 +22,9 @@ def api(request): 'text': 'use POST'}}) response['Access-Control-Allow-Origin'] = '*' return response - if not 'action' in request.POST: + if request.META['REQUEST_METHOD'] != "POST" or ( + not 'action' in request.POST and request.META.get('CONTENT_TYPE') != 'application/json' + ): methods = actions.keys() api = [] for f in sorted(methods): @@ -31,24 +33,28 @@ def api(request): context = RequestContext(request, {'api': api, 'sitename': settings.SITENAME}) return render_to_response('api.html', context) - function = request.POST['action'] - #FIXME: possible to do this in f - #data = json.loads(request.POST['data']) + if request.META.get('CONTENT_TYPE') == 'application/json': + r = json.loads(request.body) + action = r['action'] + data = r.get('data', {}) + else: + action = request.POST['action'] + data = json.loads(request.POST.get('data', '{}')) - f = actions.get(function) + f = actions.get(action) if f: - response = f(request) + response = f(request, data) else: response = render_to_json_response(json_response(status=400, - text='Unknown function %s' % function)) + text='Unknown action %s' % action)) response['Access-Control-Allow-Origin'] = '*' return response -def init(request): +def init(request, data): return render_to_json_response(json_response()) actions.register(init) -def error(request): +def error(request, data): ''' this action is used to test api error codes, it should return a 503 error ''' diff --git a/oxdata/lookup/views.py b/oxdata/lookup/views.py index 55798a7..6b3b23e 100644 --- a/oxdata/lookup/views.py +++ b/oxdata/lookup/views.py @@ -62,10 +62,9 @@ def ids(request): json = movie.json(prefix) return render_to_json_response(json) -def get(request): +def get(request, data): prefix = request.build_absolute_uri('/') response = json_response() - data = json.loads(request.POST['data']) movie_id = None movieId = None if 'id' in data: diff --git a/oxdata/movie/views.py b/oxdata/movie/views.py index a461783..b0740d9 100644 --- a/oxdata/movie/views.py +++ b/oxdata/movie/views.py @@ -6,13 +6,11 @@ import re from ox.django.shortcuts import render_to_json_response, json_response import ox.web.imdb -from ox.utils import json from api.actions import actions import models -def getId(request): - data = json.loads(request.POST['data']) +def getId(request, data): response = json_response() movie = models.find(data) if movie: @@ -22,16 +20,14 @@ def getId(request): return render_to_json_response(response) actions.register(getId) -def getIds(request): - data = json.loads(request.POST['data']) +def getIds(request, data): response = json_response() response['data']['items'] = models.Match.find(data) return render_to_json_response(response) actions.register(getIds) -def getData(request): +def getData(request, data): response = json_response() - data = json.loads(request.POST['data']) id = data['id'] if len(id) == 7: i, created = models.Imdb.objects.get_or_create(imdb=id) @@ -45,8 +41,8 @@ def getData(request): return render_to_json_response(response) actions.register(getData) -def parsePath(request): - path = json.loads(request.POST['data'])['path'] +def parsePath(request, data): + path = data['path'] response = json_response(ox.parse_movie_path(path)) return render_to_json_response(response) actions.register(parsePath)