comparison web/templog.py @ 586:87c20b8c5472 default master

port to python3
author Matt Johnston <matt@ucc.asn.au>
date Mon, 09 Sep 2019 22:24:10 +0800
parents 8441916e3095
children
comparison
equal deleted inserted replaced
585:1c484dab4e83 586:87c20b8c5472
4 import json 4 import json
5 import hmac 5 import hmac
6 import zlib 6 import zlib
7 from datetime import datetime, timedelta 7 from datetime import datetime, timedelta
8 import time 8 import time
9 import urllib 9 import urllib.request, urllib.parse, urllib.error
10 import sys 10 import sys
11 import os
12 import traceback 11 import traceback
13 import fcntl
14 import hashlib 12 import hashlib
15 13
16 import bottle 14 import bottle
17 from bottle import route, request, response 15 from bottle import route, request, response
18 16
33 31
34 secure.setup_csrf() 32 secure.setup_csrf()
35 33
36 @route('/update', method='post') 34 @route('/update', method='post')
37 def update(): 35 def update():
38 js_enc = request.forms.data 36 js_enc = request.forms.data.strip().encode()
39 mac = request.forms.hmac 37 mac = request.forms.hmac
40 38
41 h = hmac.new(config.HMAC_KEY, js_enc.strip(), hashlib.sha256).hexdigest() 39 h = hmac.new(config.HMAC_KEY.encode(), js_enc, hashlib.sha256).hexdigest()
42 if h != mac: 40 if h != mac:
43 raise bottle.HTTPError(code = 403, output = "Bad key") 41 raise bottle.HTTPError(code = 403, output = "Bad key")
44 42
45 js = zlib.decompress(binascii.a2b_base64(js_enc)) 43 js = zlib.decompress(binascii.a2b_base64(js_enc))
46 44
47 params = json.loads(js) 45 params = json.loads(js.decode())
48 46
49 log.parse(params) 47 log.parse(params)
50 48
51 return "OK" 49 return "OK"
52 50
57 55
58 start_epoch = time.mktime(start.timetuple()) 56 start_epoch = time.mktime(start.timetuple())
59 return log.graph_png(start_epoch, length_minutes * 60) 57 return log.graph_png(start_epoch, length_minutes * 60)
60 58
61 def encode_data(data, mimetype): 59 def encode_data(data, mimetype):
62 return 'data:%s;base64,%s' % (mimetype, binascii.b2a_base64(data).rstrip()) 60 return 'data:%s;base64,%s' % (mimetype, binascii.b2a_base64(data).decode().rstrip())
63 61
64 @route('/graph.png') 62 @route('/graph.png')
65 def graph(): 63 def graph():
66 response.set_header('Content-Type', 'image/png') 64 response.set_header('Content-Type', 'image/png')
67 minutes, endstr = get_request_zoom() 65 minutes, endstr = get_request_zoom()
106 return bottle.template('set', 104 return bottle.template('set',
107 inline_data = inline_data, 105 inline_data = inline_data,
108 csrf_blob = secure.get_csrf_blob(), 106 csrf_blob = secure.get_csrf_blob(),
109 allowed = allowed, 107 allowed = allowed,
110 cookie_hash = cookie_hash, 108 cookie_hash = cookie_hash,
111 email = urllib.quote(config.EMAIL)) 109 email = urllib.parse.quote(config.EMAIL))
112 110
113 def get_request_zoom(): 111 def get_request_zoom():
114 """ returns (length, end) tuple. 112 """ returns (length, end) tuple.
115 length is in minutes, end is a DATE_FORMAT string """ 113 length is in minutes, end is a DATE_FORMAT string """
116 minutes = int(request.query.get('length', 26*60)) 114 minutes = int(request.query.get('length', 26*60))
149 minutes, endstr = get_request_zoom() 147 minutes, endstr = get_request_zoom()
150 148
151 request.query.replace('length', minutes) 149 request.query.replace('length', minutes)
152 request.query.replace('end', endstr) 150 request.query.replace('end', endstr)
153 151
154 urlparams = urllib.urlencode(request.query) 152 urlparams = urllib.parse.urlencode(request.query)
155 graphdata = encode_data(make_graph(minutes, endstr), 'image/png') 153 graphdata = encode_data(make_graph(minutes, endstr), 'image/png')
156 return bottle.template('top', urlparams=urlparams, 154 return bottle.template('top', urlparams=urlparams,
157 end = endstr, 155 end = endstr,
158 length = minutes, 156 length = minutes,
159 graphwidth = config.GRAPH_WIDTH, 157 graphwidth = config.GRAPH_WIDTH,