migrate to django 1.9
This commit is contained in:
parent
f7855147ce
commit
e5dd8aaab7
48 changed files with 659 additions and 848 deletions
1
oxdata/oxdjango/api/__init__.py
Normal file
1
oxdata/oxdjango/api/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from actions import actions
|
||||
135
oxdata/oxdjango/api/actions.py
Normal file
135
oxdata/oxdjango/api/actions.py
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from __future__ import division, with_statement
|
||||
import inspect
|
||||
import sys
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from ..shortcuts import render_to_json_response, json_response
|
||||
|
||||
def autodiscover():
|
||||
# Register api actions from all installed apps
|
||||
from importlib import import_module
|
||||
from django.utils.module_loading import module_has_submodule
|
||||
for app in settings.INSTALLED_APPS:
|
||||
if app != 'api':
|
||||
mod = import_module(app)
|
||||
try:
|
||||
import_module('%s.views'%app)
|
||||
except:
|
||||
if module_has_submodule(mod, 'views'):
|
||||
raise
|
||||
|
||||
def trim(docstring):
|
||||
if not docstring:
|
||||
return ''
|
||||
# Convert tabs to spaces (following the normal Python rules)
|
||||
# and split into a list of lines:
|
||||
lines = docstring.expandtabs().splitlines()
|
||||
# Determine minimum indentation (first line doesn't count):
|
||||
indent = sys.maxint
|
||||
for line in lines[1:]:
|
||||
stripped = line.lstrip()
|
||||
if stripped:
|
||||
indent = min(indent, len(line) - len(stripped))
|
||||
# Remove indentation (first line is special):
|
||||
trimmed = [lines[0].strip()]
|
||||
if indent < sys.maxint:
|
||||
for line in lines[1:]:
|
||||
trimmed.append(line[indent:].rstrip())
|
||||
# Strip off trailing and leading blank lines:
|
||||
while trimmed and not trimmed[-1]:
|
||||
trimmed.pop()
|
||||
while trimmed and not trimmed[0]:
|
||||
trimmed.pop(0)
|
||||
# Return a single string:
|
||||
return '\n'.join(trimmed)
|
||||
|
||||
|
||||
class ApiActions(dict):
|
||||
properties = {}
|
||||
versions = {}
|
||||
def __init__(self):
|
||||
|
||||
def api(request, data):
|
||||
'''
|
||||
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
|
||||
}
|
||||
}
|
||||
'''
|
||||
docs = data.get('docs', False)
|
||||
code = data.get('code', False)
|
||||
version = getattr(request, 'version', None)
|
||||
if version:
|
||||
_actions = self.versions.get(version, {}).keys()
|
||||
_actions = list(set(_actions + self.keys()))
|
||||
else:
|
||||
_actions = self.keys()
|
||||
_actions.sort()
|
||||
actions = {}
|
||||
for a in _actions:
|
||||
actions[a] = self.properties[a]
|
||||
if docs:
|
||||
actions[a]['doc'] = self.doc(a, version)
|
||||
if code:
|
||||
actions[a]['code'] = self.code(a, version)
|
||||
response = json_response({'actions': actions})
|
||||
return render_to_json_response(response)
|
||||
self.register(api)
|
||||
|
||||
def doc(self, name, version=None):
|
||||
if version:
|
||||
f = self.versions[version].get(name, self.get(name))
|
||||
else:
|
||||
f = self[name]
|
||||
return trim(f.__doc__)
|
||||
|
||||
def code(self, name, version=None):
|
||||
if version:
|
||||
f = self.versions[version].get(name, self.get(name))
|
||||
else:
|
||||
f = self[name]
|
||||
if name != 'api' and hasattr(f, 'func_closure') and f.func_closure:
|
||||
fc = filter(lambda c: hasattr(c.cell_contents, '__call__'), f.func_closure)
|
||||
f = fc[len(fc)-1].cell_contents
|
||||
info = f.func_code.co_filename[len(settings.PROJECT_ROOT)+1:]
|
||||
info = u'%s:%s' % (info, f.func_code.co_firstlineno)
|
||||
return info, trim(inspect.getsource(f))
|
||||
|
||||
def register(self, method, action=None, cache=True, version=None):
|
||||
if not action:
|
||||
action = method.func_name
|
||||
if version:
|
||||
if not version in self.versions:
|
||||
self.versions[version] = {}
|
||||
self.versions[version][action] = method
|
||||
else:
|
||||
self[action] = method
|
||||
self.properties[action] = {'cache': cache}
|
||||
|
||||
def unregister(self, action):
|
||||
if action in self:
|
||||
del self[action]
|
||||
|
||||
actions = ApiActions()
|
||||
|
||||
def error(request, data):
|
||||
'''
|
||||
This action is used to test API error codes. It should return a 503 error.
|
||||
'''
|
||||
success = error_is_success
|
||||
return render_to_json_response({})
|
||||
actions.register(error)
|
||||
13
oxdata/oxdjango/api/urls.py
Normal file
13
oxdata/oxdjango/api/urls.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
|
||||
from django.conf.urls import url
|
||||
|
||||
import views
|
||||
|
||||
import actions
|
||||
actions.autodiscover()
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^$', views.api),
|
||||
]
|
||||
54
oxdata/oxdjango/api/views.py
Normal file
54
oxdata/oxdjango/api/views.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
# -*- coding: utf-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
|
||||
|
||||
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'
|
||||
):
|
||||
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,
|
||||
'settings': settings,
|
||||
'sitename': settings.SITENAME
|
||||
})
|
||||
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', '{}'))
|
||||
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, data)
|
||||
else:
|
||||
response = render_to_json_response(json_response(status=400,
|
||||
text='Unknown action %s' % action))
|
||||
response['Access-Control-Allow-Origin'] = '*'
|
||||
return response
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue