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