|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 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 |
| 15 | +import webapp2 |
| 16 | +from webapp2_extras import jinja2 |
| 17 | + |
20 | 18 | from apiclient.discovery import build |
21 | | -import httplib2 |
22 | 19 | from oauth2client.appengine import OAuth2Decorator |
| 20 | + |
23 | 21 | import settings |
24 | 22 |
|
25 | 23 | decorator = OAuth2Decorator(client_id=settings.CLIENT_ID, |
26 | 24 | client_secret=settings.CLIENT_SECRET, |
27 | | - scope=settings.SCOPE, |
28 | | - user_agent='mytasks') |
| 25 | + scope=settings.SCOPE) |
| 26 | +service = build('tasks', 'v1') |
| 27 | + |
29 | 28 |
|
| 29 | +class MainHandler(webapp2.RequestHandler): |
30 | 30 |
|
31 | | -class MainHandler(webapp.RequestHandler): |
| 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) |
32 | 35 |
|
33 | 36 | @decorator.oauth_aware |
34 | 37 | def get(self): |
35 | 38 | if decorator.has_credentials(): |
36 | | - service = build('tasks', 'v1', http=decorator.http()) |
37 | | - result = service.tasks().list(tasklist='@default').execute() |
| 39 | + result = service.tasks().list(tasklist='@default').execute( |
| 40 | + http=decorator.http()) |
38 | 41 | tasks = result.get('items', []) |
39 | 42 | for task in tasks: |
40 | 43 | task['title_short'] = truncate(task['title'], 26) |
41 | | - self.response.out.write(template.render('templates/index.html', |
42 | | - {'tasks': tasks})) |
| 44 | + self.render_response('index.html', tasks=tasks) |
43 | 45 | else: |
44 | 46 | url = decorator.authorize_url() |
45 | | - self.response.out.write(template.render('templates/index.html', |
46 | | - {'tasks': [], |
47 | | - 'authorize_url': url})) |
| 47 | + self.render_response('index.html', tasks=[], authorize_url=url) |
48 | 48 |
|
49 | 49 |
|
50 | 50 | def truncate(s, l): |
51 | 51 | return s[:l] + '...' if len(s) > l else s |
52 | 52 |
|
53 | | -application = webapp.WSGIApplication([ |
| 53 | + |
| 54 | +application = webapp2.WSGIApplication([ |
54 | 55 | ('/', MainHandler), |
55 | 56 | (decorator.callback_path, decorator.callback_handler()), |
56 | 57 | ], debug=True) |
57 | | - |
58 | | - |
59 | | -def main(): |
60 | | - run_wsgi_app(application) |
0 commit comments