add ox.django.api
This commit is contained in:
parent
c4d80675ae
commit
5c2e5e6a3a
5 changed files with 178 additions and 0 deletions
38
ox/django/api/views.py
Normal file
38
ox/django/api/views.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from __future__ import division, with_statement
|
||||
|
||||
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 not 'action' in request.POST:
|
||||
methods = actions.keys()
|
||||
api = []
|
||||
for f in sorted(methods):
|
||||
api.append({'name': f,
|
||||
'doc': actions.doc(f).replace('\n', '<br>\n')})
|
||||
context = RequestContext(request, {'api': api,
|
||||
'sitename': settings.SITENAME})
|
||||
return render_to_response('api.html', context)
|
||||
function = request.POST['action']
|
||||
|
||||
f = actions.get(function)
|
||||
if f:
|
||||
response = f(request)
|
||||
else:
|
||||
response = render_to_json_response(json_response(status=400,
|
||||
text='Unknown function %s' % function))
|
||||
response['Access-Control-Allow-Origin'] = '*'
|
||||
return response
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue