tear down old offline mode that is not really helpful anymore and probably broken...
[myslice.git] / manifold / manifoldproxy.py
1 import json
2 import os.path
3
4 # this is for django objects only
5 #from django.core import serializers
6 from django.http                import HttpResponse, HttpResponseForbidden
7
8 #from manifold.manifoldquery import ManifoldQuery
9 from manifold.core.query        import Query
10 from manifold.core.result_value import ResultValue
11 from manifold.manifoldapi       import ManifoldAPI
12 from manifold.manifoldresult    import ManifoldException
13 from manifold.util.log          import Log
14 from myslice.config             import Config
15
16 debug=False
17 debug=True
18
19 # add artificial delay in s
20 debug_spin=0
21 #debug_spin=1
22
23 # pretend the server only returns - empty lists to 'get' requests - this is to mimick 
24 # misconfigurations or expired credentials or similar corner case situations
25 debug_empty=False
26 #debug_empty=True
27
28 # this view is what the javascript talks to when it sends a query
29 # see also
30 # myslice/urls.py
31 # as well as 
32 # static/js/manifold.js
33 def proxy (request,format):
34     """the view associated with /manifold/proxy/ 
35 with the query passed using POST"""
36     
37     # expecting a POST
38     if request.method != 'POST':
39         print "manifoldproxy.api: unexpected method %s -- exiting"%request.method
40         return 
41     # we only support json for now
42     # if needed in the future we should probably cater for
43     # format_in : how is the query encoded in POST
44     # format_out: how to serve the results
45     if format != 'json':
46         print "manifoldproxy.proxy: unexpected format %s -- exiting"%format
47         return
48     try:
49         # translate incoming POST request into a query object
50         if debug: print 'manifoldproxy.proxy: request.POST',request.POST
51         manifold_query = Query()
52         #manifold_query = ManifoldQuery()
53         manifold_query.fill_from_POST(request.POST)
54         # retrieve session for request
55
56         # We allow some requests to use the ADMIN user account
57         if (manifold_query.get_from() == 'local:user' and manifold_query.get_action() == 'create') \
58                 or (manifold_query.get_from() == 'local:platform' and manifold_query.get_action() == 'get'):
59             admin_user, admin_password = Config().manifold_admin_user_password()
60             manifold_api_session_auth = {'AuthMethod': 'password', 'Username': admin_user, 'AuthString': admin_password}
61         else:
62             manifold_api_session_auth = request.session['manifold']['auth']
63
64         if debug_empty and manifold_query.action.lower()=='get':
65             json_answer=json.dumps({'code':0,'value':[]})
66             print "By-passing : debug_empty & 'get' request : returning a fake empty list"
67             return HttpResponse (json_answer, mimetype="application/json")
68                 
69         # actually forward
70         manifold_api= ManifoldAPI(auth=manifold_api_session_auth)
71         if debug: print '===> manifoldproxy.proxy: sending to backend', manifold_query
72         # for the benefit of the python code, manifoldAPI raises an exception if something is wrong
73         # however in this case we want to propagate the complete manifold result to the js world
74
75         result = manifold_api.forward(manifold_query.to_dict())
76
77         # XXX TEMP HACK
78         if 'description' in result and result['description'] \
79                 and isinstance(result['description'], (tuple, list, set, frozenset)):
80             result [ 'description' ] = [ ResultValue.to_html (x) for x in result['description'] ]
81
82         json_answer=json.dumps(result)
83
84         # this is an artificial delay added for debugging purposes only
85         if debug_spin>0:
86             print "Adding additional artificial delay",debug_spin
87             import time
88             time.sleep(debug_spin)
89
90         return HttpResponse (json_answer, mimetype="application/json")
91
92     except Exception,e:
93         print "** PROXY ERROR **",e
94         import traceback
95         traceback.print_exc()
96
97 #################### 
98 # see CSRF_FAILURE_VIEW in settings.py
99 # the purpose of redefining this was to display the failure reason somehow
100 # this however turns out disappointing/not very informative
101 failure_answer=[ "csrf_failure" ]
102 def csrf_failure(request, reason=""):
103     print "CSRF failure with reason '%s'"%reason
104     return HttpResponseForbidden (json.dumps (failure_answer), mimetype="application/json")