Craig Citro | 751b7fb | 2014-09-23 11:20:38 -0700 | [diff] [blame] | 1 | # Copyright 2014 Google Inc. All Rights Reserved. |
Johan Euphrosine | da68b33 | 2011-06-17 19:56:45 +0200 | [diff] [blame] | 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 | |
dhermes@google.com | 62c8b7d | 2012-12-19 14:44:38 -0800 | [diff] [blame] | 15 | import webapp2 |
| 16 | from webapp2_extras import jinja2 |
| 17 | |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 18 | from googleapiclient.discovery import build |
Adrian Carpenter | 2122d3c | 2016-02-16 16:12:22 -0600 | [diff] [blame] | 19 | from oauth2client.contrib.appengine import OAuth2Decorator |
dhermes@google.com | 62c8b7d | 2012-12-19 14:44:38 -0800 | [diff] [blame] | 20 | |
Johan Euphrosine | da68b33 | 2011-06-17 19:56:45 +0200 | [diff] [blame] | 21 | import settings |
| 22 | |
| 23 | decorator = OAuth2Decorator(client_id=settings.CLIENT_ID, |
| 24 | client_secret=settings.CLIENT_SECRET, |
dhermes@google.com | 62c8b7d | 2012-12-19 14:44:38 -0800 | [diff] [blame] | 25 | scope=settings.SCOPE) |
| 26 | service = build('tasks', 'v1') |
Johan Euphrosine | da68b33 | 2011-06-17 19:56:45 +0200 | [diff] [blame] | 27 | |
| 28 | |
dhermes@google.com | 62c8b7d | 2012-12-19 14:44:38 -0800 | [diff] [blame] | 29 | class MainHandler(webapp2.RequestHandler): |
| 30 | |
| 31 | def render_response(self, template, **context): |
| 32 | renderer = jinja2.get_jinja2(app=self.app) |
| 33 | rendered_value = renderer.render_template(template, **context) |
| 34 | self.response.write(rendered_value) |
Johan Euphrosine | da68b33 | 2011-06-17 19:56:45 +0200 | [diff] [blame] | 35 | |
| 36 | @decorator.oauth_aware |
| 37 | def get(self): |
| 38 | if decorator.has_credentials(): |
dhermes@google.com | 62c8b7d | 2012-12-19 14:44:38 -0800 | [diff] [blame] | 39 | result = service.tasks().list(tasklist='@default').execute( |
| 40 | http=decorator.http()) |
Johan Euphrosine | da68b33 | 2011-06-17 19:56:45 +0200 | [diff] [blame] | 41 | tasks = result.get('items', []) |
| 42 | for task in tasks: |
| 43 | task['title_short'] = truncate(task['title'], 26) |
dhermes@google.com | 62c8b7d | 2012-12-19 14:44:38 -0800 | [diff] [blame] | 44 | self.render_response('index.html', tasks=tasks) |
Johan Euphrosine | da68b33 | 2011-06-17 19:56:45 +0200 | [diff] [blame] | 45 | else: |
| 46 | url = decorator.authorize_url() |
dhermes@google.com | 62c8b7d | 2012-12-19 14:44:38 -0800 | [diff] [blame] | 47 | self.render_response('index.html', tasks=[], authorize_url=url) |
Johan Euphrosine | da68b33 | 2011-06-17 19:56:45 +0200 | [diff] [blame] | 48 | |
| 49 | |
| 50 | def truncate(s, l): |
| 51 | return s[:l] + '...' if len(s) > l else s |
| 52 | |
dhermes@google.com | 62c8b7d | 2012-12-19 14:44:38 -0800 | [diff] [blame] | 53 | |
| 54 | application = webapp2.WSGIApplication([ |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 55 | ('/', MainHandler), |
| 56 | (decorator.callback_path, decorator.callback_handler()), |
| 57 | ], debug=True) |