1# Copyright 2014 Google Inc. All Rights Reserved.
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
15import webapp2
16from webapp2_extras import jinja2
17
18from googleapiclient.discovery import build
19from oauth2client.contrib.appengine import OAuth2Decorator
20
21import settings
22
23decorator = OAuth2Decorator(client_id=settings.CLIENT_ID,
24                            client_secret=settings.CLIENT_SECRET,
25                            scope=settings.SCOPE)
26service = build('tasks', 'v1')
27
28
29class 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)
35
36  @decorator.oauth_aware
37  def get(self):
38    if decorator.has_credentials():
39      result = service.tasks().list(tasklist='@default').execute(
40          http=decorator.http())
41      tasks = result.get('items', [])
42      for task in tasks:
43        task['title_short'] = truncate(task['title'], 26)
44      self.render_response('index.html', tasks=tasks)
45    else:
46      url = decorator.authorize_url()
47      self.render_response('index.html', tasks=[], authorize_url=url)
48
49
50def truncate(s, l):
51  return s[:l] + '...' if len(s) > l else s
52
53
54application = webapp2.WSGIApplication([
55    ('/', MainHandler),
56    (decorator.callback_path, decorator.callback_handler()),
57    ], debug=True)
58