py3 support
This commit is contained in:
parent
98795d3a25
commit
0b80bebf15
26 changed files with 168 additions and 111 deletions
|
|
@ -1 +1,3 @@
|
|||
from actions import actions
|
||||
from __future__ import absolute_import
|
||||
|
||||
from .actions import actions
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from __future__ import division, with_statement
|
||||
from __future__ import division, absolute_import
|
||||
|
||||
import inspect
|
||||
import sys
|
||||
|
||||
|
|
@ -8,7 +9,7 @@ from django.conf import settings
|
|||
|
||||
from ..shortcuts import render_to_json_response, json_response
|
||||
|
||||
def autodiscover():
|
||||
def autodiscover(self=None):
|
||||
# Register api actions from all installed apps
|
||||
from importlib import import_module
|
||||
from django.utils.module_loading import module_has_submodule
|
||||
|
|
@ -28,14 +29,14 @@ def trim(docstring):
|
|||
# and split into a list of lines:
|
||||
lines = docstring.expandtabs().splitlines()
|
||||
# Determine minimum indentation (first line doesn't count):
|
||||
indent = sys.maxint
|
||||
indent = sys.maxsize
|
||||
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:
|
||||
if indent < sys.maxsize:
|
||||
for line in lines[1:]:
|
||||
trimmed.append(line[indent:].rstrip())
|
||||
# Strip off trailing and leading blank lines:
|
||||
|
|
@ -50,6 +51,9 @@ def trim(docstring):
|
|||
class ApiActions(dict):
|
||||
properties = {}
|
||||
versions = {}
|
||||
|
||||
autodiscover = autodiscover
|
||||
|
||||
def __init__(self):
|
||||
|
||||
def api(request, data):
|
||||
|
|
@ -74,10 +78,10 @@ class ApiActions(dict):
|
|||
code = data.get('code', False)
|
||||
version = getattr(request, 'version', None)
|
||||
if version:
|
||||
_actions = self.versions.get(version, {}).keys()
|
||||
_actions = list(set(_actions + self.keys()))
|
||||
_actions = list(self.versions.get(version, {}))
|
||||
_actions = list(set(_actions + list(self)))
|
||||
else:
|
||||
_actions = self.keys()
|
||||
_actions = list(self)
|
||||
_actions.sort()
|
||||
actions = {}
|
||||
for a in _actions:
|
||||
|
|
@ -111,7 +115,10 @@ class ApiActions(dict):
|
|||
|
||||
def register(self, method, action=None, cache=True, version=None):
|
||||
if not action:
|
||||
action = method.func_name
|
||||
if hasattr(method, 'func_name'):
|
||||
action = method.func_name
|
||||
else:
|
||||
action = method.__name__
|
||||
if version:
|
||||
if not version in self.versions:
|
||||
self.versions[version] = {}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from __future__ import absolute_import
|
||||
|
||||
from django.conf.urls import url
|
||||
|
||||
import views
|
||||
from . import views
|
||||
|
||||
import actions
|
||||
from . import actions
|
||||
actions.autodiscover()
|
||||
|
||||
urlpatterns = [
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from __future__ import division, with_statement
|
||||
from __future__ import division, absolute_import
|
||||
|
||||
import json
|
||||
|
||||
|
|
@ -8,9 +8,9 @@ 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 ..shortcuts import render_to_json_response, json_response, HttpErrorJson
|
||||
|
||||
from actions import actions
|
||||
from .actions import actions
|
||||
|
||||
def api(request):
|
||||
if request.META['REQUEST_METHOD'] == "OPTIONS":
|
||||
|
|
@ -21,7 +21,7 @@ def api(request):
|
|||
if request.META['REQUEST_METHOD'] != "POST" or (
|
||||
not 'action' in request.POST and request.META.get('CONTENT_TYPE') != 'application/json'
|
||||
):
|
||||
methods = actions.keys()
|
||||
methods = list(actions)
|
||||
api = []
|
||||
for f in sorted(methods):
|
||||
api.append({'name': f,
|
||||
|
|
@ -31,7 +31,9 @@ def api(request):
|
|||
'settings': settings,
|
||||
'sitename': settings.SITENAME
|
||||
})
|
||||
return render_to_response('api.html', context)
|
||||
response = render_to_response('api.html', context)
|
||||
response['Access-Control-Allow-Origin'] = '*'
|
||||
return response
|
||||
if request.META.get('CONTENT_TYPE') == 'application/json':
|
||||
r = json.loads(request.body)
|
||||
action = r['action']
|
||||
|
|
@ -45,7 +47,10 @@ def api(request):
|
|||
else:
|
||||
f = actions.get(action)
|
||||
if f:
|
||||
response = f(request, data)
|
||||
try:
|
||||
response = f(request, data)
|
||||
except HttpErrorJson as e:
|
||||
response = render_to_json_response(e.response)
|
||||
else:
|
||||
response = render_to_json_response(json_response(status=400,
|
||||
text='Unknown action %s' % action))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue