blob: 2b0057015ea2ee64935a6c4fd2b87e8e46654906 [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
dhermes@google.com62c8b7d2012-12-19 14:44:38 -080015import webapp2
16from webapp2_extras import jinja2
17
John Asmuth864311d2014-04-24 15:46:08 -040018from googleapiclient.discovery import build
Johan Euphrosineda68b332011-06-17 19:56:45 +020019from oauth2client.appengine import OAuth2Decorator
dhermes@google.com62c8b7d2012-12-19 14:44:38 -080020
Johan Euphrosineda68b332011-06-17 19:56:45 +020021import settings
22
23decorator = OAuth2Decorator(client_id=settings.CLIENT_ID,
24 client_secret=settings.CLIENT_SECRET,
dhermes@google.com62c8b7d2012-12-19 14:44:38 -080025 scope=settings.SCOPE)
26service = build('tasks', 'v1')
Johan Euphrosineda68b332011-06-17 19:56:45 +020027
28
dhermes@google.com62c8b7d2012-12-19 14:44:38 -080029class 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 Euphrosineda68b332011-06-17 19:56:45 +020035
36 @decorator.oauth_aware
37 def get(self):
38 if decorator.has_credentials():
dhermes@google.com62c8b7d2012-12-19 14:44:38 -080039 result = service.tasks().list(tasklist='@default').execute(
40 http=decorator.http())
Johan Euphrosineda68b332011-06-17 19:56:45 +020041 tasks = result.get('items', [])
42 for task in tasks:
43 task['title_short'] = truncate(task['title'], 26)
dhermes@google.com62c8b7d2012-12-19 14:44:38 -080044 self.render_response('index.html', tasks=tasks)
Johan Euphrosineda68b332011-06-17 19:56:45 +020045 else:
46 url = decorator.authorize_url()
dhermes@google.com62c8b7d2012-12-19 14:44:38 -080047 self.render_response('index.html', tasks=[], authorize_url=url)
Johan Euphrosineda68b332011-06-17 19:56:45 +020048
49
50def truncate(s, l):
51 return s[:l] + '...' if len(s) > l else s
52
dhermes@google.com62c8b7d2012-12-19 14:44:38 -080053
54application = webapp2.WSGIApplication([
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040055 ('/', MainHandler),
56 (decorator.callback_path, decorator.callback_handler()),
57 ], debug=True)