Index: Tools/GardeningServer/alerts.py |
diff --git a/Tools/GardeningServer/alerts.py b/Tools/GardeningServer/alerts.py |
index 071dc7da47c6cf9f5e7f6756e47b44c06d733c75..05eb7be534765d872c393bacf521df1f8e050074 100644 |
--- a/Tools/GardeningServer/alerts.py |
+++ b/Tools/GardeningServer/alerts.py |
@@ -10,6 +10,8 @@ import webapp2 |
import zlib |
from google.appengine.api import memcache |
+from google.appengine.api import users |
+from google.appengine.datastore import datastore_query |
from google.appengine.ext import ndb |
LOGGER = logging.getLogger(__name__) |
@@ -24,12 +26,13 @@ class DateTimeEncoder(json.JSONEncoder): |
class AlertsJSON(ndb.Model): |
+ type = ndb.StringProperty() |
json = ndb.BlobProperty(compressed=True) |
date = ndb.DateTimeProperty(auto_now_add=True) |
class AlertsHandler(webapp2.RequestHandler): |
- MEMCACHE_ALERTS_KEY = 'alerts' |
+ ALERTS_TYPE = 'alerts' |
# Has no 'response' member. |
# pylint: disable=E1101 |
@@ -55,10 +58,11 @@ class AlertsHandler(webapp2.RequestHandler): |
self.send_json_data(uncompressed) |
def get(self): |
- self.get_from_memcache(AlertsHandler.MEMCACHE_ALERTS_KEY) |
+ self.get_from_memcache(AlertsHandler.ALERTS_TYPE) |
- def save_alerts_to_history(self, alerts): |
- last_entry = AlertsJSON.query().order(-AlertsJSON.date).get() |
+ def post_to_history(self, alerts_type, alerts): |
+ last_query = AlertsJSON.query().filter(AlertsJSON.type == alerts_type) |
+ last_entry = last_query.order(-AlertsJSON.date).get() |
last_alerts = json.loads(last_entry.json) if last_entry else {} |
# Only changes to the fields with 'alerts' in the name should cause a |
@@ -71,7 +75,9 @@ class AlertsHandler(webapp2.RequestHandler): |
return filtered_json |
if alert_fields(last_alerts) != alert_fields(alerts): |
- new_entry = AlertsJSON(json=self.generate_json_dump(alerts)) |
+ new_entry = AlertsJSON( |
+ json=self.generate_json_dump(alerts), |
+ type=alerts_type) |
new_entry.put() |
# Has no 'response' member. |
@@ -95,16 +101,57 @@ class AlertsHandler(webapp2.RequestHandler): |
return alerts |
- def update_alerts(self, memcache_key): |
+ def update_alerts(self, alerts_type): |
alerts = self.parse_alerts(self.request.get('content')) |
if alerts: |
- self.post_to_memcache(memcache_key, alerts) |
- self.save_alerts_to_history(alerts) |
+ self.post_to_memcache(alerts_type, alerts) |
+ self.post_to_history(alerts_type, alerts) |
def post(self): |
- self.update_alerts(AlertsHandler.MEMCACHE_ALERTS_KEY) |
+ self.update_alerts(AlertsHandler.ALERTS_TYPE) |
+ |
+ |
+class AlertsHistory(webapp2.RequestHandler): |
+ MAX_LIMIT_PER_PAGE = 50 |
+ |
+ def get(self): |
+ alerts_query = AlertsJSON.query().order(-AlertsJSON.date) |
+ result_json = {} |
+ |
+ user = users.get_current_user() |
+ if not user: |
+ result_json['redirect-url'] = users.create_login_url( |
+ self.request.uri) |
+ |
+ # Return only public alerts for non-internal users. |
+ if not user or not user.email().endswith('@google.com'): |
+ alerts_query = alerts_query.filter( |
+ AlertsJSON.type == AlertsHandler.ALERTS_TYPE) |
+ |
+ cursor = self.request.get('cursor') |
+ if cursor: |
+ cursor = datastore_query.Cursor(urlsafe=cursor) |
+ |
+ limit = int(self.request.get('limit', self.MAX_LIMIT_PER_PAGE)) |
+ limit = min(self.MAX_LIMIT_PER_PAGE, limit) |
+ |
+ if cursor: |
+ alerts, next_cursor, has_more = alerts_query.fetch_page( |
+ limit, start_cursor=cursor) |
+ else: |
+ alerts, next_cursor, has_more = alerts_query.fetch_page(limit) |
+ |
+ result_json.update({ |
+ 'has_more': has_more, |
+ 'cursor': next_cursor.urlsafe() if next_cursor else '', |
+ 'history': [json.loads(alert.json) for alert in alerts] |
+ }) |
+ |
+ self.response.headers['Content-Type'] = 'application/json' |
+ self.response.out.write(json.dumps(result_json)) |
app = webapp2.WSGIApplication([ |
- ('/alerts', AlertsHandler) |
+ ('/alerts', AlertsHandler), |
+ ('/alerts-history', AlertsHistory) |
]) |