def get(self): q = PachubeMapping.all() q.order("stationid") self.response.headers["Content-Type"] = "text/html" parsepath = os.path.join(os.path.dirname(__file__), "templates", "particulates.html") self.response.out.write(template.render(parsepath, {"mappings": q}))
def post(self, stationid): stationid = self.request.get("stationid") stationname = self.request.get("stationname") value = self.request.get("value") logging.info("updating pachube with for station %s with value %s", stationid, value) if PACHUBE: eemlpath = os.path.join(os.path.dirname(__file__), "templates", "eeml.xml") eeml = template.render(eemlpath, {"station": stationid, "stationname": stationname, "value": value}) # Check if there's a Pachube association q = PachubeMapping.all().filter("stationid =", stationid) if q.count() == 0: logging.info("creating feed for station %s", stationid) # Create a Pachube FEED for this station feedcreate = urlfetch.fetch( url="http://www.pachube.com/api.xml?key=" + PACHUBE_API_KEY, payload=eeml, method=urlfetch.POST ) location = feedcreate.headers["location"] feedid = location.split("/")[-1].split(".")[0] logging.info("station %s feed created at %s", stationid, location) p = PachubeMapping(stationid=stationid, stationname=stationname, pachubeid=feedid) p.put() else: # We do have a Pachube feed id for this station feedid = q[0].pachubeid logging.info("sending update to pachube feed %s", feedid) # Update that station logging.info("updating station %s on http://www.pachube.com/api/%s.xml", stationid, feedid) try: pachubeupdate = urlfetch.fetch( url="http://www.pachube.com/api/%s.xml?key=%s" % (feedid, PACHUBE_API_KEY), payload=eeml, method=urlfetch.PUT, ) except urlfetch.DownloadError: traceback.print_exc() logging.info("urlfetch failed in updating feed %s" % feedid)
def get(self, stationid): response = {} q = ParticulateData.all() q.filter("stationid =", stationid) q.filter("date > ", date.today() - timedelta(90)) q.order("date") results = q.fetch(1000) data = [] for result in results: data.append(result.value) logging.info("data got for station %s" % stationid) response["data"] = data # Pachube is not very happy if we hammer them on every index load environmentXML = memcache.get("%s_environmentxml" % stationid) if environmentXML is None: mappingQuery = PachubeMapping.all() mappingQuery.filter("stationid =", stationid) mapping = None if mappingQuery.count(): mapping = mappingQuery[0] environmentURL = "http://www.pachube.com/api/%s.xml?key=%s" % (mapping.pachubeid, PACHUBE_API_KEY) logging.info("fetching url: %s", environmentURL) environmentGet = urlfetch.fetch(url=environmentURL) if environmentGet.status_code == 200 and environmentGet.content: environmentXML = environmentGet.content memcache.set("%s_environmentxml" % stationid, environmentGet.content, time=7 * 24 * 60 * 60) lat = "" lon = "" logging.debug("environment xml is - %s", environmentXML) if environmentXML: lat = re.search("<lat>(.+?)</lat>", environmentXML).group(1) lon = re.search("<lon>(.+?)</lon>", environmentXML).group(1) logging.info("got lon %s and lat %s", lon, lat) response["lat"] = lat response["lon"] = lon self.response.headers["Content-Type"] = "text/json" self.response.out.write(simplejson.dumps(response))
def post(self): # TODO close off delete for admin only # users.is_current_user_admin() q = PachubeMapping.all() for mapping in q: logging.info("deleting pachube feed %s", mapping.pachubeid) urlfetch.fetch( url="http://www.pachube.com/api/%s?key=%s" % (mapping.pachubeid, PACHUBE_API_KEY), method=urlfetch.DELETE, ) logging.info("deleting mapping") mapping.delete()
def get(self, stationid): q = ParticulateData.all() q.filter("stationid =", stationid) q.filter("date > ", date.today() - timedelta(90)) q.order("date") results = q.fetch(1000) mappingQuery = PachubeMapping.all() mappingQuery.filter("stationid =", stationid) mapping = None if mappingQuery.count(): mapping = mappingQuery[0] values = {"values": results, "station": stationid, "mapping": mapping} # Add the flash if we got it if self.request.get("flash", ""): values["flash"] = self.request.get("flash") self.response.headers["Content-Type"] = "text/html" templatepath = os.path.join(os.path.dirname(__file__), "templates", "station.html") self.response.out.write(template.render(templatepath, values))