Updating tasks App Engine sample.

Reviewed in https://codereview.appspot.com/6973047/
This commit is contained in:
dhermes@google.com
2012-12-19 14:44:38 -08:00
parent 1cea2a7663
commit 62c8b7d2d6
2 changed files with 25 additions and 23 deletions

View File

@@ -1,11 +1,16 @@
application: mytasks application: mytasks
version: 1 version: 1
runtime: python runtime: python27
api_version: 1 api_version: 1
threadsafe: true
handlers: handlers:
- url: /css - url: /css
static_dir: css static_dir: css
- url: .* - url: .*
script: main.py script: main.application
libraries:
- name: jinja2
version: latest

View File

@@ -12,49 +12,46 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from google.appengine.dist import use_library import webapp2
use_library('django', '1.2') from webapp2_extras import jinja2
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from apiclient.discovery import build from apiclient.discovery import build
import httplib2
from oauth2client.appengine import OAuth2Decorator from oauth2client.appengine import OAuth2Decorator
import settings import settings
decorator = OAuth2Decorator(client_id=settings.CLIENT_ID, decorator = OAuth2Decorator(client_id=settings.CLIENT_ID,
client_secret=settings.CLIENT_SECRET, client_secret=settings.CLIENT_SECRET,
scope=settings.SCOPE, scope=settings.SCOPE)
user_agent='mytasks') service = build('tasks', 'v1')
class MainHandler(webapp.RequestHandler): class MainHandler(webapp2.RequestHandler):
def render_response(self, template, **context):
renderer = jinja2.get_jinja2(app=self.app)
rendered_value = renderer.render_template(template, **context)
self.response.write(rendered_value)
@decorator.oauth_aware @decorator.oauth_aware
def get(self): def get(self):
if decorator.has_credentials(): if decorator.has_credentials():
service = build('tasks', 'v1', http=decorator.http()) result = service.tasks().list(tasklist='@default').execute(
result = service.tasks().list(tasklist='@default').execute() http=decorator.http())
tasks = result.get('items', []) tasks = result.get('items', [])
for task in tasks: for task in tasks:
task['title_short'] = truncate(task['title'], 26) task['title_short'] = truncate(task['title'], 26)
self.response.out.write(template.render('templates/index.html', self.render_response('index.html', tasks=tasks)
{'tasks': tasks}))
else: else:
url = decorator.authorize_url() url = decorator.authorize_url()
self.response.out.write(template.render('templates/index.html', self.render_response('index.html', tasks=[], authorize_url=url)
{'tasks': [],
'authorize_url': url}))
def truncate(s, l): def truncate(s, l):
return s[:l] + '...' if len(s) > l else s return s[:l] + '...' if len(s) > l else s
application = webapp.WSGIApplication([
application = webapp2.WSGIApplication([
('/', MainHandler), ('/', MainHandler),
(decorator.callback_path, decorator.callback_handler()), (decorator.callback_path, decorator.callback_handler()),
], debug=True) ], debug=True)
def main():
run_wsgi_app(application)