-
Notifications
You must be signed in to change notification settings - Fork 1
/
views.py
50 lines (39 loc) · 1.49 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from google.appengine.ext import db
from lib import dictify
from models import week_key, Song
import datetime
import jinja2
import json
import logging
import os
import rssloader
import webapp2 as webapp
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class MainPage(webapp.RequestHandler):
def get(self):
template = jinja_environment.get_template('templates/index.html')
self.response.out.write(template.render())
########################## JSON #########################
class Pop40Page(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
#self.response.out.write(json.dumps(rssloader.rssdata))
week_date = '2012-06-16'
# read
songs = Song.gql("WHERE ANCESTOR IS :1 "
"ORDER BY rank DESC LIMIT 10",
week_key(week_date))
res = {}
res[week_date] = [db.to_dict(song) for song in songs]
#res[week_date] = [dictify.to_dict(song) for song in songs]
self.response.out.write(json.dumps(res, default=dthandler))
# provide handler for converting datetime objects to json
# from http://stackoverflow.com/a/2680060/856897
def dthandler(obj):
if hasattr(obj, 'isoformat'):
return obj.isoformat()
else:
raise (TypeError,
'Object of type %s with value of %s is not JSON serializable'
% (type(obj), repr(obj)))