def get(self): count = 0 chp_data_last_updated = CHPData.last_updated() if chp_data_last_updated is not None: query = CHPIncident.all(keys_only=True) query.filter('updated <', chp_data_last_updated - timedelta(hours=1)) count = query.count() db.delete(query) self.response.write("Purged %d records." % count)
def get_incidents(self): """Return CHP incidents given various request args. """ id = self.request.get("id") center = self.request.get("center") dispatch = self.request.get("dispatch") area = self.request.get("area") city = self.request.get("city") since = self.request.get("since") memcache_key = "incidents-%s-%s-%s-%s-%s-%s" % (id, center, dispatch, area, city, since) memcache_expiry_time = 60 incidents = memcache.get(memcache_key) if incidents is None: if id == "": query = CHPIncident.all() query.order('-LogTime') if center != "": query.filter('CenterID =', center) if dispatch != "": query.filter('DispatchID =', dispatch) if area != "": query.filter('Area =', area) if city != "": query.filter('city =', city) if since != "": query.filter('LogTime >', datetime.fromtimestamp(float(since))) incidents = query.fetch(10000) else: # Handle single incident requests, slightly different approach # We want to use get_by_key_name() instead of filtering. incidents = [] incident = CHPIncident.get_by_key_name(id) if incident is not None: incidents.append(incident) try: memcache.add(memcache_key, incidents, memcache_expiry_time) except ValueError: pass if len(incidents) > 0: self.incidents_last_mod = max(incidents, key=lambda incident: incident.updated).updated else: self.incidents_last_mod = CHPData.last_updated() self.incidents = incidents