• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5# This file is expected to be used under another directory to use,
6# so we disable checking import path of GAE tools from this directory.
7# pylint: disable=F0401,E0611,W0232
8
9import jinja2
10import json
11import os
12import re
13import urllib
14import webapp2
15
16from google.appengine.ext import blobstore
17from google.appengine.ext.webapp import blobstore_handlers
18
19import services
20
21
22JINJA_ENVIRONMENT = jinja2.Environment(
23  loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
24  extensions=['jinja2.ext.autoescape'])
25
26
27class MainPage(webapp2.RequestHandler):
28  """Show breakdown with received profiler-id and template-id. If nothing was
29  received, show blank page waiting user to upload file."""
30  def get(self):
31    page_template = JINJA_ENVIRONMENT.get_template('index.html')
32    upload_url = blobstore.create_upload_url('/upload')
33
34    # Get profiler id and template id from url query.
35    run_id = self.request.get('run_id')
36    tmpl_id = self.request.get('tmpl_id')
37    upload_msg = self.request.get('upload_msg')
38
39    template_values = {
40      'upload_url': upload_url,
41      'upload_msg': upload_msg
42    }
43
44    if run_id and tmpl_id:
45      template_values['json'] = services.GetProfiler(run_id)
46      template_values['template'] = services.GetTemplate(tmpl_id)
47
48    self.response.write(page_template.render(template_values))
49
50
51class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
52  """Handle file uploading with BlobstoreUploadHandler. BlobstoreUploadHandler
53  can deal with files overweighing size limitation within one HTTP connection so
54  that user can upload large json file."""
55  def post(self):
56    blob_info = self.get_uploads('file')[0]
57
58    run_id = services.CreateProfiler(blob_info)
59    default_key = services.CreateTemplates(blob_info)
60
61    # TODO(junjianx): Validation of uploaded file should be done separately.
62    if not default_key:
63      # Jump to home page with error message.
64      req_params = {
65        'upload_msg': 'No default_template key was found.'
66      }
67    else:
68      # Jump to new graph page using default template.
69      req_params = {
70        'run_id': run_id,
71        'tmpl_id': default_key.urlsafe()
72      }
73
74    self.redirect('/?' + urllib.urlencode(req_params))
75
76
77class ShareHandler(webapp2.RequestHandler):
78  """Handle breakdown template sharing. Generate public url for transferred
79  template and return it back."""
80  def post(self):
81    run_id = self.request.POST['run_id']
82    content = json.loads(self.request.POST['content'])
83    tmpl_key = services.CreateTemplate(content)
84
85    req_params = {
86      'run_id': run_id,
87      'tmpl_id': tmpl_key.urlsafe()
88    }
89
90    # Take out host url from request by removing share suffix.
91    url = re.sub('share', '', self.request.url)
92    self.response.write(url + '?' + urllib.urlencode(req_params))
93
94
95application = webapp2.WSGIApplication([
96  ('/', MainPage),
97  ('/upload', UploadHandler),
98  ('/share', ShareHandler)
99], debug=True)
100