python-ox/ox/django/api/views.py

55 lines
1.9 KiB
Python
Raw Normal View History

2012-01-03 20:00:33 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division, with_statement
import json
2012-01-03 20:00:33 +00:00
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.conf import settings
from ..shortcuts import render_to_json_response, json_response
from actions import actions
def api(request):
if request.META['REQUEST_METHOD'] == "OPTIONS":
response = render_to_json_response({'status': {'code': 200,
'text': 'use POST'}})
response['Access-Control-Allow-Origin'] = '*'
return response
if request.META['REQUEST_METHOD'] != "POST" or (
not 'action' in request.POST and request.META.get('CONTENT_TYPE') != 'application/json'
):
2012-01-03 20:00:33 +00:00
methods = actions.keys()
api = []
for f in sorted(methods):
api.append({'name': f,
'doc': actions.doc(f).replace('\n', '<br>\n')})
2013-06-07 09:25:28 +00:00
context = RequestContext(request, {
'api': api,
'settings': settings,
'sitename': settings.SITENAME
})
2012-01-03 20:00:33 +00:00
return render_to_response('api.html', context)
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', '{}'))
2012-07-07 16:51:46 +00:00
version = getattr(request, 'version', None)
if version:
f = actions.versions.get(version, {}).get(action, actions.get(action))
else:
f = actions.get(action)
2012-01-03 20:00:33 +00:00
if f:
response = f(request, data)
2012-01-03 20:00:33 +00:00
else:
response = render_to_json_response(json_response(status=400,
2012-07-07 16:51:46 +00:00
text='Unknown action %s' % action))
2012-01-03 20:00:33 +00:00
response['Access-Control-Allow-Origin'] = '*'
return response