various
[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 # this view is what the javascript talks to when it sends a query
17 # see also
18 # myslice/urls.py
19 # as well as 
20 # static/js/manifold.js
21 def proxy (request,format):
22     """the view associated with /manifold/proxy/ 
23 with the query passed using POST"""
24     
25     # expecting a POST
26     if request.method != 'POST':
27         print "manifoldproxy.api: unexpected method %s -- exiting"%request.method
28         return 
29     # we only support json for now
30     # if needed in the future we should probably cater for
31     # format_in : how is the query encoded in POST
32     # format_out: how to serve the results
33     if format != 'json':
34         print "manifoldproxy.api: unexpected format %s -- exiting"%format
35         return
36     try:
37         # translate incoming POST request into a query object
38         if debug: print 'manifoldproxy.proxy: request.POST',request.POST
39         manifold_query = ManifoldQuery()
40         manifold_query.fill_from_POST(request.POST)
41         # retrieve session for request
42         manifold_api_session_auth = request.session['manifold']['auth']
43         # actually forward
44         manifold_api= ManifoldAPI(auth=manifold_api_session_auth)
45         if debug: print 'manifoldproxy.proxy: sending to backend', manifold_query
46         answer=manifold_api.send_manifold_query (manifold_query)
47         if debug_spin:
48             import time
49             time.sleep(debug_spin)
50         # return json-encoded answer
51         return HttpResponse (json.dumps(answer), mimetype="application/json")
52     except:
53         import traceback
54         traceback.print_exc()
55
56 #################### 
57 # see CSRF_FAILURE_VIEW in settings.py
58 # the purpose of redefining this was to display the failure reason somehow
59 # this however turns out disappointing/not very informative
60 failure_answer=[ "csrf_failure" ]
61 def csrf_failure(request, reason=""):
62     print "CSRF failure with reason '%s'"%reason
63     return HttpResponseForbidden (json.dumps (failure_answer), mimetype="application/json")