WebKitTools/TestResultServer/handlers/dashboardhandler.py
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 # Copyright (C) 2010 Google Inc. All rights reserved.
       
     2 #
       
     3 # Redistribution and use in source and binary forms, with or without
       
     4 # modification, are permitted provided that the following conditions are
       
     5 # met:
       
     6 # 
       
     7 #     * Redistributions of source code must retain the above copyright
       
     8 # notice, this list of conditions and the following disclaimer.
       
     9 #     * Redistributions in binary form must reproduce the above
       
    10 # copyright notice, this list of conditions and the following disclaimer
       
    11 # in the documentation and/or other materials provided with the
       
    12 # distribution.
       
    13 #     * Neither the name of Google Inc. nor the names of its
       
    14 # contributors may be used to endorse or promote products derived from
       
    15 # this software without specific prior written permission.
       
    16 # 
       
    17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    28 
       
    29 import logging
       
    30 import mimetypes
       
    31 import urllib2
       
    32 
       
    33 from google.appengine.api import users
       
    34 from google.appengine.ext import webapp
       
    35 from google.appengine.ext.webapp import template
       
    36 
       
    37 from model.dashboardfile import DashboardFile
       
    38 
       
    39 PARAM_FILE = "file"
       
    40 
       
    41 def get_content_type(filename):
       
    42     return mimetypes.guess_type(filename)[0] or "application/octet-stream"
       
    43 
       
    44 
       
    45 class GetDashboardFile(webapp.RequestHandler):
       
    46     def get(self, resource):
       
    47         if not resource:
       
    48             logging.debug("Getting dashboard file list.")
       
    49             return self._get_file_list()
       
    50 
       
    51         filename = str(urllib2.unquote(resource))
       
    52 
       
    53         logging.debug("Getting dashboard file: %s", filename)
       
    54 
       
    55         files = DashboardFile.get_files(filename)
       
    56         if not files:
       
    57             logging.error("Failed to find dashboard file: %s, request: %s",
       
    58                 filename, self.request)
       
    59             self.response.set_status(404)
       
    60             return
       
    61 
       
    62         content_type = "%s; charset=utf-8" % get_content_type(filename)
       
    63         logging.info("content type: %s", content_type)
       
    64         self.response.headers["Content-Type"] = content_type
       
    65         self.response.out.write(files[0].data)
       
    66 
       
    67     def _get_file_list(self):
       
    68         logging.info("getting dashboard file list.")
       
    69 
       
    70         files = DashboardFile.get_files("", 100)
       
    71         if not files:
       
    72             logging.info("Failed to find dashboard files.")
       
    73             self.response.set_status(404)
       
    74             return
       
    75 
       
    76         template_values = {
       
    77             "admin": users.is_current_user_admin(),
       
    78             "files": files,
       
    79         }
       
    80         self.response.out.write(
       
    81             template.render("templates/dashboardfilelist.html",
       
    82                 template_values))
       
    83 
       
    84 
       
    85 class UpdateDashboardFile(webapp.RequestHandler):
       
    86     def get(self):
       
    87         files = self.request.get_all(PARAM_FILE)
       
    88         if not files:
       
    89             files = ["flakiness_dashboard.html",
       
    90                      "dashboard_base.js",
       
    91                      "aggregate_results.html"]
       
    92 
       
    93         errors = []
       
    94         for file in files:
       
    95             if not DashboardFile.update_file(file):
       
    96                 errors.append("Failed to update file: %s" % file)
       
    97 
       
    98         if errors:
       
    99             messages = "; ".join(errors)
       
   100             logging.warning(messages)
       
   101             self.response.set_status(500, messages)
       
   102             self.response.out.write("FAIL")
       
   103         else:
       
   104             self.response.set_status(200)
       
   105             self.response.out.write("OK")
       
   106 
       
   107 
       
   108 class DeleteDashboardFile(webapp.RequestHandler):
       
   109     def get(self):
       
   110         files = self.request.get_all(PARAM_FILE)
       
   111         if not files:
       
   112             logging.warning("No dashboard file to delete.")
       
   113             self.response.set_status(400)
       
   114             return
       
   115 
       
   116         for file in files:
       
   117             DashboardFile.delete_file(file)
       
   118 
       
   119         # Display dashboard file list after deleting the file.
       
   120         self.redirect("/dashboards/")