| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 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 from alerts import AlertsHandler | 5 from alerts import AlertsHandler |
| 6 from alerts import AlertsJSON | 6 from alerts import AlertsJSON |
| 7 from alerts import increment_monarch |
| 7 from internal_alerts import InternalAlertsHandler | 8 from internal_alerts import InternalAlertsHandler |
| 8 import json | 9 import json |
| 9 import webapp2 | 10 import webapp2 |
| 10 | 11 |
| 11 from google.appengine.api import users | 12 from google.appengine.api import users |
| 12 from google.appengine.datastore import datastore_query | 13 from google.appengine.datastore import datastore_query |
| 13 from google.appengine.ext import ndb | 14 from google.appengine.ext import ndb |
| 14 | 15 |
| 15 | 16 |
| 16 class AlertsHistory(webapp2.RequestHandler): | 17 class AlertsHistory(webapp2.RequestHandler): |
| 17 MAX_LIMIT_PER_PAGE = 100 | 18 MAX_LIMIT_PER_PAGE = 100 |
| 18 PUBLIC_TYPE = AlertsHandler.ALERT_TYPE | 19 PUBLIC_TYPE = AlertsHandler.ALERT_TYPE |
| 19 PRIVATE_TYPE = InternalAlertsHandler.ALERT_TYPE | |
| 20 | 20 |
| 21 def get_entry(self, query, key): | 21 def get_entry(self, query, key): |
| 22 try: | 22 try: |
| 23 key = int(key) | 23 key = int(key) |
| 24 except ValueError: | 24 except ValueError: |
| 25 self.response.set_status(400, 'Invalid key format') | 25 self.response.set_status(400, 'Invalid key format') |
| 26 self.abort(400) | 26 self.abort(400) |
| 27 | 27 |
| 28 ndb_key = ndb.Key(AlertsJSON, key) | 28 ndb_key = ndb.Key(AlertsJSON, key) |
| 29 result = query.filter(AlertsJSON.key == ndb_key).get() | 29 result = query.filter(AlertsJSON.key == ndb_key).get() |
| (...skipping 22 matching lines...) Expand all Loading... |
| 52 else: | 52 else: |
| 53 alerts, next_cursor, has_more = query.fetch_page(limit) | 53 alerts, next_cursor, has_more = query.fetch_page(limit) |
| 54 | 54 |
| 55 return { | 55 return { |
| 56 'has_more': has_more, | 56 'has_more': has_more, |
| 57 'cursor': next_cursor.urlsafe() if next_cursor else '', | 57 'cursor': next_cursor.urlsafe() if next_cursor else '', |
| 58 'history': [alert.key.integer_id() for alert in alerts] | 58 'history': [alert.key.integer_id() for alert in alerts] |
| 59 } | 59 } |
| 60 | 60 |
| 61 def get(self, key=None): | 61 def get(self, key=None): |
| 62 increment_monarch('alerts-history') |
| 62 query = AlertsJSON.query().order(-AlertsJSON.date) | 63 query = AlertsJSON.query().order(-AlertsJSON.date) |
| 63 result_json = {} | 64 result_json = {} |
| 64 | 65 |
| 65 user = users.get_current_user() | 66 user = users.get_current_user() |
| 66 result_json['login-url'] = users.create_login_url(self.request.uri) | 67 result_json['login-url'] = users.create_login_url(self.request.uri) |
| 67 | 68 |
| 68 # Return only public alerts for non-internal users. | 69 # Return only public alerts for non-internal users. |
| 69 if not user or not user.email().endswith('@google.com'): | 70 if not user or not user.email().endswith('@google.com'): |
| 70 query = query.filter(AlertsJSON.type == self.PUBLIC_TYPE) | 71 query = query.filter(AlertsJSON.type == self.PUBLIC_TYPE) |
| 71 else: | |
| 72 query = query.filter(AlertsJSON.type.IN([self.PUBLIC_TYPE, | |
| 73 self.PRIVATE_TYPE])) | |
| 74 | 72 |
| 75 if key: | 73 if key: |
| 76 result_json.update(self.get_entry(query, key)) | 74 result_json.update(self.get_entry(query, key)) |
| 77 else: | 75 else: |
| 78 result_json.update(self.get_list(query)) | 76 result_json.update(self.get_list(query)) |
| 79 | 77 |
| 80 self.response.headers['Content-Type'] = 'application/json' | 78 self.response.headers['Content-Type'] = 'application/json' |
| 81 self.response.headers['Access-Control-Allow-Origin'] = '*' | 79 self.response.headers['Access-Control-Allow-Origin'] = '*' |
| 82 | 80 |
| 83 self.response.out.write(json.dumps(result_json)) | 81 self.response.out.write(json.dumps(result_json)) |
| 84 | 82 |
| 85 | 83 |
| 86 app = webapp2.WSGIApplication([ | 84 app = webapp2.WSGIApplication([ |
| 87 ('/alerts-history', AlertsHistory), | 85 ('/alerts-history', AlertsHistory), |
| 88 ('/alerts-history/(.*)', AlertsHistory) | 86 ('/alerts-history/(.*)', AlertsHistory) |
| 89 ]) | 87 ]) |
| OLD | NEW |