From 5f8e4fcc42bd010a22c4ccb895a4dd2e3c0e4b29 Mon Sep 17 00:00:00 2001 From: Joe Gregorio Date: Thu, 16 May 2013 15:56:26 -0400 Subject: [PATCH] Update the appengine sample to use the Python 2.7 runtime. Reviewed in https://codereview.appspot.com/9394043. --- samples/appengine/app.yaml | 12 ++++++--- samples/appengine/main.py | 55 +++++++++++++++++++------------------- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/samples/appengine/app.yaml b/samples/appengine/app.yaml index 1361642..e408ea1 100644 --- a/samples/appengine/app.yaml +++ b/samples/appengine/app.yaml @@ -1,9 +1,15 @@ -application: jcg-testing-01 +application: app-id-goes-here version: 2 -runtime: python +runtime: python27 +threadsafe: true api_version: 1 handlers: - url: .* - script: main.py + script: main.app +libraries: +- name: jinja2 + version: "latest" +- name: webapp2 + version: "latest" diff --git a/samples/appengine/main.py b/samples/appengine/main.py index f649bfb..0255cd3 100644 --- a/samples/appengine/main.py +++ b/samples/appengine/main.py @@ -30,14 +30,19 @@ import logging import os import pickle -from apiclient.discovery import build -from oauth2client.appengine import oauth2decorator_from_clientsecrets -from oauth2client.client import AccessTokenRefreshError +from apiclient import discovery +from oauth2client import appengine +from oauth2client import client from google.appengine.api import memcache -from google.appengine.ext import webapp -from google.appengine.ext.webapp import template -from google.appengine.ext.webapp.util import run_wsgi_app +import webapp2 +import jinja2 + + +JINJA_ENVIRONMENT = jinja2.Environment( + loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), + autoescape=True, + extensions=['jinja2.ext.autoescape']) # CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this # application, including client_id and client_secret, which are found @@ -63,25 +68,25 @@ href="https://code.google.com/apis/console">APIs Console. http = httplib2.Http(memcache) -service = build("plus", "v1", http=http) -decorator = oauth2decorator_from_clientsecrets( +service = discovery.build("plus", "v1", http=http) +decorator = appengine.oauth2decorator_from_clientsecrets( CLIENT_SECRETS, scope='https://www.googleapis.com/auth/plus.me', message=MISSING_CLIENT_SECRETS_MESSAGE) -class MainHandler(webapp.RequestHandler): +class MainHandler(webapp2.RequestHandler): @decorator.oauth_aware def get(self): - path = os.path.join(os.path.dirname(__file__), 'grant.html') variables = { 'url': decorator.authorize_url(), 'has_credentials': decorator.has_credentials() } - self.response.out.write(template.render(path, variables)) + template = JINJA_ENVIRONMENT.get_template('grant.html') + self.response.write(template.render(variables)) -class AboutHandler(webapp.RequestHandler): +class AboutHandler(webapp2.RequestHandler): @decorator.oauth_required def get(self): @@ -90,22 +95,16 @@ class AboutHandler(webapp.RequestHandler): user = service.people().get(userId='me').execute(http=http) text = 'Hello, %s!' % user['displayName'] - path = os.path.join(os.path.dirname(__file__), 'welcome.html') - self.response.out.write(template.render(path, {'text': text })) - except AccessTokenRefreshError: + template = JINJA_ENVIRONMENT.get_template('welcome.html') + self.response.write(template.render({'text': text })) + except client.AccessTokenRefreshError: self.redirect('/') -def main(): - application = webapp.WSGIApplication( - [ - ('/', MainHandler), - ('/about', AboutHandler), - (decorator.callback_path, decorator.callback_handler()), - ], - debug=True) - run_wsgi_app(application) - - -if __name__ == '__main__': - main() +app = webapp2.WSGIApplication( + [ + ('/', MainHandler), + ('/about', AboutHandler), + (decorator.callback_path, decorator.callback_handler()), + ], + debug=True)