Home | History | Annotate | Download | only in handlers
      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 
     42 def get_content_type(filename):
     43     return mimetypes.guess_type(filename)[0] or "application/octet-stream"
     44 
     45 
     46 class GetDashboardFile(webapp.RequestHandler):
     47     def get(self, resource):
     48         if not resource:
     49             logging.debug("Getting dashboard file list.")
     50             return self._get_file_list()
     51 
     52         filename = str(urllib2.unquote(resource))
     53 
     54         logging.debug("Getting dashboard file: %s", filename)
     55 
     56         files = DashboardFile.get_files(filename)
     57         if not files:
     58             logging.error("Failed to find dashboard file: %s, request: %s",
     59                 filename, self.request)
     60             self.response.set_status(404)
     61             return
     62 
     63         content_type = "%s; charset=utf-8" % get_content_type(filename)
     64         logging.info("content type: %s", content_type)
     65         self.response.headers["Content-Type"] = content_type
     66         self.response.out.write(files[0].data)
     67 
     68     def _get_file_list(self):
     69         logging.info("getting dashboard file list.")
     70 
     71         files = DashboardFile.get_files("", 100)
     72         if not files:
     73             logging.info("Failed to find dashboard files.")
     74             self.response.set_status(404)
     75             return
     76 
     77         template_values = {
     78             "admin": users.is_current_user_admin(),
     79             "files": files,
     80         }
     81         self.response.out.write(
     82             template.render("templates/dashboardfilelist.html",
     83                 template_values))
     84 
     85 
     86 class UpdateDashboardFile(webapp.RequestHandler):
     87     def get(self):
     88         files = self.request.get_all(PARAM_FILE)
     89         if not files:
     90             # FIXME: Just grab the entire dashboards directory.
     91             files = ["aggregate_results.html",
     92                      "builders.js",
     93                      "dashboard_base.js",
     94                      "dygraph-combined.js",
     95                      "flakiness_dashboard.html",
     96                      "timeline_explorer.html",
     97                      "treemap.html",
     98                      "webtreemap.css",
     99                      "webtreemap.js"]
    100 
    101         errors = []
    102         for file in files:
    103             if not DashboardFile.update_file(file):
    104                 errors.append("Failed to update file: %s" % file)
    105 
    106         if errors:
    107             messages = "; ".join(errors)
    108             logging.warning(messages)
    109             self.response.set_status(500, messages)
    110             self.response.out.write("FAIL")
    111         else:
    112             self.response.set_status(200)
    113             self.response.out.write("OK")
    114 
    115 
    116 class DeleteDashboardFile(webapp.RequestHandler):
    117     def get(self):
    118         files = self.request.get_all(PARAM_FILE)
    119         if not files:
    120             logging.warning("No dashboard file to delete.")
    121             self.response.set_status(400)
    122             return
    123 
    124         for file in files:
    125             DashboardFile.delete_file(file)
    126 
    127         # Display dashboard file list after deleting the file.
    128         self.redirect("/dashboards/")
    129