OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import os.path | |
6 import urllib | 5 import urllib |
7 | 6 |
8 from google.appengine.ext import deferred | 7 from google.appengine.ext import deferred |
9 # F0401: 9,0: Unable to import 'webapp2' | 8 # F0401: 9,0: Unable to import 'webapp2' |
10 # pylint: disable=F0401 | 9 # pylint: disable=F0401 |
11 import webapp2 | 10 import webapp2 |
12 from google.appengine.ext.webapp import template | |
13 | 11 |
14 import app | 12 import app |
15 | 13 import base_page |
16 def _clean_int(value, default): | 14 import utils |
17 """Convert a value to an int, or the default value if conversion fails.""" | |
18 try: | |
19 return int(value) | |
20 except (TypeError, ValueError): | |
21 return default | |
22 | 15 |
23 | 16 |
24 # W0232: 23,0:MyRequestHandler: Class has no __init__ method | 17 class PageAction(base_page.BasePage): |
25 # pylint: disable=W0232 | |
26 class MyRequestHandler(webapp2.RequestHandler): | |
27 """Base request handler with this application specific helpers.""" | |
28 | |
29 # E1101: 31,4:MyRequestHandler._render_template: Instance of | |
30 # 'MyRequestHandler' has no 'response' member | |
31 # pylint: disable=E1101 | |
32 def _render_template(self, name, values): | |
33 """ | |
34 Wrapper for template.render that updates response | |
35 and knows where to look for templates. | |
36 """ | |
37 self.response.out.write(template.render( | |
38 os.path.join(os.path.dirname(__file__), 'templates', name), | |
39 values)) | |
40 | |
41 | |
42 # W0232: 42,0:PageAction: Class has no __init__ method | |
43 # pylint: disable=W0232 | |
44 class PageAction(MyRequestHandler): | |
45 | 18 |
46 # W0221: 44,2:PageAction.get: Arguments number differs from overridden method | 19 # W0221: 44,2:PageAction.get: Arguments number differs from overridden method |
47 # pylint: disable=W0221 | 20 # pylint: disable=W0221 |
48 def get(self, localpath): | 21 def get(self, localpath): |
49 # E1101: 70,11:PageAction.get: Instance of 'PageAction' has no 'request' | |
50 # member | |
51 # pylint: disable=E1101 | |
52 if len(self.request.query_string) > 0: | 22 if len(self.request.query_string) > 0: |
53 # E1101: 47,11:PageAction.get: Instance of 'PageAction' has no 'request' | 23 # The reload arg, if present, must be stripped from the URL. |
54 # member | 24 args = self.request.query_string.split('&') |
55 # pylint: disable=E1101 | 25 args = [arg for arg in args if not arg.startswith('reload=')] |
56 localpath += '?' + self.request.query_string | 26 if args: |
| 27 localpath += '?' + '&'.join(args) |
57 unquoted_localpath = urllib.unquote(localpath) | 28 unquoted_localpath = urllib.unquote(localpath) |
58 content = app.get_and_cache_page(unquoted_localpath) | 29 page_data = app.get_and_cache_pagedata(unquoted_localpath) |
59 if content is None: | 30 if page_data.get('content') is None: |
60 # E1101: 78,6:PageAction.get: Instance of 'PageAction' has no 'error' | |
61 # member | |
62 # pylint: disable=E1101 | |
63 self.error(404) # file not found | 31 self.error(404) # file not found |
64 return | 32 return |
65 | 33 |
| 34 self.response.headers['Content-Type'] = app.path_to_mime_type( |
| 35 unquoted_localpath) |
| 36 template_values = self.InitializeTemplate() |
| 37 if self.request.path.endswith('/console'): |
| 38 template_values = self.InitializeTemplate() |
| 39 template_values['body_class'] = page_data.get('body_class') |
| 40 template_values['content'] = page_data.get('content') |
| 41 template_values['offsite_base'] = page_data.get('offsite_base') |
| 42 template_values['title'] = page_data.get('title') |
| 43 if self.user: |
| 44 reloadarg = utils.clean_int(self.request.get('reload'), -1) |
| 45 if reloadarg != -1: |
| 46 reloadarg = max(reloadarg, 30) |
| 47 template_values['reloadarg'] = reloadarg |
| 48 else: |
| 49 # Make the Google Frontend capable of caching this request for 60 |
| 50 # seconds. |
| 51 # TODO: Caching is not working yet. |
| 52 self.response.headers['Cache-Control'] = 'public, max-age=60' |
| 53 self.response.headers['Pragma'] = 'Public' |
| 54 self.DisplayTemplate('base.html', template_values) |
| 55 return |
| 56 |
66 # Make the Google Frontend capable of caching this request for 60 seconds. | 57 # Make the Google Frontend capable of caching this request for 60 seconds. |
67 # TODO: Caching is not working yet. | 58 # TODO: Caching is not working yet. |
68 # E1101: 83,4:PageAction.get: Instance of 'PageAction' has no 'response' | |
69 # member | |
70 # pylint: disable=E1101 | |
71 self.response.headers['Cache-Control'] = 'public, max-age=60' | 59 self.response.headers['Cache-Control'] = 'public, max-age=60' |
72 # E1101: 83,4:PageAction.get: Instance of 'PageAction' has no 'response' | |
73 # member | |
74 # pylint: disable=E1101 | |
75 self.response.headers['Pragma'] = 'Public' | 60 self.response.headers['Pragma'] = 'Public' |
76 # E1101: 83,4:PageAction.get: Instance of 'PageAction' has no 'response' | 61 self.response.out.write(page_data.get('content')) |
77 # member | |
78 # pylint: disable=E1101 | |
79 self.response.headers['Content-Type'] = app.path_to_mime_type( | |
80 unquoted_localpath) | |
81 self.response.out.write(content) | |
82 | 62 |
83 | 63 |
84 | 64 |
85 class FetchPagesAction(MyRequestHandler): | 65 class FetchPagesAction(base_page.BasePage): |
86 | 66 |
87 # R0201: 93,2:FetchPagesAction.get: Method could be a function | 67 # R0201: 93,2:FetchPagesAction.get: Method could be a function |
88 # pylint: disable=R0201 | 68 # pylint: disable=R0201 |
89 def get(self): | 69 def get(self): |
90 deferred.defer(app.fetch_pages) | 70 deferred.defer(app.fetch_pages) |
91 | 71 |
92 | 72 |
93 class MainAction(MyRequestHandler): | 73 class MainAction(base_page.BasePage): |
94 | 74 |
95 def get(self): | 75 def get(self): |
96 # E1101: 96,4:MainAction.get: Instance of 'MainAction' has no 'redirect' | |
97 # member | |
98 # pylint: disable=E1101 | |
99 self.redirect('/p/chromium/console') | 76 self.redirect('/p/chromium/console') |
100 | 77 |
101 | 78 |
102 # Call initial bootstrap for the app module. | 79 # Call initial bootstrap for the app module. |
103 app.bootstrap() | 80 app.bootstrap() |
| 81 base_page.bootstrap() |
104 | 82 |
105 # GAE will cache |application| across requests if we set it here. See | 83 # GAE will cache |application| across requests if we set it here. See |
106 # http://code.google.com/appengine/docs/python/runtime.html#App_Caching for more | 84 # http://code.google.com/appengine/docs/python/runtime.html#App_Caching for more |
107 # info. | 85 # info. |
108 application = webapp2.WSGIApplication( | 86 application = webapp2.WSGIApplication( |
109 [('/', MainAction), | 87 [('/', MainAction), |
110 ('/p/(.*)', PageAction), | 88 ('/p/(.*)', PageAction), |
111 ('/tasks/fetch_pages', FetchPagesAction)]) | 89 ('/tasks/fetch_pages', FetchPagesAction)]) |
OLD | NEW |