update Shared

This commit is contained in:
j 2015-11-04 13:01:55 +01:00
commit 6881f3471a
184 changed files with 13080 additions and 13691 deletions

View file

@ -10,7 +10,7 @@ from ..shortcuts import render_to_json_response, json_response
from ...utils import json
def autodiscover():
#register api actions from all installed apps
# Register api actions from all installed apps
from django.utils.importlib import import_module
from django.utils.module_loading import module_has_submodule
for app in settings.INSTALLED_APPS:
@ -53,31 +53,24 @@ class ApiActions(dict):
versions = {}
def __init__(self):
def api(request):
def api(request, data):
'''
returns list of all known api actions
param data {
docs: bool
}
if docs is true, action properties contain docstrings
return {
status: {'code': int, 'text': string},
data: {
actions: {
'api': {
cache: true,
doc: 'recursion'
},
'hello': {
cache: true,
..
}
...
}
}
Returns a list of all api actions
takes {
code: boolean, // if true, return source code (optional)
docs: boolean // if true, return doc strings (optional)
}
returns {
actions: {
name: {
cache: boolean, // if false, don't cache results
code: string, // source code
doc: string // doc strings
},
... // more actions
}
}
'''
data = json.loads(request.POST.get('data', '{}'))
docs = data.get('docs', False)
code = data.get('code', False)
version = getattr(request, 'version', None)
@ -134,9 +127,9 @@ class ApiActions(dict):
actions = ApiActions()
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.
'''
success = error_is_success
return render_to_json_response({})

View file

@ -2,6 +2,8 @@
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division, with_statement
import json
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.conf import settings
@ -16,7 +18,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):
@ -28,14 +32,20 @@ def api(request):
'sitename': settings.SITENAME
})
return render_to_response('api.html', context)
action = request.POST['action']
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', '{}'))
version = getattr(request, 'version', None)
if version:
f = actions.versions.get(version, {}).get(action, actions.get(action))
else:
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 action %s' % action))