This repository has been archived by the owner on Aug 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
handler_json.py
65 lines (48 loc) · 1.72 KB
/
handler_json.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from django.utils import simplejson
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import fix_path
import models
import datetime
class BaseHandler(webapp.RequestHandler):
# def render_to_response(self, template_name, template_vals=None, theme=None):
# if not template_vals:
# template_vals = {}
# template_vals.update({
# 'path': self.request.path,
# 'handler_class': self.__class__.__name__,
# 'is_admin': True,
# })
# template_name = os.path.join("admin", template_name)
# self.response.out.write(
# utils.render_template(template_name, template_vals, theme))
pass
class JSONHandler(BaseHandler):
def get(self):
posts = models.BlogPost.all().order('-published').fetch(None) # all posts
data = []
for p in posts:
if p.published != datetime.datetime.max:
data.append({
'title': p.title,
'author_id': p.author_id,
'description': unicode(p.description),
'updated': unicode(p.updated),
'published': unicode(p.published),
'path': p.path,
})
# Add CORS and Chrome Frame to response.
self.response.headers.add_header('Access-Control-Allow-Origin', '*')
self.response.headers.add_header('X-UA-Compatible', 'IE=Edge,chrome=1')
self.response.headers['Content-Type'] = 'application/json'
#return self.response.out.write(
# simplejson.dumps([entity.to_dict() for entity in posts]))
return self.response.out.write(simplejson.dumps(data))
application = webapp.WSGIApplication([
('/.*', JSONHandler),
])
def main():
fix_path.fix_sys_path()
run_wsgi_app(application)
if __name__ == '__main__':
main()