move to new POST json + form api

This commit is contained in:
j 2014-10-06 10:31:30 +02:00
parent 233c9a56a5
commit d5e75fc880
3 changed files with 21 additions and 20 deletions

View file

@ -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
'''

View file

@ -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:

View file

@ -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)