Beispiel #1
0
	def get(self):
	  # We need to be careful here.  It's possible this handler could run in the
	  # window between when the CHPData is updated and when the incidents get
	  # updated.  So throw in 5 minutes of padding.
	  #
	  # See also: https://developers.google.com/appengine/docs/python/ndb/#writes
		query = CHPIncident.query(CHPIncident.updated < CHPData.last_updated() - timedelta(minutes=5))
		count = query.count()
		keys = query.fetch(keys_only=True)
		ndb.delete_multi(keys)

		self.response.write("Purged %d records." % count)
	def get_incidents(self):
		"""Return CHP incidents given various request args.

		"""
		center = self.request.get("center")
		dispatch = self.request.get("dispatch")
		area = self.request.get("area")
		city = self.request.get("city")

		# Change this for the West Sac News-Leger since no one appears home there
		if "http://www.westsac.com/news-ledger" in self.request.headers['User-Agent']:
			if dispatch == "":
				dispatch = 'SACC'

		memcache_key = "incidents-%s-%s-%s-%s" % (center, dispatch, area, city)

		# Don't even try the cache if this looks like a PubSubHubBub request.
		if "pubsubhubbub" in self.request.headers['User-Agent']:
			incidents = None
		else:
			incidents = memcache.get(memcache_key)

		if incidents is None:
			query = CHPIncident.query().order(-CHPIncident.LogTime)

			if city != "":
				query = query.filter(CHPIncident.city == city)
			elif area != "":
				query = query.filter(CHPIncident.Area == area)
			elif dispatch != "":
				query = query.filter(CHPIncident.DispatchID == dispatch)
			elif center != "":
				query = query.filter(CHPIncident.CenterID == center)

			incidents = query.fetch(10000)

			try:
				memcache.set(memcache_key, incidents, 300)
			except:
				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