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'}}) 'text': 'use POST'}})
response['Access-Control-Allow-Origin'] = '*' response['Access-Control-Allow-Origin'] = '*'
return response 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() methods = actions.keys()
api = [] api = []
for f in sorted(methods): for f in sorted(methods):
@ -31,24 +33,28 @@ def api(request):
context = RequestContext(request, {'api': api, context = RequestContext(request, {'api': api,
'sitename': settings.SITENAME}) 'sitename': settings.SITENAME})
return render_to_response('api.html', context) return render_to_response('api.html', context)
function = request.POST['action'] if request.META.get('CONTENT_TYPE') == 'application/json':
#FIXME: possible to do this in f r = json.loads(request.body)
#data = json.loads(request.POST['data']) 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: if f:
response = f(request) response = f(request, data)
else: else:
response = render_to_json_response(json_response(status=400, response = render_to_json_response(json_response(status=400,
text='Unknown function %s' % function)) text='Unknown action %s' % action))
response['Access-Control-Allow-Origin'] = '*' response['Access-Control-Allow-Origin'] = '*'
return response return response
def init(request): def init(request, data):
return render_to_json_response(json_response()) return render_to_json_response(json_response())
actions.register(init) 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 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) json = movie.json(prefix)
return render_to_json_response(json) return render_to_json_response(json)
def get(request): def get(request, data):
prefix = request.build_absolute_uri('/') prefix = request.build_absolute_uri('/')
response = json_response() response = json_response()
data = json.loads(request.POST['data'])
movie_id = None movie_id = None
movieId = None movieId = None
if 'id' in data: if 'id' in data:

View file

@ -6,13 +6,11 @@ import re
from ox.django.shortcuts import render_to_json_response, json_response from ox.django.shortcuts import render_to_json_response, json_response
import ox.web.imdb import ox.web.imdb
from ox.utils import json
from api.actions import actions from api.actions import actions
import models import models
def getId(request): def getId(request, data):
data = json.loads(request.POST['data'])
response = json_response() response = json_response()
movie = models.find(data) movie = models.find(data)
if movie: if movie:
@ -22,16 +20,14 @@ def getId(request):
return render_to_json_response(response) return render_to_json_response(response)
actions.register(getId) actions.register(getId)
def getIds(request): def getIds(request, data):
data = json.loads(request.POST['data'])
response = json_response() response = json_response()
response['data']['items'] = models.Match.find(data) response['data']['items'] = models.Match.find(data)
return render_to_json_response(response) return render_to_json_response(response)
actions.register(getIds) actions.register(getIds)
def getData(request): def getData(request, data):
response = json_response() response = json_response()
data = json.loads(request.POST['data'])
id = data['id'] id = data['id']
if len(id) == 7: if len(id) == 7:
i, created = models.Imdb.objects.get_or_create(imdb=id) i, created = models.Imdb.objects.get_or_create(imdb=id)
@ -45,8 +41,8 @@ def getData(request):
return render_to_json_response(response) return render_to_json_response(response)
actions.register(getData) actions.register(getData)
def parsePath(request): def parsePath(request, data):
path = json.loads(request.POST['data'])['path'] path = data['path']
response = json_response(ox.parse_movie_path(path)) response = json_response(ox.parse_movie_path(path))
return render_to_json_response(response) return render_to_json_response(response)
actions.register(parsePath) actions.register(parsePath)