Commit 8cdd7519 authored by Dominik Rosiek's avatar Dominik Rosiek

fix models for tornado

parent 30ee034e
...@@ -7,20 +7,25 @@ import decimal ...@@ -7,20 +7,25 @@ import decimal
#import boto.dynamodb2 #import boto.dynamodb2
#from boto.dynamodb2.table import Table #from boto.dynamodb2.table import Table
#from time import sleep #from time import sleep
from tornado import gen
class MainHandler(tornado.web.RequestHandler): class MainHandler(tornado.web.RequestHandler):
@gen.coroutine
def get(self): def get(self):
#stations = self.get_stations() #stations = self.get_stations()
#stations = models.Station.get_all() #stations = models.Station.get_all()
#stations = models.Station.getall() #stations = models.Station.getall()
stations = models.Station.get("b1d12bb6-a3b7-43b8-b6ab-6d6e62cb794e", tornado.ioloop.IOLoop.current()) stations = yield models.Station.get("b1d12bb6-a3b7-43b8-b6ab-6d6e62cb794e")
for s in stations: s = stations.result()
self.write("{0} {1} {2} {3} {4}<br />".format(s['station_id'], s['city'], s['longitude'], s['latitude'], s['name']))
measurement = yield models.Measurement.get("5c2be10b-5d33-47d7-95fe-b3f6fc29a3af")
m = measurement.result()
self.write("{0} {1} {2} {3} {4}<br />".format(s.id, s.city, s.longitude, s.latitude, s.name))
for s in stations: # measurement = models.Measurement.getlast(station=s.id)
measurement = models.Measurement.getlast(station=s.id) # self.write("".format())
self.write("".format())
#measurements = self.get_measurements() #measurements = self.get_measurements()
...@@ -60,5 +65,5 @@ def make_app(): ...@@ -60,5 +65,5 @@ def make_app():
if __name__ == "__main__": if __name__ == "__main__":
app = make_app() app = make_app()
app.listen(80) app.listen(8090)
tornado.ioloop.IOLoop.current().start() tornado.ioloop.IOLoop.current().start()
...@@ -2,12 +2,14 @@ import uuid ...@@ -2,12 +2,14 @@ import uuid
from db import DDBtypes, DDBmeasurements, DDBstations from db import DDBtypes, DDBmeasurements, DDBstations
from functools import partial from functools import partial
from tornado.gen import coroutine
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
class DDBobject(object): class DDBobject(object):
@classmethod @classmethod
@coroutine
def from_dict(cls, data): def from_dict(cls, data):
return cls(**data) return cls(**data)
...@@ -24,14 +26,18 @@ class Station(DDBobject): ...@@ -24,14 +26,18 @@ class Station(DDBobject):
self.name = name self.name = name
@classmethod @classmethod
@coroutine
def get(cls, station_id): def get(cls, station_id):
if cls.stored.get(station_id, None): if cls.stored.get(station_id, None):
return cls.stored.get(station_id) return cls.stored.get(station_id)
return cls.from_dict(IOLoop.instance().run_sync(partial(DDBstations().get, station_id))) result = yield DDBstations().get(station_id)
return cls.from_dict(result)
@coroutine
def save(self): def save(self):
return IOLoop.instance().run_sync(partial(DDBstations().add, station=self)) result = yield DDBstations().add(station=self)
return result
class Type(DDBobject): class Type(DDBobject):
...@@ -46,14 +52,17 @@ class Type(DDBobject): ...@@ -46,14 +52,17 @@ class Type(DDBobject):
self.description = description self.description = description
@classmethod @classmethod
@coroutine
def get(cls, type_id): def get(cls, type_id):
if cls.stored.get(type_id, None): if cls.stored.get(type_id, None):
return cls.stored.get(type_id) return cls.stored.get(type_id)
return cls.from_dict(IOLoop.instance().run_sync(partial(DDBtypes().get, type_id))) result = yield DDBtypes().get(type_id)
return cls.from_dict(result)
def save(self): def save(self):
return IOLoop.instance().run_sync(partial(DDBtypes().add, pollution_type=self)) result = yield (DDBtypes().add(pollution_type=self))
return result
class Measurement(DDBobject): class Measurement(DDBobject):
...@@ -67,19 +76,24 @@ class Measurement(DDBobject): ...@@ -67,19 +76,24 @@ class Measurement(DDBobject):
self.time = time self.time = time
@classmethod @classmethod
@coroutine
def get(cls, measurement_id): def get(cls, measurement_id):
if cls.stored.get(measurement_id, None): if cls.stored.get(measurement_id, None):
return cls.stored.get(measurement_id) return cls.stored.get(measurement_id)
data = IOLoop.instance().run_sync(partial(DDBmeasurements().get, measurement_id)) data = yield DDBmeasurements().get(measurement_id)
data['station'] = Station.get(data['station_id']) station = yield Station.get(data['station_id'])
data['station'] = station.result()
del data['station_id'] del data['station_id']
data['pollution_type'] = Type.get(data['type_id']) pollution_type = yield Type.get(data['type_id'])
data['pollution_type'] = pollution_type.result()
del data['type_id'] del data['type_id']
return cls.from_dict(data) return cls.from_dict(data)
@coroutine
def save(self): def save(self):
return IOLoop.instance().run_sync(partial(DDBmeasurements().add, measurement=self)) result = yield DDBmeasurements().add(measurement=self)
return result
import models import models
station = models.Station(1, "Kraków", 12, 13, "Aleje") station = models.Station(1, "Kraków", 12, 13, "Aleje")
station.save() station.save()
station2 = models.Station.get(station.id) station2 = models.Station.get(station.id)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment