return 403 if csrf is not OK with our custom view
[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 # xxx should probably cater for
13 # format_in : how is the query encoded in POST
14 # format_out: how to serve the results
15 def api (request,format):
16     # expecting a POST
17     if request.method != 'POST':
18         print "manifoldproxy.api: unexpected method %s -- exiting"%request.method
19         return 
20     # we only support json for now
21     if format != 'json':
22         print "manifoldproxy.api: unexpected format %s -- exiting"%format
23         return
24
25     # xxx actually ask the backend here
26     hard_wired_answer = [ {'slice_hrn':'a.b.c'}, {'slice_hrn':'ple.inria.foo' } ]
27     answer=hard_wired_answer
28     return HttpResponse (json.dumps(answer), mimetype="application/json")
29
30 #################### 
31 # to enable : see CSRF_FAILURE_VIEW in settings.py
32 # probably we want to elaborate this one a little in real life
33 # at least we can display the reason in the django output (although this turns out disappointing)
34 failure_answer=[ "csrf_failure" ]
35 def csrf_failure(request, reason=""):
36     print "CSRF failure with reason '%s'"%reason
37     return HttpResponseForbidden (json.dumps (failure_answer), mimetype="application/json")