more comments + cosmetic
[unfold.git] / engine / 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 engine.manifoldquery import ManifoldQuery
7 from engine.manifoldapi import ManifoldAPI
8
9 # this view is what the javascript talks to when it sends a query
10 # see also
11 # myslice/urls.py
12 # as well as 
13 # static/js/manifold-async.js
14 def api (request,format):
15     """the view associated with /manifold/api/ 
16 with the query passed using POST"""
17     
18     # expecting a POST
19     if request.method != 'POST':
20         print "manifoldproxy.api: unexpected method %s -- exiting"%request.method
21         return 
22     # we only support json for now
23     # if needed in the future we should probably cater for
24     # format_in : how is the query encoded in POST
25     # format_out: how to serve the results
26     if format != 'json':
27         print "manifoldproxy.api: unexpected format %s -- exiting"%format
28         return
29     # translate incoming POST request into a query object
30     manifold_query = ManifoldQuery()
31     manifold_query.fill_from_dict(request.POST)
32     # retrieve session for request
33     manifold_api_session_auth = request.session['manifold']['auth']
34     # actually forward
35     manifold_api= ManifoldAPI(auth=manifold_api_session_auth)
36     answer=manifold_api.send_manifold_query (manifold_query)
37     # return json-encoded answer
38     return HttpResponse (json.dumps(answer), mimetype="application/json")
39
40 #################### 
41 # see CSRF_FAILURE_VIEW in settings.py
42 # the purpose of redefining this was to display the failure reason somehow
43 # this however turns out disappointing/not very informative
44 failure_answer=[ "csrf_failure" ]
45 def csrf_failure(request, reason=""):
46     print "CSRF failure with reason '%s'"%reason
47     return HttpResponseForbidden (json.dumps (failure_answer), mimetype="application/json")