Mercurial > personal > weather-server
comparison weather_server/server.py @ 2:cda47993a193
server: fix bugs and improve template.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sun, 29 Sep 2019 01:18:18 -0400 |
parents | f66df122f18d |
children | b42c4bfe57c7 |
comparison
equal
deleted
inserted
replaced
1:f66df122f18d | 2:cda47993a193 |
---|---|
23 req = flask.request | 23 req = flask.request |
24 target = req.args.get('location') | 24 target = req.args.get('location') |
25 if not target: | 25 if not target: |
26 flask.abort(404) | 26 flask.abort(404) |
27 try: | 27 try: |
28 target_loc = locs.get(target) | 28 target_loc, logger = locs.get(target) |
29 except KeyError: | 29 except KeyError: |
30 flask.abort(404) | 30 flask.abort(404) |
31 | 31 |
32 password = req.args.get('password') | 32 password = req.args.get('password') |
33 if password != target_loc.password: | 33 if password != target_loc.password: |
41 temp_c=item['temp_c'], | 41 temp_c=item['temp_c'], |
42 rh_pct=item['rh_pct'], | 42 rh_pct=item['rh_pct'], |
43 ) | 43 ) |
44 for item in reader | 44 for item in reader |
45 ] | 45 ] |
46 target_loc.logger.write_rows(entries) | 46 logger.write_rows(entries) |
47 return {'status': 'OK'} | 47 return flask.jsonify({'status': 'OK'}) |
48 | 48 |
49 @app.route('/<location>') | 49 @app.route('/<location>') |
50 def show(location): | 50 def show(location): |
51 try: | 51 try: |
52 loc, logger = locs.get(location) | 52 loc, logger = locs.get(location) |
53 except KeyError: | 53 except KeyError: |
54 flask.abort(404) | 54 flask.abort(404) |
55 data = logger.data | 55 data = logger.data |
56 if data: | 56 if data: |
57 last_reading = data[-1] | 57 last_reading = data[-1] |
58 tz = loc.timezone | 58 tz = loc.timezone() |
59 date = tz.normalize(last_reading.sample_time.astimezone(tz)) | 59 date = tz.normalize(last_reading.sample_time.astimezone(tz)) |
60 else: | 60 else: |
61 last_reading = None | 61 last_reading = None |
62 date = None | 62 date = None |
63 return flask.render_template( | 63 return flask.render_template( |