blob: 54260bdef919e40ffa53ebb7eabeb1776df0fc5a [file] [log] [blame]
Johan Euphrosineda68b332011-06-17 19:56:45 +02001# Copyright (C) 2011 Google Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from google.appengine.dist import use_library
16use_library('django', '1.2')
17from google.appengine.ext import webapp
18from google.appengine.ext.webapp import template
19from google.appengine.ext.webapp.util import run_wsgi_app
20from apiclient.discovery import build
21import httplib2
22from oauth2client.appengine import OAuth2Decorator
23import settings
24
25decorator = OAuth2Decorator(client_id=settings.CLIENT_ID,
26 client_secret=settings.CLIENT_SECRET,
27 scope=settings.SCOPE,
28 user_agent='mytasks')
29
30
31class MainHandler(webapp.RequestHandler):
32
33 @decorator.oauth_aware
34 def get(self):
35 if decorator.has_credentials():
36 service = build('tasks', 'v1', http=decorator.http())
37 result = service.tasks().list(tasklist='@default').execute()
38 tasks = result.get('items', [])
39 for task in tasks:
40 task['title_short'] = truncate(task['title'], 26)
41 self.response.out.write(template.render('templates/index.html',
42 {'tasks': tasks}))
43 else:
44 url = decorator.authorize_url()
45 self.response.out.write(template.render('templates/index.html',
46 {'tasks': [],
47 'authorize_url': url}))
48
49
50def truncate(s, l):
51 return s[:l] + '...' if len(s) > l else s
52
53application = webapp.WSGIApplication([('/', MainHandler)], debug=True)
54
55
56def main():
57 run_wsgi_app(application)