From a4271fd81a772a41d304544faf6e2649eb692cc6 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sat, 7 Jul 2012 18:51:46 +0200 Subject: [PATCH] support versioned apis --- ox/django/api/actions.py | 36 +++++++++++++++++++++++++++--------- ox/django/api/views.py | 11 +++++++---- ox/django/shortcuts.py | 2 +- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/ox/django/api/actions.py b/ox/django/api/actions.py index 085b7fe..f651e18 100644 --- a/ox/django/api/actions.py +++ b/ox/django/api/actions.py @@ -50,6 +50,7 @@ def trim(docstring): class ApiActions(dict): properties = {} + versions = {} def __init__(self): def api(request): @@ -79,34 +80,51 @@ class ApiActions(dict): data = json.loads(request.POST.get('data', '{}')) docs = data.get('docs', False) code = data.get('code', False) - _actions = self.keys() + 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) + actions[a]['doc'] = self.doc(a, version) if code: - actions[a]['code'] = self.code(a) + actions[a]['code'] = self.code(a, version) response = json_response({'actions': actions}) return render_to_json_response(response) self.register(api) - def doc(self, f): - return trim(self[f].__doc__) + 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): - f = self[name] + 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: f = f.func_closure[0].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): + def register(self, method, action=None, cache=True, version=None): if not action: action = method.func_name - self[action] = method + 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): diff --git a/ox/django/api/views.py b/ox/django/api/views.py index edfe1dd..6983f15 100644 --- a/ox/django/api/views.py +++ b/ox/django/api/views.py @@ -25,14 +25,17 @@ def api(request): context = RequestContext(request, {'api': api, 'sitename': settings.SITENAME}) return render_to_response('api.html', context) - function = request.POST['action'] - - f = actions.get(function) + action = request.POST['action'] + 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) else: response = render_to_json_response(json_response(status=400, - text='Unknown function %s' % function)) + text='Unknown action %s' % action)) response['Access-Control-Allow-Origin'] = '*' return response diff --git a/ox/django/shortcuts.py b/ox/django/shortcuts.py index e11cf1a..b5be8b9 100644 --- a/ox/django/shortcuts.py +++ b/ox/django/shortcuts.py @@ -27,7 +27,7 @@ def render_to_json_response(dictionary, content_type="text/json", status=200): if settings.DEBUG: content_type = "text/javascript" indent = 2 - if settings.JSON_DEBUG: + if getattr(settings, 'JSON_DEBUG', False): print json.dumps(dictionary, indent=2, default=_to_json) return HttpResponse(json.dumps(dictionary, indent=indent, default=_to_json),