OLD | NEW |
---|---|
(Empty) | |
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 | |
3 # found in the LICENSE file. | |
4 | |
5 import datetime | |
6 import logging | |
7 import os.path | |
8 import re | |
9 | |
10 from google.appengine.api import memcache | |
11 from google.appengine.api import users | |
12 # F0401: 9,0: Unable to import 'webapp2' | |
M-A Ruel
2012/05/29 18:46:33
You should add it to sys.path in the PRESUBMIT che
cmp
2012/05/29 19:38:03
This is provided by GAE SDK. It is not a submodul
| |
13 # pylint: disable=F0401 | |
14 import webapp2 | |
15 from google.appengine.ext.webapp import template | |
16 | |
17 | |
18 # W0232: 23,0:MyRequestHandler: Class has no __init__ method | |
M-A Ruel
2012/05/29 18:46:33
I thought I had disabled this warning, feel free t
cmp
2012/05/29 19:38:03
Done.
| |
19 # pylint: disable=W0232 | |
20 class BasePage(webapp2.RequestHandler): | |
21 """Base request handler with this application specific helpers.""" | |
22 # Check if the username ends with @chromium.org/@google.com. | |
23 _VALID_EMAIL = re.compile(r"^.*@(chromium\.org|google\.com)$") | |
M-A Ruel
2012/05/29 18:46:33
replace .* with .+ :)
cmp
2012/05/29 19:38:03
Done. You should look at chromium-status, the sam
| |
24 app_name = '' | |
25 _is_admin = None | |
26 _user = None | |
27 _initialized = False | |
28 | |
29 def _late_init(self): | |
30 """Initializes self._is_admin and self._user once the request object is | |
31 setup. | |
32 """ | |
33 self._is_admin = False | |
34 | |
35 self._user = users.get_current_user() | |
36 if not self._is_admin and self._user: | |
37 self._is_admin = bool( | |
38 users.is_current_user_admin() or | |
39 self._VALID_EMAIL.match(self._user.email())) | |
40 self._initialized = True | |
41 logging.info('Admin: %s, User: %s' % (self._is_admin, self._user)) | |
42 | |
43 @property | |
44 def is_admin(self): | |
45 if not self._initialized: | |
46 self._late_init() | |
47 return self._is_admin | |
48 | |
49 @property | |
50 def user(self): | |
51 if not self._initialized: | |
52 self._late_init() | |
53 return self._user | |
54 | |
55 def InitializeTemplate(self, title='Chromium Build'): | |
56 """Initializes the template values with information needed by all pages.""" | |
57 if self.user: | |
M-A Ruel
2012/05/29 18:46:33
optional style nit: I'd make that a one line:
nick
cmp
2012/05/29 19:38:03
Done.
| |
58 user_nickname = self.user.email() | |
59 else: | |
60 user_nickname = '' | |
61 template_values = { | |
62 'app_name': self.app_name, | |
63 'current_utc_time': datetime.datetime.now(), | |
64 'is_admin': self.is_admin, | |
65 'login_url': users.create_login_url(self.request.url), | |
66 'logout_url': users.create_logout_url(self.request.url), | |
67 'title': title, | |
68 'user': self.user, | |
69 'user_nickname': user_nickname, | |
70 } | |
71 return template_values | |
72 | |
73 def DisplayTemplate(self, name, template_values, use_cache=False): | |
74 """Replies to a http request with a template. | |
75 | |
76 Optionally cache it for 1 second. Only to be used for user-invariant | |
M-A Ruel
2012/05/29 18:46:33
Any way to assert this programmatically? I don't t
cmp
2012/05/29 19:38:03
Nothing quick and easy that I can think of. Mostl
| |
77 pages! | |
78 """ | |
79 self.response.headers['Cache-Control'] = 'no-cache, private, max-age=0' | |
80 buff = None | |
81 if use_cache: | |
82 buff = memcache.get(name) | |
83 if not buff: | |
84 path = os.path.join(os.path.dirname(__file__), 'templates/%s' % name) | |
85 buff = template.render(path, template_values) | |
86 if use_cache: | |
87 memcache.add(name, buff, 1) | |
88 self.response.out.write(buff) | |
89 | |
90 | |
91 def bootstrap(): | |
92 app_name = os.environ['APPLICATION_ID'] | |
93 BasePage.app_name = app_name | |
OLD | NEW |