OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import calendar | |
6 import datetime | |
7 import json | |
8 import webapp2 | |
9 | |
10 from google.appengine.api import memcache | |
11 | |
12 | |
13 class DateTimeEncoder(json.JSONEncoder): | |
14 def default(self, obj): | |
15 if isinstance(obj, datetime.datetime): | |
16 return calendar.timegm(obj.timetuple()) | |
17 # Let the base class default method raise the TypeError | |
18 return json.JSONEncoder.default(self, obj) | |
19 | |
20 | |
21 class AlertsHandler(webapp2.RequestHandler): | |
22 MEMCACHE_ALERTS_KEY = 'alerts' | |
23 | |
24 def get(self): | |
25 self.response.headers.add_header('Access-Control-Allow-Origin', '*') | |
26 self.response.headers['Content-Type'] = 'application/json' | |
27 alerts = memcache.get(AlertsHandler.MEMCACHE_ALERTS_KEY) | |
28 if not alerts: | |
29 return | |
30 self.response.write(json.dumps(alerts, cls=DateTimeEncoder, indent=1)) | |
31 | |
32 def post(self): | |
33 try: | |
34 alerts = json.loads(self.request.get('content')) | |
35 except ValueError: | |
36 self.response.set_status(400, 'content field was not JSON') | |
37 return | |
38 alerts.update({ | |
39 'date': datetime.datetime.utcnow(), | |
40 'alerts': alerts['alerts']}) | |
ojan
2014/08/13 06:13:24
Bikeshed nit: I'd add a newline before the }
| |
41 memcache.set(AlertsHandler.MEMCACHE_ALERTS_KEY, alerts) | |
42 | |
43 | |
44 app = webapp2.WSGIApplication([ | |
45 ('/alerts', AlertsHandler)]) | |
ojan
2014/08/13 06:13:25
Ditto nit: newline before the ]
| |
OLD | NEW |