OLD | NEW |
(Empty) | |
| 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 import jinja2 |
| 6 import os |
| 7 import services |
| 8 import urllib |
| 9 import webapp2 |
| 10 |
| 11 from google.appengine.ext import blobstore |
| 12 from google.appengine.ext.webapp import blobstore_handlers |
| 13 |
| 14 |
| 15 JINJA_ENVIRONMENT = jinja2.Environment( |
| 16 loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), |
| 17 extensions=['jinja2.ext.autoescape']) |
| 18 |
| 19 |
| 20 class MainPage(webapp2.RequestHandler): |
| 21 """Show breakdown with received profiler-id and template-id. If nothing was |
| 22 received, show blank page waiting user to upload file.""" |
| 23 def get(self): |
| 24 page_template = JINJA_ENVIRONMENT.get_template('index.html') |
| 25 upload_url = blobstore.create_upload_url('/upload') |
| 26 |
| 27 # Get profiler id and template id from url query. |
| 28 run_id = self.request.get('run_id') |
| 29 tmpl_id = self.request.get('tmpl_id') |
| 30 upload_msg = self.request.get('upload_msg') |
| 31 |
| 32 template_values = { |
| 33 'upload_url': upload_url, |
| 34 'upload_msg': upload_msg |
| 35 } |
| 36 |
| 37 if not run_id or not tmpl_id: |
| 38 template_values['json'] = 'null' |
| 39 template_values['template'] = 'null' |
| 40 |
| 41 self.response.write(page_template.render(template_values)) |
| 42 else: |
| 43 template_values['json'] = services.GetProfiler(run_id) |
| 44 template_values['template'] = services.GetTemplate(tmpl_id) |
| 45 |
| 46 self.response.write(page_template.render(template_values)) |
| 47 |
| 48 |
| 49 class UploadHandler(blobstore_handlers.BlobstoreUploadHandler): |
| 50 """Handle file uploading with BlobstoreUploadHandler. BlobstoreUploadHandler |
| 51 can deal with files overweighing size limitation within one HTTP connection so |
| 52 that user can upload large json file.""" |
| 53 def post(self): |
| 54 blob_info = self.get_uploads('file')[0] |
| 55 |
| 56 run_id = services.CreateProfiler(blob_info) |
| 57 default_key = services.CreateTemplates(blob_info) |
| 58 |
| 59 # TODO(junjianx): Validation of uploaded file should be done separately. |
| 60 if not default_key: |
| 61 # Jump to home page with error message. |
| 62 req_params = { |
| 63 'upload_msg': 'No default key was found.' |
| 64 } |
| 65 self.redirect('/?' + urllib.urlencode(req_params)) |
| 66 else: |
| 67 # Jump to new graph page using default template. |
| 68 req_params = { |
| 69 'run_id': run_id, |
| 70 'tmpl_id': default_key.urlsafe() |
| 71 } |
| 72 self.redirect('/?' + urllib.urlencode(req_params)) |
| 73 |
| 74 |
| 75 application = webapp2.WSGIApplication([ |
| 76 ('/', MainPage), |
| 77 ('/upload', UploadHandler) |
| 78 ], debug=True) |
OLD | NEW |