From 41760cb473535ee479682da6e4a9a049f1056b7c Mon Sep 17 00:00:00 2001 From: yvesf Date: Thu, 17 Jun 2010 23:07:01 +0000 Subject: update controller git-svn-id: http://10.2.2.13/svn/common/ebus@1680 d0e8fea9-7529-0410-93fb-d39fd5b9c1dd --- ebus/web/controller.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/ebus/web/controller.py b/ebus/web/controller.py index f581e13..c5b50d2 100644 --- a/ebus/web/controller.py +++ b/ebus/web/controller.py @@ -8,7 +8,6 @@ from sqlalchemy.orm import sessionmaker from ebus.web import model - Session = sessionmaker() class CacheDecorator(object): @@ -29,14 +28,8 @@ class CacheDecorator(object): self.func = func return self.cachedFunc -def data(sensor, from_date=None, till_date=None): - session = Session() - values = session.query(model.Value)\ - .join(model.Sensor)\ - .filter(model.Sensor.name == sensor)\ - .all() - return {"sensor":sensor, "from_date":from_date, "till_date":till_date, "values":values} +## Web Requests @itty.get("/") def index(req): filename=os.path.join(os.path.dirname(__file__), "static", "index.html") @@ -49,7 +42,19 @@ def index(req): timeout=20) def json(req,sensor=None,from_date=None,till_date=None): print "sensor=%s from_date=%s till_date=%s" % (sensor,from_date,till_date) - return itty.Response(dump_json(data(sensor)), + values = Session().query(model.Value).join(model.Sensor) + + if sensor: + values = values.filter(model.Sensor.name == sensor)\ + + if from_date: + values = values.filter(model.Value.timestamp >= from_date) + + if till_date: + values = values.filter(model.Value.timestamp <= till_date) + + response = {"sensor":sensor, "from_date":from_date, "till_date":till_date, "values":values.all()} + return itty.Response(dump_json(response), content_type="application/json") @itty.get("/static/(?P.+)") @@ -58,6 +63,8 @@ def static(request,filename): return itty.Response(file, content_type=itty.content_type(file)) + +## Start method def run(db_engine,host="0.0.0.0",port=5000): Session.configure(bind=db_engine) return itty.run_itty(host=host,port=port) -- cgit v1.2.1