towards supporting python 2 and 3
- use absolute_imports - make use of six.moves - use exec instead of execfile - use list(dict) instead if dict.keys()
This commit is contained in:
parent
728ed14499
commit
1468ddbecb
89 changed files with 400 additions and 265 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
|
||||
|
|
@ -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
|
||||
|
||||
|
|
@ -10,7 +10,7 @@ from django.conf import settings
|
|||
|
||||
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,
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from __future__ import absolute_import
|
||||
|
||||
try:
|
||||
from django.contrib.auth.decorators import wraps
|
||||
except:
|
||||
from django.utils.functional import wraps
|
||||
from shortcuts import render_to_json_response
|
||||
from .shortcuts import render_to_json_response
|
||||
|
||||
def login_required_json(function=None):
|
||||
"""
|
||||
|
|
@ -13,11 +14,11 @@ def login_required_json(function=None):
|
|||
return json error if not logged in.
|
||||
"""
|
||||
|
||||
def _wrapped_view(request, *args, **kwargs):
|
||||
if request.user.is_authenticated():
|
||||
return function(request, *args, **kwargs)
|
||||
return render_to_json_response({'status': {'code': 401, 'text': 'login required'}})
|
||||
return wraps(function)(_wrapped_view)
|
||||
def _wrapped_view(request, *args, **kwargs):
|
||||
if request.user.is_authenticated():
|
||||
return function(request, *args, **kwargs)
|
||||
return render_to_json_response({'status': {'code': 401, 'text': 'login required'}})
|
||||
return wraps(function)(_wrapped_view)
|
||||
|
||||
def admin_required_json(function=None):
|
||||
"""
|
||||
|
|
@ -25,8 +26,8 @@ def admin_required_json(function=None):
|
|||
return json error if not logged in.
|
||||
"""
|
||||
|
||||
def _wrapped_view(request, *args, **kwargs):
|
||||
if request.user.is_authenticated() and request.user.profile.get_level() == 'admin':
|
||||
return function(request, *args, **kwargs)
|
||||
return render_to_json_response({'status': {'code': 403, 'text': 'permission denied'}})
|
||||
return wraps(function)(_wrapped_view)
|
||||
def _wrapped_view(request, *args, **kwargs):
|
||||
if request.user.is_authenticated() and request.user.profile.get_level() == 'admin':
|
||||
return function(request, *args, **kwargs)
|
||||
return render_to_json_response({'status': {'code': 403, 'text': 'permission denied'}})
|
||||
return wraps(function)(_wrapped_view)
|
||||
|
|
|
|||
|
|
@ -27,15 +27,16 @@ def _to_json(python_object):
|
|||
raise TypeError(u'%s %s is not JSON serializable' % (repr(python_object), type(python_object)))
|
||||
|
||||
def render_to_json_response(dictionary, content_type="text/json", status=200):
|
||||
indent=None
|
||||
indent = None
|
||||
if settings.DEBUG:
|
||||
content_type = "text/javascript"
|
||||
indent = 2
|
||||
if getattr(settings, 'JSON_DEBUG', False):
|
||||
print(json.dumps(dictionary, indent=2, default=_to_json, ensure_ascii=False).encode('utf-8'))
|
||||
|
||||
return HttpResponse(json.dumps(dictionary, indent=indent, default=_to_json,
|
||||
ensure_ascii=False).encode('utf-8'), content_type=content_type, status=status)
|
||||
response = json.dumps(dictionary, indent=indent, default=_to_json, ensure_ascii=False)
|
||||
if not isinstance(response, bytes):
|
||||
response = response.encode('utf-8')
|
||||
return HttpResponse(response, content_type=content_type, status=status)
|
||||
|
||||
def get_object_or_404_json(klass, *args, **kwargs):
|
||||
from django.shortcuts import _get_queryset
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue