Mercurial > personal > weather-server
comparison weather_server/server.py @ 23:88249e451566
server: show date when last report was >12h ago.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sun, 10 Nov 2019 19:42:04 -0500 |
parents | beb42c835c52 |
children | 7def5611895b |
comparison
equal
deleted
inserted
replaced
22:e229afdd447b | 23:88249e451566 |
---|---|
53 if last_reading: | 53 if last_reading: |
54 tz = loc.timezone() | 54 tz = loc.timezone() |
55 date = tz.normalize(last_reading.sample_time.astimezone(tz)) | 55 date = tz.normalize(last_reading.sample_time.astimezone(tz)) |
56 else: | 56 else: |
57 date = None | 57 date = None |
58 now = datetime.datetime.now(tz=pytz.UTC) | |
59 diff = (now - date) if date else None | |
60 is_recent = diff and diff < datetime.timedelta(hours=12) | |
58 return flask.render_template( | 61 return flask.render_template( |
59 'location.html', | 62 'location.html', |
60 location=loc, | 63 location=loc, |
61 last_reading=last_reading, | 64 last_reading=last_reading, |
62 date=date) | 65 date=date, |
66 date_format=f'%H:%M' if is_recent else '%Y-%m-%d %H:%M') | |
63 | 67 |
64 @app.route('/<location>/recent') | 68 @app.route('/<location>/recent') |
65 def recent(location: str): | 69 def recent(location: str): |
66 try: | 70 try: |
67 loc = locs.get(location) | 71 loc = locs.get(location) |