Query trip: bridge the gap between FE nad BE
[unfold.git] / engine / manifoldproxy.py
1 # this view is what the javascript talks to when it sends a query
2 # see also
3 # myslice/urls.py
4 # as well as 
5 # static/js/manifold-async.js
6
7 import json
8 # this is for django objects only
9 #from django.core import serializers
10 from django.http import HttpResponse, HttpResponseForbidden
11
12 from engine.manifoldquery import ManifoldQuery
13 from engine.manifoldapi import ManifoldAPI
14
15 # xxx should probably cater for
16 # format_in : how is the query encoded in POST
17 # format_out: how to serve the results
18 def api (request,format):
19     # expecting a POST
20     if request.method != 'POST':
21         print "manifoldproxy.api: unexpected method %s -- exiting"%request.method
22         return 
23     # we only support json for now
24     if format != 'json':
25         print "manifoldproxy.api: unexpected format %s -- exiting"%format
26         return
27     # xxx actually ask the backend here
28     manifold_query = ManifoldQuery()
29     manifold_query.fill_from_dict(request.POST)
30     manifold_api_session_auth = request.session['manifold']['auth']
31     manifold_api= ManifoldAPI(auth=manifold_api_session_auth)
32     # forward
33     answer=manifold_api.send_manifold_query (manifold_query)
34     #hard_wired_answer = [ {'slice_hrn':'a.b.c'}, {'slice_hrn':'ple.inria.foo' } ]
35     #answer=hard_wired_answer
36     return HttpResponse (json.dumps(answer), mimetype="application/json")
37
38 #################### 
39 # to enable : see CSRF_FAILURE_VIEW in settings.py
40 # probably we want to elaborate this one a little in real life
41 # at least we can display the reason in the django output (although this turns out disappointing)
42 failure_answer=[ "csrf_failure" ]
43 def csrf_failure(request, reason=""):
44     print "CSRF failure with reason '%s'"%reason
45     return HttpResponseForbidden (json.dumps (failure_answer), mimetype="application/json")