1 from __future__ import print_function
6 # this is for django objects only
7 #from django.core import serializers
8 from django.http import HttpResponse, HttpResponseForbidden
10 #from manifoldapi.manifoldquery import ManifoldQuery
11 from manifold.core.query import Query
12 from manifold.core.result_value import ResultValue
13 from manifoldapi import ManifoldAPI
14 from manifoldresult import ManifoldException
15 from manifold.util.log import Log
17 from myslice.settings import config, logger, DEBUG
25 # pretend the server only returns - empty lists to 'get' requests - this is to mimick
26 # misconfigurations or expired credentials or similar corner case situations
30 # this view is what the javascript talks to when it sends a query
34 # static/js/manifold.js
35 def proxy (request,format):
36 """the view associated with /manifold/proxy/ with the query passed using POST"""
39 if request.method != 'POST':
40 logger.error("MANIFOLDPROXY unexpected method %s -- exiting" % request.method)
41 return HttpResponse ({"ret":0}, mimetype="application/json")
42 # we only support json for now
43 # if needed in the future we should probably cater for
44 # format_in : how is the query encoded in POST
45 # format_out: how to serve the results
47 logger.error("MANIFOLDPROXY unexpected format %s -- exiting" % format)
48 return HttpResponse ({"ret":0}, mimetype="application/json")
50 # translate incoming POST request into a query object
51 #logger.debug("MANIFOLDPROXY request.POST %s" % request.POST)
53 manifold_query = Query()
54 #manifold_query = ManifoldQuery()
55 manifold_query.fill_from_POST(request.POST)
56 # retrieve session for request
58 # We allow some requests to use the ADMIN user account
59 if (manifold_query.get_from() == 'local:user' and manifold_query.get_action() == 'create') \
60 or (manifold_query.get_from() == 'local:platform' and manifold_query.get_action() == 'get'):
61 admin_user, admin_password = config.manifold_admin_user_password()
62 manifold_api_session_auth = {'AuthMethod': 'password', 'Username': admin_user, 'AuthString': admin_password}
64 if 'manifold' in request.session:
65 manifold_api_session_auth = request.session['manifold']['auth']
67 return HttpResponse (json.dumps({'code':0,'value':[]}), mimetype="application/json")
69 if debug_empty and manifold_query.action.lower()=='get':
70 return HttpResponse (json.dumps({'code':0,'value':[]}), mimetype="application/json")
73 manifold_api= ManifoldAPI(auth=manifold_api_session_auth)
75 # for the benefit of the python code, manifoldAPI raises an exception if something is wrong
76 # however in this case we want to propagate the complete manifold result to the js world
78 result = manifold_api.forward(manifold_query.to_dict())
81 if 'description' in result and result['description'] \
82 and isinstance(result['description'], (tuple, list, set, frozenset)):
83 result [ 'description' ] = [ ResultValue.to_html (x) for x in result['description'] ]
88 # resource reservation
89 if (manifold_query.action.lower() == 'update') :
90 print(result['value'][0])
91 if 'resource' in result['value'][0] :
92 for resource in result['value'][0]['resource'] :
93 activity.slice.resource(request,
95 'slice' : result['value'][0]['slice_hrn'],
96 'resource' : resource['hostname'],
97 'resource_type' : resource['type'],
98 'facility' : resource['facility_name'],
99 'testbed' : resource['testbed_name']
103 json_answer=json.dumps(result)
105 return HttpResponse (json_answer, mimetype="application/json")
108 logger.error("MANIFOLDPROXY %s" % e)
110 traceback.print_exc()
111 return HttpResponse ({"ret":0}, mimetype="application/json")
114 # see CSRF_FAILURE_VIEW in settings.py
115 # the purpose of redefining this was to display the failure reason somehow
116 # this however turns out disappointing/not very informative
117 failure_answer=[ "csrf_failure" ]
118 def csrf_failure(request, reason=""):
119 print("CSRF failure with reason '%s'"%reason)
120 return HttpResponseForbidden (json.dumps (failure_answer), mimetype="application/json")