OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import calendar | 5 import calendar |
6 import datetime | 6 import datetime |
7 import json | 7 import json |
8 import logging | 8 import logging |
9 import webapp2 | 9 import webapp2 |
10 import zlib | 10 import zlib |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 alerts = self.parse_alerts(self.request.get('content')) | 105 alerts = self.parse_alerts(self.request.get('content')) |
106 if alerts: | 106 if alerts: |
107 self.post_to_memcache(alerts_type, alerts) | 107 self.post_to_memcache(alerts_type, alerts) |
108 self.post_to_history(alerts_type, alerts) | 108 self.post_to_history(alerts_type, alerts) |
109 | 109 |
110 def post(self): | 110 def post(self): |
111 self.update_alerts(AlertsHandler.ALERTS_TYPE) | 111 self.update_alerts(AlertsHandler.ALERTS_TYPE) |
112 | 112 |
113 | 113 |
114 class AlertsHistory(webapp2.RequestHandler): | 114 class AlertsHistory(webapp2.RequestHandler): |
115 MAX_LIMIT_PER_PAGE = 5 | 115 MAX_LIMIT_PER_PAGE = 100 |
116 | 116 |
117 def get(self): | 117 def get_entry(self, query, key): |
118 alerts_query = AlertsJSON.query().order(-AlertsJSON.date) | 118 try: |
119 result_json = {} | 119 key = int(key) |
| 120 except ValueError: |
| 121 self.response.set_status(400, 'Invalid key format') |
| 122 return {} |
120 | 123 |
121 user = users.get_current_user() | 124 ndb_key = ndb.Key(AlertsJSON, key) |
122 if not user: | 125 result = query.filter(AlertsJSON.key == ndb_key).get() |
123 result_json['redirect-url'] = users.create_login_url( | 126 if result: |
124 self.request.uri) | 127 return json.loads(result.json) |
| 128 else: |
| 129 self.response.set_status(404, 'Failed to find key %s' % key) |
| 130 return {} |
125 | 131 |
126 # Return only public alerts for non-internal users. | 132 def get_list(self, query): |
127 if not user or not user.email().endswith('@google.com'): | |
128 alerts_query = alerts_query.filter( | |
129 AlertsJSON.type == AlertsHandler.ALERTS_TYPE) | |
130 | |
131 cursor = self.request.get('cursor') | 133 cursor = self.request.get('cursor') |
132 if cursor: | 134 if cursor: |
133 cursor = datastore_query.Cursor(urlsafe=cursor) | 135 cursor = datastore_query.Cursor(urlsafe=cursor) |
134 | 136 |
135 limit = int(self.request.get('limit', self.MAX_LIMIT_PER_PAGE)) | 137 limit = int(self.request.get('limit', self.MAX_LIMIT_PER_PAGE)) |
136 limit = min(self.MAX_LIMIT_PER_PAGE, limit) | 138 limit = min(self.MAX_LIMIT_PER_PAGE, limit) |
137 | 139 |
138 if cursor: | 140 if cursor: |
139 alerts, next_cursor, has_more = alerts_query.fetch_page( | 141 alerts, next_cursor, has_more = query.fetch_page(limit, |
140 limit, start_cursor=cursor) | 142 start_cursor=cursor
) |
141 else: | 143 else: |
142 alerts, next_cursor, has_more = alerts_query.fetch_page(limit) | 144 alerts, next_cursor, has_more = query.fetch_page(limit) |
143 | 145 |
144 result_json.update({ | 146 return { |
145 'has_more': has_more, | 147 'has_more': has_more, |
146 'cursor': next_cursor.urlsafe() if next_cursor else '', | 148 'cursor': next_cursor.urlsafe() if next_cursor else '', |
147 'history': [json.loads(alert.json) for alert in alerts] | 149 'history': [alert.key.integer_id() for alert in alerts] |
148 }) | 150 } |
| 151 |
| 152 def get(self, key=None): |
| 153 query = AlertsJSON.query().order(-AlertsJSON.date) |
| 154 result_json = {} |
| 155 |
| 156 user = users.get_current_user() |
| 157 result_json['login-url'] = users.create_login_url(self.request.uri) |
| 158 |
| 159 # Return only public alerts for non-internal users. |
| 160 if not user or not user.email().endswith('@google.com'): |
| 161 query = query.filter(AlertsJSON.type == AlertsHandler.ALERTS_TYPE) |
| 162 |
| 163 if key: |
| 164 result_json.update(self.get_entry(query, key)) |
| 165 else: |
| 166 result_json.update(self.get_list(query)) |
149 | 167 |
150 self.response.headers['Content-Type'] = 'application/json' | 168 self.response.headers['Content-Type'] = 'application/json' |
151 self.response.out.write(json.dumps(result_json)) | 169 self.response.out.write(json.dumps(result_json)) |
152 | 170 |
153 | 171 |
154 app = webapp2.WSGIApplication([ | 172 app = webapp2.WSGIApplication([ |
155 ('/alerts', AlertsHandler), | 173 ('/alerts', AlertsHandler), |
156 ('/alerts-history', AlertsHistory) | 174 ('/alerts-history', AlertsHistory), |
| 175 ('/alerts-history/(.*)', AlertsHistory), |
157 ]) | 176 ]) |
OLD | NEW |