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