From 1971296521a4582dc307a963fde44c5ceb6fab98 Mon Sep 17 00:00:00 2001 From: sanj Date: Wed, 19 Jan 2011 17:00:04 +0530 Subject: [PATCH] using actions.register --- itf/api/views.py | 87 +++++++++++-------- .../bestpractices/BestPractice/info.html | 9 +- 2 files changed, 54 insertions(+), 42 deletions(-) diff --git a/itf/api/views.py b/itf/api/views.py index 30c75ec..5ede06b 100644 --- a/itf/api/views.py +++ b/itf/api/views.py @@ -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', '
\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") diff --git a/itf/templates/bestpractices/BestPractice/info.html b/itf/templates/bestpractices/BestPractice/info.html index 4b08e9e..1187ece 100644 --- a/itf/templates/bestpractices/BestPractice/info.html +++ b/itf/templates/bestpractices/BestPractice/info.html @@ -1,7 +1,6 @@
- Story: ${story}

- Guideline: ${guideline}

- Law: ${law}

- Relevance to Theatre: ${theatre}

- Quick Howto: ${quick_howto}

+ Story: {{html tmplToHtml(story)}}

+ Law: {{html tmplToHtml(law)}}

+ Relevance to Theatre: {{html tmplToHtml(theatre)}}

+ Quick Howto: {{html tmplToHtml(quick_howto)}}