2 # this is for django objects only
3 #from django.core import serializers
4 from django.http import HttpResponse, HttpResponseForbidden
6 from manifold.manifoldquery import ManifoldQuery
7 from manifold.manifoldapi import ManifoldAPI
12 # add artificial delay in s
16 # turn this on if you want the fastest possible (locally cached) feedback
17 # beware that this is very rough though...
21 # this view is what the javascript talks to when it sends a query
25 # static/js/manifold.js
26 def proxy (request,format):
27 """the view associated with /manifold/proxy/
28 with the query passed using POST"""
31 if request.method != 'POST':
32 print "manifoldproxy.api: unexpected method %s -- exiting"%request.method
34 # we only support json for now
35 # if needed in the future we should probably cater for
36 # format_in : how is the query encoded in POST
37 # format_out: how to serve the results
39 print "manifoldproxy.api: unexpected format %s -- exiting"%format
42 # translate incoming POST request into a query object
43 if debug: print 'manifoldproxy.proxy: request.POST',request.POST
44 manifold_query = ManifoldQuery()
45 manifold_query.fill_from_POST(request.POST)
46 offline_filename="offline-%s-%s.json"%(manifold_query.action,manifold_query.subject)
47 # retrieve session for request
48 manifold_api_session_auth = request.session['manifold']['auth']
49 ### patch : return the latest one..
51 # if that won't work then we'll try to update anyways
53 with (file(offline_filename,"r")) as f:
55 print "By-passing : using contents from %s"%offline_filename
56 return HttpResponse (json_answer, mimetype="application/json")
64 manifold_api= ManifoldAPI(auth=manifold_api_session_auth)
65 if debug: print 'manifoldproxy.proxy: sending to backend', manifold_query
66 answer=manifold_api.send_manifold_query (manifold_query)
67 if debug: print 'manifoldproxy.proxy: received from backend with code', answer['code']
68 json_answer=json.dumps(answer)
70 with (file(offline_filename,"w")) as f:
74 time.sleep(debug_spin)
75 return HttpResponse (json_answer, mimetype="application/json")
82 # see CSRF_FAILURE_VIEW in settings.py
83 # the purpose of redefining this was to display the failure reason somehow
84 # this however turns out disappointing/not very informative
85 failure_answer=[ "csrf_failure" ]
86 def csrf_failure(request, reason=""):
87 print "CSRF failure with reason '%s'"%reason
88 return HttpResponseForbidden (json.dumps (failure_answer), mimetype="application/json")