def get(self, key=None): self.remove_expired_alerts() if not users.get_current_user(): results = {"date": dt.utcnow(), "redirect-url": users.create_login_url(self.request.uri)} self.write_json(results) return if key: logging.info("getting the key: " + key) try: data = memcache.get(key) or TSAlertsJSON.query_hash(key).json except AttributeError: self.response.write("This alert does not exist.") self.response.set_status(404, "Alert does not exist") return if not data: self.response.write("This alert does not exist.") self.response.set_status(404, "Alert does not exist") elif data.get("private", True) and not utils.is_googler(): logging.info("Permission denied.") self.abort(403) else: self.write_json(data.get(json, data)) else: query = TSAlertsJSON.query_active().fetch() data = [] for item in query: if item.json.get("private", True) and not utils.is_googler(): continue data.append(item.json) self.write_json({"alerts": data})
def delete(self, key): if not utils.is_googler(): self.response.set_status(403, "Permission Denied") return if key == "all": all_keys = TSAlertsJSON.query().fetch(keys_only=True) ndb.delete_multi(all_keys) for k in all_keys: logging.info("deleting key from memcache: " + k.id()) memcache.delete(k.id()) self.response.set_status(200, "Cleared all alerts") return changed_alert = TSAlertsJSON.query_hash(key) if not changed_alert: self.response.write("This alert does not exist.") self.response.set_status(404, "Alert does not exist") return memcache.delete(key) changed_alert.key.delete()
def put(self, key): if not utils.is_googler(): self.response.set_status(403, "Permission Denied") return changed_alert = TSAlertsJSON.query_hash(key) if not changed_alert: self.response.write("This alert does not exist.") self.response.set_status(404, "Alert does not exist") return try: data = json.loads(self.request.body) except ValueError: warning = ("Content %s was not valid JSON string.", self.request.body) self.response.set_status(400, warning) return logging.info("Alert before: " + str(changed_alert)) logging.info("Data: " + str(data)) changed_alert.json.update(data) logging.info("Alert after: " + str(changed_alert)) changed_alert.put() memcache.set(key, changed_alert.json) self.response.write("Updated ts-alerts.")
def get(self, timestamp=None): result_json = {} if not users.get_current_user(): result_json["login-url"] = users.create_login_url(self.request.uri) return result_json alerts = TSAlertsJSON.query_active().fetch() if timestamp: try: time = dt.fromtimestamp(int(timestamp)) except ValueError: self.response.set_status(400, "Invalid timestamp.") return if time > dt.utcnow(): self.response.write("Sheriff-o-matic cannot predict the future... yet.") self.response.set_status(400, "Invalid timestamp.") else: time = dt.utcnow() alerts += TSAlertsJSON.query(TSAlertsJSON.active_until > time).fetch() history = [] for a in alerts: ts, private = timestamp, a.json["private"] in_range = not (ts and utils.secs_ago(a.json["active_since_utc"], ts) < 0) permission = utils.is_googler() or not private if in_range and permission: history.append(a.json) result_json.update( { "timestamp": time.strftime("%s"), "time_string": time.strftime("%Y-%m-%d %H:%M:%S %Z"), "active_alerts": history, } ) self.write_json(result_json)