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

View File

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