manifoldproxy in debug mode also shows backend's answer
[myslice.git] / manifold / manifoldproxy.py
1 import json
2 # this is for django objects only
3 #from django.core import serializers
4 from django.http import HttpResponse, HttpResponseForbidden
5
6 from manifold.manifoldquery import ManifoldQuery
7 from manifold.manifoldapi import ManifoldAPI
8
9 debug=False
10 debug=True
11
12 # add artificial delay in s
13 debug_spin=0
14 #debug_spin=1
15
16 # turn this on if you want the fastest possible (locally cached) feedback
17 # beware that this is very rough though...
18 work_offline=False
19 #work_offline=True
20
21 # this view is what the javascript talks to when it sends a query
22 # see also
23 # myslice/urls.py
24 # as well as 
25 # static/js/manifold.js
26 def proxy (request,format):
27     """the view associated with /manifold/proxy/ 
28 with the query passed using POST"""
29     
30     # expecting a POST
31     if request.method != 'POST':
32         print "manifoldproxy.api: unexpected method %s -- exiting"%request.method
33         return 
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
38     if format != 'json':
39         print "manifoldproxy.api: unexpected format %s -- exiting"%format
40         return
41     try:
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..
50         if work_offline:
51             # if that won't work then we'll try to update anyways
52             try:
53                 with (file(offline_filename,"r")) as f:
54                     json_answer=f.read()
55                 print "By-passing : using contents from %s"%offline_filename
56                 return HttpResponse (json_answer, mimetype="application/json")
57             except:
58                 import traceback
59                 traceback.print_exc()
60                 print "PROCEEDING"
61                 pass
62                 
63         # actually forward
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: 
68             try:        print "received answer from backend with %d rows"%len(answer)
69             except:     print "received answer from backend - can't say len"
70         json_answer=json.dumps(answer)
71         if (debug):
72             with (file(offline_filename,"w")) as f:
73                 f.write(json_answer)
74         if debug_spin:
75             import time
76             time.sleep(debug_spin)
77         # return json-encoded answer
78         return HttpResponse (json_answer, mimetype="application/json")
79     except:
80         import traceback
81         traceback.print_exc()
82
83 #################### 
84 # see CSRF_FAILURE_VIEW in settings.py
85 # the purpose of redefining this was to display the failure reason somehow
86 # this however turns out disappointing/not very informative
87 failure_answer=[ "csrf_failure" ]
88 def csrf_failure(request, reason=""):
89     print "CSRF failure with reason '%s'"%reason
90     return HttpResponseForbidden (json.dumps (failure_answer), mimetype="application/json")