From 97fc9179c91f691f222e1d9492ab011a8e391298 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 26 Jan 2011 18:55:26 +0530 Subject: [PATCH] display code in api browser --- pandora/{annotaion => annotation}/__init__.py | 0 pandora/{annotaion => annotation}/admin.py | 0 pandora/{annotaion => annotation}/models.py | 0 pandora/{annotaion => annotation}/tests.py | 0 pandora/{annotaion => annotation}/utils.py | 0 pandora/{annotaion => annotation}/views.py | 0 pandora/api/actions.py | 13 +++++++++++- pandora/item/models.py | 2 +- pandora/settings.py | 2 +- static/js/pandora.api.js | 20 ++++++++++++++++--- 10 files changed, 31 insertions(+), 6 deletions(-) rename pandora/{annotaion => annotation}/__init__.py (100%) rename pandora/{annotaion => annotation}/admin.py (100%) rename pandora/{annotaion => annotation}/models.py (100%) rename pandora/{annotaion => annotation}/tests.py (100%) rename pandora/{annotaion => annotation}/utils.py (100%) rename pandora/{annotaion => annotation}/views.py (100%) diff --git a/pandora/annotaion/__init__.py b/pandora/annotation/__init__.py similarity index 100% rename from pandora/annotaion/__init__.py rename to pandora/annotation/__init__.py diff --git a/pandora/annotaion/admin.py b/pandora/annotation/admin.py similarity index 100% rename from pandora/annotaion/admin.py rename to pandora/annotation/admin.py diff --git a/pandora/annotaion/models.py b/pandora/annotation/models.py similarity index 100% rename from pandora/annotaion/models.py rename to pandora/annotation/models.py diff --git a/pandora/annotaion/tests.py b/pandora/annotation/tests.py similarity index 100% rename from pandora/annotaion/tests.py rename to pandora/annotation/tests.py diff --git a/pandora/annotaion/utils.py b/pandora/annotation/utils.py similarity index 100% rename from pandora/annotaion/utils.py rename to pandora/annotation/utils.py diff --git a/pandora/annotaion/views.py b/pandora/annotation/views.py similarity index 100% rename from pandora/annotaion/views.py rename to pandora/annotation/views.py diff --git a/pandora/api/actions.py b/pandora/api/actions.py index 6a2c151..7c46899 100644 --- a/pandora/api/actions.py +++ b/pandora/api/actions.py @@ -1,12 +1,14 @@ # -*- coding: utf-8 -*- # vi:si:et:sw=4:sts=4:ts=4 import sys +import inspect from django.conf import settings from ox.django.shortcuts import render_to_json_response, json_response from ox.utils import json + def autodiscover(): #register api actions from all installed apps from django.utils.importlib import import_module @@ -20,7 +22,6 @@ def autodiscover(): if module_has_submodule(mod, 'views'): raise - def trim(docstring): if not docstring: return '' @@ -77,6 +78,7 @@ class ApiActions(dict): ''' data = json.loads(request.POST.get('data', '{}')) docs = data.get('docs', False) + code = data.get('code', False) _actions = self.keys() _actions.sort() actions = {} @@ -84,6 +86,8 @@ class ApiActions(dict): actions[a] = self.properties[a] if docs: actions[a]['doc'] = self.doc(a) + if code: + actions[a]['code'] = self.code(a) response = json_response({'actions': actions}) return render_to_json_response(response) self.register(api) @@ -91,6 +95,12 @@ class ApiActions(dict): def doc(self, f): return trim(self[f].__doc__) + def code(self, name): + f = self[name] + if name != 'api' and hasattr(f, 'func_closure') and f.func_closure: + f = f.func_closure[0].cell_contents + return trim(inspect.getsource(f)) + def register(self, method, action=None, cache=True): if not action: action = method.func_name @@ -102,3 +112,4 @@ class ApiActions(dict): del self[action] actions = ApiActions() + diff --git a/pandora/item/models.py b/pandora/item/models.py index 0a96ae5..31397fa 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -24,7 +24,7 @@ import utils import tasks from archive import extract -from annotaion.models import Annotation, Layer +from annotation.models import Annotation, Layer from person.models import get_name_sort from app.models import site_config diff --git a/pandora/settings.py b/pandora/settings.py index 87fbd5a..9be10a8 100644 --- a/pandora/settings.py +++ b/pandora/settings.py @@ -119,7 +119,7 @@ INSTALLED_APPS = ( # 'south', 'djcelery', - 'annotaion', + 'annotation', 'app', 'archive', 'date', diff --git a/static/js/pandora.api.js b/static/js/pandora.api.js index 611d7b6..ac48ee9 100755 --- a/static/js/pandora.api.js +++ b/static/js/pandora.api.js @@ -21,7 +21,7 @@ var app = new Ox.App({ app.$ui.actionList = constructList(); app.$ui.actionInfo = Ox.Container().css({padding: '16px'}).html(app.config.default_info); - app.api.api({docs: true}, function(results) { + app.api.api({docs: true, code: true}, function(results) { app.actions = results.data.actions; if(document.location.hash) { @@ -118,9 +118,23 @@ function constructList() { hash = '#'; if(data.ids.length) $.each(data.ids, function(v, k) { - console.log(k) info.append($("
').html(app.actions[k]['doc'].replace('/\n/
\n/g'))); + info.append($('').html(app.actions[k].doc.replace('/\n/
\n/g'))); + + var $code = $('').html(app.actions[k].code.replace('/\n/
\n/g')) + .hide(); + var $button = new Ox.Button({ + title: [ + {id: "one", title: "expand"}, + {id: "two", title: "collapse"}, + ], + type: "image" + }) + .addClass("margin") + .click(function() { $code.toggle()}) + .appendTo(info) + $('').html(' View Python Source').appendTo(info) + $code.appendTo(info) hash += k + ',' }); else