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