1import httplib2, os, sys, traceback, cgi 2 3from django.http import HttpResponse, HttpResponsePermanentRedirect 4from django.http import HttpResponseServerError 5from django.template import Context, loader 6from autotest_lib.client.common_lib import utils 7from autotest_lib.frontend import views_common 8from autotest_lib.frontend.afe import models, rpc_handler, rpc_interface 9from autotest_lib.frontend.afe import rpc_utils 10 11moblab_rpc_interface = utils.import_site_module( 12 __file__, 'autotest_lib.frontend.afe.moblab_rpc_interface', 13 dummy=object()) 14 15# since moblab_rpc_interface is later in the list, its methods will 16# override those of rpc_interface 17rpc_handler_obj = rpc_handler.RpcHandler((rpc_interface, 18 moblab_rpc_interface), 19 document_module=rpc_interface) 20 21 22def handle_rpc(request): 23 """Handle the RPC request. 24 25 @param request: the RPC request. 26 """ 27 return rpc_handler_obj.handle_rpc_request(request) 28 29 30def rpc_documentation(request): 31 """Return the rpc documentation. 32 33 @param request: the RPC request. 34 """ 35 return rpc_handler_obj.get_rpc_documentation() 36 37 38def model_documentation(request): 39 """Get the model documentation. 40 41 @param request: the RPC request. 42 """ 43 model_names = ('Label', 'Host', 'Test', 'User', 'AclGroup', 'Job', 44 'AtomicGroup') 45 return views_common.model_documentation(models, model_names) 46 47 48def redirect_with_extra_data(request, url, **kwargs): 49 """Redirect according to the extra data. 50 51 @param request: the RPC request. 52 @param url: the partial redirected url. 53 @param kwargs: the parameters used in redirection. 54 """ 55 kwargs['getdata'] = request.GET.urlencode() 56 kwargs['server_name'] = request.META['SERVER_NAME'] 57 return HttpResponsePermanentRedirect(url % kwargs) 58 59 60GWT_SERVER = 'http://localhost:8888/' 61def gwt_forward(request, forward_addr): 62 """Get the response from forwarding address. 63 64 @param request: the RPC request. 65 @param forward_addr: the forwarding address. 66 """ 67 url = GWT_SERVER + forward_addr 68 if len(request.POST) == 0: 69 headers, content = httplib2.Http().request(url, 'GET') 70 else: 71 headers, content = httplib2.Http().request(url, 'POST', 72 body=request.body) 73 http_response = HttpResponse(content) 74 for header, value in headers.iteritems(): 75 if header not in ('connection',): 76 http_response[header] = value 77 return http_response 78 79 80def handler500(request): 81 """Redirect to error website page. 82 83 @param request: the RPC request. 84 """ 85 t = loader.get_template('500.html') 86 trace = traceback.format_exc() 87 context = Context({ 88 'type': sys.exc_type, 89 'value': sys.exc_value, 90 'traceback': cgi.escape(trace) 91 }) 92 return HttpResponseServerError(t.render(context)) 93 94 95def handle_file_upload(request): 96 """Handler for uploading files. 97 98 Saves the files to /tmp and returns the resulting paths on disk. 99 100 @param request: request containing the file data. 101 102 @returns HttpResponse: with the paths of the saved files. 103 """ 104 if request.method == 'POST': 105 TEMPT_DIR = '/tmp/' 106 file_paths = [] 107 for file_name, upload_file in request.FILES.iteritems(): 108 file_path = os.path.join( 109 TEMPT_DIR, '_'.join([file_name, upload_file.name])) 110 with open(file_path, 'wb+') as destination: 111 for chunk in upload_file.chunks(): 112 destination.write(chunk) 113 file_paths.append(file_path) 114 return HttpResponse(rpc_utils.prepare_for_serialization(file_paths)) 115