using actions.register

This commit is contained in:
sanj 2011-01-19 17:00:04 +05:30
parent 7ea11616ce
commit 1971296521
2 changed files with 54 additions and 42 deletions

View File

@ -23,21 +23,29 @@ from ox.django.decorators import login_required_json
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response
from ox.django.http import HttpFileResponse
import ox
from actions import actions
def api(request):
if request.META['REQUEST_METHOD'] == "OPTIONS":
response = HttpResponse('')
response = render_to_json_response({'status': {'code': 200, 'text': 'use POST'}})
response = render_to_json_response({'status': {'code': 200,
'text': 'use POST'}})
response['Access-Control-Allow-Origin'] = '*'
return response
if not 'action' in request.POST:
return apidoc(request)
methods = actions.keys()
api = []
for f in sorted(methods):
api.append({'name': f,
'doc': actions.doc(f).replace('\n', '<br>\n')})
context = RequestContext(request, {'api': api,
'sitename': settings.SITENAME})
return render_to_response('api.html', context)
function = request.POST['action']
#FIXME: possible to do this in f
#data = json.loads(request.POST['data'])
f = globals().get('api_'+function, None)
f = actions.get(function, None)
if f:
response = f(request)
else:
@ -46,41 +54,41 @@ def api(request):
response['Access-Control-Allow-Origin'] = '*'
return response
def api_api(request):
'''
returns list of all known api action
return {'status': {'code': int, 'text': string},
'data': {actions: ['api', 'hello', ...]}}
'''
actions = globals().keys()
actions = map(lambda a: a[4:], filter(lambda a: a.startswith('api_'), actions))
actions.sort()
ret = {}
#FIXME: set cache to False for login, logout, etc.
for a in actions:
ret[a] = {
'cache': True
}
return render_to_json_response(json_response({'actions': ret}))
#def api_api(request):
# '''
# returns list of all known api action
# return {'status': {'code': int, 'text': string},
# 'data': {actions: ['api', 'hello', ...]}}
# '''
# actions = globals().keys()
# actions = map(lambda a: a[4:], filter(lambda a: a.startswith('api_'), actions))
# actions.sort()
# ret = {}
# #FIXME: set cache to False for login, logout, etc.
# for a in actions:
# ret[a] = {
# 'cache': True
# }
# return render_to_json_response(json_response({'actions': ret}))
def api_apidoc(request):
'''
returns array of actions with documentation
'''
actions = globals().keys()
actions = map(lambda a: a[4:], filter(lambda a: a.startswith('api_'), actions))
actions.sort()
docs = {}
for f in actions:
docs[f] = get_api_doc(f)
return render_to_json_response(json_response({'actions': docs}))
#def api_apidoc(request):
# '''
# returns array of actions with documentation
# '''
# actions = globals().keys()
# actions = map(lambda a: a[4:], filter(lambda a: a.startswith('api_'), actions))
# actions.sort()
# docs = {}
# for f in actions:
# docs[f] = get_api_doc(f)
# return render_to_json_response(json_response({'actions': docs}))
#FIXME: REMOVE THIS FUNCTION WHEN THERE ARE REAL USERS!!!!
def get_user_json(u):
return {'name': 'Guest', 'group': 'guest', 'preferences': {}}
def api_hello(request):
def hello(request):
'''
return {'status': {'code': int, 'text': string},
'data': {user: object}}
@ -92,16 +100,17 @@ def api_hello(request):
else:
response['data']['user'] = {'name': 'Guest', 'group': 'guest', 'preferences': {}}
return render_to_json_response(response)
actions.register(hello)
def api_error(request):
def error(request):
'''
trows 503 error
'''
success = error_is_success
return render_to_json_response({})
actions.register(error)
def api_find(request):
def find(request):
data = json.loads(request.POST['data'])
# print json.dumps(data)
model = getModel(data)
@ -118,6 +127,7 @@ def api_find(request):
response['data']['positions'] = _get_positions(ids, l)
response['status'] = {'code': 200}
return render_to_json_response(response)
actions.register(find)
def _get_positions(ids, l):
ret = {}
@ -131,7 +141,7 @@ def _get_positions(ids, l):
return ret
def api_preview(request):
def preview(request):
data = json.loads(request.POST['data'])
if not data.has_key('id'):
return render_to_json_response({'status': {'code': 404}})
@ -143,9 +153,10 @@ def api_preview(request):
response['data'] = obj.preview_dict()
response['template'] = getTemplate(data, "preview")
return render_to_json_response(response)
actions.register(preview)
#FIXME: Generalize based on these two functions being the same.
def api_info(request):
def info(request):
data = json.loads(request.POST['data'])
id = int(data['id'])
model = getModel(data)
@ -155,6 +166,8 @@ def api_info(request):
response['data'] = obj.info_dict()
response['template'] = getTemplate(data, "info")
return render_to_json_response(response)
actions.register(info)
def getTemplate(data, tmpl_name):
path = join(settings.PROJECT_PATH, "templates", data['module'], data['model'], tmpl_name + ".html")

View File

@ -1,7 +1,6 @@
<div class="itfInfo">
<span class="itfInfoSub">Story: </span><span class="itfInfoInfo">${story}</span><br /><br />
<span class="itfInfoSub">Guideline: </span><span class="itfInfoInfo">${guideline}</span><br /><br />
<span class="itfInfoSub">Law: </span><span class="itfInfoInfo">${law}</span><br /><br />
<span class="itfInfoSub">Relevance to Theatre: </span><span class="itfInfoInfo">${theatre}</span><br /><br />
<span class="itfInfoSub">Quick Howto: </span><span class="itfInfoInfo">${quick_howto}</span><br /><br />
<span class="itfInfoSub">Story: </span><span class="itfInfoInfo">{{html tmplToHtml(story)}}</span><br /><br />
<span class="itfInfoSub">Law: </span><span class="itfInfoInfo">{{html tmplToHtml(law)}}</span><br /><br />
<span class="itfInfoSub">Relevance to Theatre: </span><span class="itfInfoInfo">{{html tmplToHtml(theatre)}}</span><br /><br />
<span class="itfInfoSub">Quick Howto: </span><span class="itfInfoInfo">{{html tmplToHtml(quick_howto)}}</span><br /><br />
</div>