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 | |
23 | |
24 # W0232: 23,0:MyRequestHandler: Class has no __init__ method | |
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 | 15 |
41 | 16 |
42 # W0232: 42,0:PageAction: Class has no __init__ method | 17 # W0232: 42,0:PageAction: Class has no __init__ method |
43 # pylint: disable=W0232 | 18 # pylint: disable=W0232 |
44 class PageAction(MyRequestHandler): | 19 class PageAction(base_page.BasePage): |
45 | 20 |
46 # W0221: 44,2:PageAction.get: Arguments number differs from overridden method | 21 # W0221: 44,2:PageAction.get: Arguments number differs from overridden method |
47 # pylint: disable=W0221 | 22 # pylint: disable=W0221 |
48 def get(self, localpath): | 23 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: | 24 if len(self.request.query_string) > 0: |
53 # E1101: 47,11:PageAction.get: Instance of 'PageAction' has no 'request' | 25 # The refresh arg, if present, must be stripped from the URL. |
54 # member | 26 args = self.request.query_string.split('&') |
55 # pylint: disable=E1101 | 27 args = [arg for arg in args if not arg.startswith('refresh=')] |
56 localpath += '?' + self.request.query_string | 28 if args: |
29 localpath += '?' + '&'.join(args) | |
57 unquoted_localpath = urllib.unquote(localpath) | 30 unquoted_localpath = urllib.unquote(localpath) |
58 content = app.get_and_cache_page(unquoted_localpath) | 31 page_data = app.get_and_cache_pagedata(unquoted_localpath) |
59 if content is None: | 32 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 | 33 self.error(404) # file not found |
64 return | 34 return |
65 | 35 |
36 self.response.headers['Content-Type'] = app.path_to_mime_type( | |
37 unquoted_localpath) | |
38 template_values = self.InitializeTemplate() | |
39 if self.request.path.endswith('/console'): | |
40 template_values = self.InitializeTemplate() | |
41 template_values['body_class'] = page_data.get('body_class') | |
M-A Ruel
2012/05/29 18:46:33
optional: would also be a perfect fit for page_dat
cmp
2012/05/29 19:38:03
Yeah, I agree. If you have an idea in the other c
| |
42 template_values['content'] = page_data.get('content') | |
43 template_values['offsite_base'] = page_data.get('offsite_base') | |
44 template_values['title'] = page_data.get('title') | |
45 if self.user: | |
46 refresh = utils.clean_int(self.request.get('refresh'), -1) | |
47 if refresh != -1: | |
48 refresh = max(refresh, 30) | |
49 template_values['refresh'] = refresh | |
50 else: | |
51 # Make the Google Frontend capable of caching this request for 60 | |
52 # seconds. | |
53 # TODO: Caching is not working yet. | |
54 self.response.headers['Cache-Control'] = 'public, max-age=60' | |
55 self.response.headers['Pragma'] = 'Public' | |
56 self.DisplayTemplate('base.html', template_values) | |
57 return | |
58 | |
66 # Make the Google Frontend capable of caching this request for 60 seconds. | 59 # Make the Google Frontend capable of caching this request for 60 seconds. |
67 # TODO: Caching is not working yet. | 60 # 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' | 61 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' | 62 self.response.headers['Pragma'] = 'Public' |
76 # E1101: 83,4:PageAction.get: Instance of 'PageAction' has no 'response' | 63 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 | 64 |
83 | 65 |
84 | 66 |
85 class FetchPagesAction(MyRequestHandler): | 67 class FetchPagesAction(base_page.BasePage): |
86 | 68 |
87 # R0201: 93,2:FetchPagesAction.get: Method could be a function | 69 # R0201: 93,2:FetchPagesAction.get: Method could be a function |
88 # pylint: disable=R0201 | 70 # pylint: disable=R0201 |
89 def get(self): | 71 def get(self): |
90 deferred.defer(app.fetch_pages) | 72 deferred.defer(app.fetch_pages) |
91 | 73 |
92 | 74 |
93 class MainAction(MyRequestHandler): | 75 class MainAction(base_page.BasePage): |
94 | 76 |
95 def get(self): | 77 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') | 78 self.redirect('/p/chromium/console') |
100 | 79 |
101 | 80 |
102 # Call initial bootstrap for the app module. | 81 # Call initial bootstrap for the app module. |
103 app.bootstrap() | 82 app.bootstrap() |
83 base_page.bootstrap() | |
104 | 84 |
105 # GAE will cache |application| across requests if we set it here. See | 85 # 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 | 86 # http://code.google.com/appengine/docs/python/runtime.html#App_Caching for more |
107 # info. | 87 # info. |
108 application = webapp2.WSGIApplication( | 88 application = webapp2.WSGIApplication( |
109 [('/', MainAction), | 89 [('/', MainAction), |
110 ('/p/(.*)', PageAction), | 90 ('/p/(.*)', PageAction), |
111 ('/tasks/fetch_pages', FetchPagesAction)]) | 91 ('/tasks/fetch_pages', FetchPagesAction)]) |
OLD | NEW |