Home | History | Annotate | Download | only in afe
      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 moblab_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
     17 rpc_handler_obj = rpc_handler.RpcHandler((rpc_interface,
     18                                           moblab_rpc_interface),
     19                                          document_module=rpc_interface)
     20 
     21 
     22 def 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 
     30 def 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 
     38 def 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 
     48 def 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 
     60 GWT_SERVER = 'http://localhost:8888/'
     61 def 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 
     80 def 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 
     95 def 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