4 # this is for django objects only
5 #from django.core import serializers
6 from django.http import HttpResponse, HttpResponseForbidden
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
19 # add artificial delay in s
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
28 # turn this on if you want the fastest possible (locally cached) feedback
29 # beware that this is very rough though...
33 # this view is what the javascript talks to when it sends a query
37 # static/js/manifold.js
38 def proxy (request,format):
39 """the view associated with /manifold/proxy/
40 with the query passed using POST"""
43 if request.method != 'POST':
44 print "manifoldproxy.api: unexpected method %s -- exiting"%request.method
46 # we only support json for now
47 # if needed in the future we should probably cater for
48 # format_in : how is the query encoded in POST
49 # format_out: how to serve the results
51 print "manifoldproxy.proxy: unexpected format %s -- exiting"%format
54 # translate incoming POST request into a query object
55 if debug: print 'manifoldproxy.proxy: request.POST',request.POST
56 manifold_query = Query()
57 #manifold_query = ManifoldQuery()
58 manifold_query.fill_from_POST(request.POST)
59 offline_filename="%s/../offline-%s-%s.json"%(os.path.dirname(__file__),manifold_query.action,manifold_query.object)
60 # retrieve session for request
62 # We allow some requests to use the ADMIN user account
63 if (manifold_query.get_from() == 'local:user' and manifold_query.get_action() == 'create') or (manifold_query.get_from() == 'local:platform' and manifold_query.get_action() == 'get'):
64 admin_user, admin_password = Config().manifold_admin_user_password()
65 manifold_api_session_auth = {'AuthMethod': 'password', 'Username': admin_user, 'AuthString': admin_password}
67 manifold_api_session_auth = request.session['manifold']['auth']
69 if debug_empty and manifold_query.action.lower()=='get':
70 json_answer=json.dumps({'code':0,'value':[]})
71 print "By-passing : debug_empty & 'get' request : returning a fake empty list"
72 return HttpResponse (json_answer, mimetype="application/json")
73 ### patch : return the latest one..
75 # if that won't work then we'll try to update anyways
77 with (file(offline_filename,"r")) as f:
79 print "By-passing : using contents from %s"%offline_filename
80 return HttpResponse (json_answer, mimetype="application/json")
84 print "Could not run in offline mode, PROCEEDING"
88 manifold_api= ManifoldAPI(auth=manifold_api_session_auth)
89 if debug: print '===> manifoldproxy.proxy: sending to backend', manifold_query
90 # for the benefit of the python code, manifoldAPI raises an exception if something is wrong
91 # however in this case we want to propagate the complete manifold result to the js world
93 result = manifold_api.forward(manifold_query.to_dict())
96 if 'description' in result and result['description'] and isinstance(result['description'], (tuple, list, set, frozenset)):
97 result [ 'description' ] = [ ResultValue.to_html (x) for x in result['description'] ]
99 json_answer=json.dumps(result)
100 # if in debug mode we save this so we can use offline mode later
102 with (file(offline_filename,"w")) as f:
105 # this is an artificial delay added for debugging purposes only
107 print "Adding additional artificial delay",debug_spin
109 time.sleep(debug_spin)
111 return HttpResponse (json_answer, mimetype="application/json")
114 print "** PROXY ERROR **",e
116 traceback.print_exc()
119 # see CSRF_FAILURE_VIEW in settings.py
120 # the purpose of redefining this was to display the failure reason somehow
121 # this however turns out disappointing/not very informative
122 failure_answer=[ "csrf_failure" ]
123 def csrf_failure(request, reason=""):
124 print "CSRF failure with reason '%s'"%reason
125 return HttpResponseForbidden (json.dumps (failure_answer), mimetype="application/json")