forked from 0x2620/pandora
display code in api browser
This commit is contained in:
parent
0daf4067a1
commit
72e4146c25
10 changed files with 31 additions and 6 deletions
|
@ -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()
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -119,7 +119,7 @@ INSTALLED_APPS = (
|
|||
# 'south',
|
||||
'djcelery',
|
||||
|
||||
'annotaion',
|
||||
'annotation',
|
||||
'app',
|
||||
'archive',
|
||||
'date',
|
||||
|
|
|
@ -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($("<h2>").html(k));
|
||||
info.append($('<pre>').html(app.actions[k]['doc'].replace('/\n/<br>\n/g')));
|
||||
info.append($('<pre>').html(app.actions[k].doc.replace('/\n/<br>\n/g')));
|
||||
|
||||
var $code = $('<pre>').html(app.actions[k].code.replace('/\n/<br>\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)
|
||||
$('<span>').html(' View Python Source').appendTo(info)
|
||||
$code.appendTo(info)
|
||||
hash += k + ','
|
||||
});
|
||||
else
|
||||
|
|
Loading…
Reference in a new issue