55 lines
1.6 KiB
Python
55 lines
1.6 KiB
Python
import models
|
|
import os
|
|
import tornado.ioloop
|
|
import tornado.web
|
|
import colour
|
|
|
|
models.db.connect()
|
|
|
|
curpath = os.path.dirname(os.path.abspath(__file__))
|
|
tplpath = os.path.join(curpath, "templates");
|
|
|
|
loader = tornado.template.Loader(tplpath, autoescape=None)
|
|
|
|
config = {
|
|
"port": 8881,
|
|
"conn": models.db
|
|
}
|
|
|
|
emotions = [ e for e in models.Emotion.select()]
|
|
|
|
|
|
class MainHandler(tornado.web.RequestHandler):
|
|
def get(self):
|
|
loader.reset()
|
|
html = loader.load('index.html').generate(host=self.request.host, emotions=emotions, ages=models.getAges(), groups=models.Group.select())
|
|
self.write(html)
|
|
|
|
class ColourHandler(tornado.web.RequestHandler):
|
|
def get(self):
|
|
req_group = self.get_query_argument("group", default=None)
|
|
req_age = self.get_query_argument("age", default=None)
|
|
req_emotion = self.get_query_argument("emotion", default=None)
|
|
req_elId = self.get_query_argument("elId", default="")
|
|
|
|
q = models.Artwork.select()
|
|
if req_group is not None:
|
|
group = models.Group.get(models.Group.id == req_group)
|
|
q = q.where(models.Artwork.group == group)
|
|
elif req_age is not None:
|
|
q = q.where(models.Artwork.age == req_age)
|
|
elif req_emotion is not None:
|
|
emotion = models.Emotion.get(models.Emotion.id == req_emotion)
|
|
q = q.where(models.Artwork.emotion == emotion)
|
|
images = q
|
|
svg = colour.getSvgFromDbImages(images, elId = req_elId)
|
|
self.write(svg)
|
|
|
|
if __name__ == "__main__":
|
|
print("Start server", config)
|
|
app = tornado.web.Application([
|
|
(r"/", MainHandler),
|
|
(r"/colours", ColourHandler),
|
|
])
|
|
app.listen(config['port'])
|
|
tornado.ioloop.IOLoop.current().start()
|