Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2282)

Unified Diff: base_page.py

Issue 10448057: Add refresh support to the console page. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/chromium-build
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base_page.py
diff --git a/base_page.py b/base_page.py
new file mode 100644
index 0000000000000000000000000000000000000000..793903b9c5417396628af6385396db17c842bb8c
--- /dev/null
+++ b/base_page.py
@@ -0,0 +1,93 @@
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import datetime
+import logging
+import os.path
+import re
+
+from google.appengine.api import memcache
+from google.appengine.api import users
+# 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
+# pylint: disable=F0401
+import webapp2
+from google.appengine.ext.webapp import template
+
+
+# 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.
+# pylint: disable=W0232
+class BasePage(webapp2.RequestHandler):
+ """Base request handler with this application specific helpers."""
+ # Check if the username ends with @chromium.org/@google.com.
+ _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
+ app_name = ''
+ _is_admin = None
+ _user = None
+ _initialized = False
+
+ def _late_init(self):
+ """Initializes self._is_admin and self._user once the request object is
+ setup.
+ """
+ self._is_admin = False
+
+ self._user = users.get_current_user()
+ if not self._is_admin and self._user:
+ self._is_admin = bool(
+ users.is_current_user_admin() or
+ self._VALID_EMAIL.match(self._user.email()))
+ self._initialized = True
+ logging.info('Admin: %s, User: %s' % (self._is_admin, self._user))
+
+ @property
+ def is_admin(self):
+ if not self._initialized:
+ self._late_init()
+ return self._is_admin
+
+ @property
+ def user(self):
+ if not self._initialized:
+ self._late_init()
+ return self._user
+
+ def InitializeTemplate(self, title='Chromium Build'):
+ """Initializes the template values with information needed by all pages."""
+ 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.
+ user_nickname = self.user.email()
+ else:
+ user_nickname = ''
+ template_values = {
+ 'app_name': self.app_name,
+ 'current_utc_time': datetime.datetime.now(),
+ 'is_admin': self.is_admin,
+ 'login_url': users.create_login_url(self.request.url),
+ 'logout_url': users.create_logout_url(self.request.url),
+ 'title': title,
+ 'user': self.user,
+ 'user_nickname': user_nickname,
+ }
+ return template_values
+
+ def DisplayTemplate(self, name, template_values, use_cache=False):
+ """Replies to a http request with a template.
+
+ 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
+ pages!
+ """
+ self.response.headers['Cache-Control'] = 'no-cache, private, max-age=0'
+ buff = None
+ if use_cache:
+ buff = memcache.get(name)
+ if not buff:
+ path = os.path.join(os.path.dirname(__file__), 'templates/%s' % name)
+ buff = template.render(path, template_values)
+ if use_cache:
+ memcache.add(name, buff, 1)
+ self.response.out.write(buff)
+
+
+def bootstrap():
+ app_name = os.environ['APPLICATION_ID']
+ BasePage.app_name = app_name

Powered by Google App Engine
This is Rietveld 408576698