# HG changeset patch # User Matt Johnston # Date 1432137654 -28800 # Node ID 2d46d7f1ccf0d4c90a445b4e53845d84d3b6a47b # Parent d9b819dcac53d6b13abfe19e6635153eda6698dd# Parent 2071d939e4ffdd2cdf6ecb08ae2c9471ac6c4431 merge diff -r d9b819dcac53 -r 2d46d7f1ccf0 web/config.py --- a/web/config.py Wed May 20 23:10:37 2015 +0800 +++ b/web/config.py Thu May 21 00:00:54 2015 +0800 @@ -16,9 +16,11 @@ UPDATE_URL = 'http://evil.ucc.asn.au/~matt/templog/update' -GRAPH_WIDTH = 1200 -GRAPH_HEIGHT = 600 +GRAPH_WIDTH = 600 +GRAPH_HEIGHT = 700 ZOOM = 1 +# determine by viewing the image +GRAPH_LEFT_MARGIN = 65 LINE_WIDTH = 2 @@ -50,8 +52,6 @@ GRAPH_FONT = "Prociono" #GRAPH_FONT = "URW Gothic L" -# determine by zooming in an image viewer -GRAPH_LEFT_MARGIN = 63 # 1 hour CSRF_TIMEOUT = 3600 diff -r d9b819dcac53 -r 2d46d7f1ccf0 web/templog.py --- a/web/templog.py Wed May 20 23:10:37 2015 +0800 +++ b/web/templog.py Thu May 21 00:00:54 2015 +0800 @@ -11,6 +11,7 @@ import os import traceback import fcntl +import hashlib import bottle from bottle import route, request, response @@ -23,12 +24,23 @@ DATE_FORMAT = '%Y%m%d-%H.%M' ZOOM_SCALE = 2.0 +class TemplogBottle(bottle.Bottle): + def run(*args, **argm): + argm['server'] = 'gevent' + super(TemplogBottle, self).run(*args, **argm) + print "ran custom bottle" + +#bottle.default_app.push(TemplogBottle()) + +secure.setup_csrf() + @route('/update', method='post') def update(): js_enc = request.forms.data mac = request.forms.hmac - if hmac.new(config.HMAC_KEY, js_enc).hexdigest() != mac: + h = hmac.new(config.HMAC_KEY, js_enc.strip(), hashlib.sha256).hexdigest() + if h != mac: raise bottle.HTTPError(code = 403, output = "Bad key") js = zlib.decompress(binascii.a2b_base64(js_enc)) @@ -39,15 +51,22 @@ return "OK" +def make_graph(length, end): + length_minutes = int(length) + end = datetime.strptime(end, DATE_FORMAT) + start = end - timedelta(minutes=length_minutes) + + start_epoch = time.mktime(start.timetuple()) + return log.graph_png(start_epoch, length_minutes * 60) + +def encode_data(data, mimetype): + return 'data:%s;base64,%s' % (mimetype, binascii.b2a_base64(data).rstrip()) + + @route('/graph.png') def graph(): - length_minutes = int(request.query.length) - end = datetime.strptime(request.query.end, DATE_FORMAT) - start = end - timedelta(minutes=length_minutes) - response.set_header('Content-Type', 'image/png') - start_epoch = time.mktime(start.timetuple()) - return log.graph_png(start_epoch, length_minutes * 60) + return make_graph(request.query.length, request.query.end) @route('/set/update', method='post') def set_update(): @@ -75,11 +94,6 @@ csrf_blob = secure.get_csrf_blob(), allowed = allowed) -@route('/set_current.json') -def set_fresh(): - response.set_header('Content-Type', 'application/javascript') - return log.get_current() - @route('/') def top(): @@ -114,9 +128,12 @@ request.query.replace('end', end.strftime(DATE_FORMAT)) urlparams = urllib.urlencode(request.query) + graphdata = encode_data(make_graph(request.query.length, request.query.end), 'image/png') return bottle.template('top', urlparams=urlparams, end = end.strftime(DATE_FORMAT), - length = minutes) + length = minutes, + graphwidth = config.GRAPH_WIDTH, + graphdata = graphdata) @route('/debug') def debuglog(): @@ -138,8 +155,6 @@ response.set_header('Cache-Control', "public, max-age=1296000") return bottle.static_file(filename, root='static') -secure.setup_csrf() - def main(): #bottle.debug(True) #bottle.run(reloader=True) diff -r d9b819dcac53 -r 2d46d7f1ccf0 web/templog.wsgi --- a/web/templog.wsgi Wed May 20 23:10:37 2015 +0800 +++ b/web/templog.wsgi Thu May 21 00:00:54 2015 +0800 @@ -1,3 +1,5 @@ +#from gevent import monkey; monkey.patch_all() + import os import sys # Change working directory so relative paths (and template lookup) work again diff -r d9b819dcac53 -r 2d46d7f1ccf0 web/views/top.tpl --- a/web/views/top.tpl Wed May 20 23:10:37 2015 +0800 +++ b/web/views/top.tpl Thu May 21 00:00:54 2015 +0800 @@ -2,6 +2,7 @@ Wort Temperature Log +