Johan Euphrosine | da68b33 | 2011-06-17 19:56:45 +0200 | [diff] [blame] | 1 | # 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 | |
| 15 | from google.appengine.dist import use_library |
| 16 | use_library('django', '1.2') |
| 17 | from google.appengine.ext import webapp |
| 18 | from google.appengine.ext.webapp import template |
| 19 | from google.appengine.ext.webapp.util import run_wsgi_app |
| 20 | from apiclient.discovery import build |
| 21 | import httplib2 |
| 22 | from oauth2client.appengine import OAuth2Decorator |
| 23 | import settings |
| 24 | |
| 25 | decorator = OAuth2Decorator(client_id=settings.CLIENT_ID, |
| 26 | client_secret=settings.CLIENT_SECRET, |
| 27 | scope=settings.SCOPE, |
| 28 | user_agent='mytasks') |
| 29 | |
| 30 | |
| 31 | class 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 | |
| 50 | def truncate(s, l): |
| 51 | return s[:l] + '...' if len(s) > l else s |
| 52 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame^] | 53 | application = webapp.WSGIApplication([ |
| 54 | ('/', MainHandler), |
| 55 | (decorator.callback_path, decorator.callback_handler()), |
| 56 | ], debug=True) |
Johan Euphrosine | da68b33 | 2011-06-17 19:56:45 +0200 | [diff] [blame] | 57 | |
| 58 | |
| 59 | def main(): |
| 60 | run_wsgi_app(application) |